如何使用Python将HTML转换为PDF

在本教程中,我们将介绍如何实现 Web 呈现器的异步方法。您只需要将 HTML 内容发送到 SubmitPDFFromHTMLTask 或 SubmitImageFromHTMLTask API 方法即可接收任务标识符作为响应。还有SubmitPDFFromUrlTask和SubmitImageFromUrlTask方法,用于从公共Web Uri呈现PDF或图像。 收到任务标识符后,您需要通过调用 GetRenderingTaskStatus 方法来检查任务状态。只要任务状态等于“Waiting”,您需要在几秒钟延迟后调用相同的方法。一旦你从 GetRenderingTaskStatus 方法收到“Completed”,你可以调用 DownloadResult 方法来下载最终文件。您可以使用下面的 Python 实现。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 import requests import time def submit_pdf_from_html_task(htmlContent, uri, rapid_api_key): multipart_form_data = { 'uri': (None, uri), 'useCompression': (None, 'false'), 'pageLabel': (None, 'Home Page'), 'messageLabel': (None, 'Initial Capture'), 'browserHeight': (None, '768'), 'browserWidth': (None, '1024'), 'htmlContent': (None, htmlContent) } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.post('https://web-renderer.p.rapidapi.com/SubmitPDFFromHTMLTask', files=multipart_form_data, headers=headers) if response.status_code != 200: raise Exception('Cannot create task') return response.text def get_rendering_task_status(task_id, rapid_api_key): params = { 'taskId': task_id } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.get('https://web-renderer.p.rapidapi.com/GetRenderingTaskStatus', headers=headers, params=params) if response.status_code != 200: raise Exception("Cannot check task status") return response.text def download_result(task_id, rapid_api_key): params = { 'taskId': task_id } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.get('https://web-renderer.p.rapidapi.com/DownloadResult', headers=headers, params=params) if response.status_code != 200: raise Exception('Cannot download file') return response.content if __name__ == '__main__': rapid_api_key = '<YOUR RAPIDAPI KEY HERE>' html_content = '<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"></head><body><h1>Hello World!</h1></body></html>' uriOfHtmlContent = 'https://www.google.com?hl=en' task_id = submit_pdf_from_html_task(html_content, uriOfHtmlContent, rapid_api_key) retry_count = 0 while retry_count < 100: retry_count += 1 time.sleep(5) status = get_rendering_task_status(task_id, rapid_api_key) if status == 'Completed': file_bytes = download_result(task_id, rapid_api_key) with open('html_from_html.pdf', mode='wb') as binary_pdf: binary_pdf.write(file_bytes) break elif status == 'Waiting': continue elif status == 'Failed': raise Exception('Cannot render html') else: raise Exception('Invalid status')

2 分钟 · 塞巴斯蒂安·克罗西奇

如何使用Python将文档转换为PDF

在本教程中,我们将介绍如何实现文档转换套件的异步方法。您只需将源文档发送到 SubmitTIFFConversionTask 或 SubmitPDFConversionTask API 方法即可接收任务标识符作为响应。还有SubmitDOCXConversionTask方法将PDF文档转换为可编辑的Microsoft Word文档。 收到任务标识符后,您需要通过调用 GetConversionTaskStatus 方法检查任务状态。只要任务状态等于“Waiting”,您需要在几秒钟延迟后调用相同的方法。一旦您从GetConversionTaskStatus方法收到“Completed”,您可以调用DownloadResult方法来下载最终文档文件。您可以使用下面的 Python 实现。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 import requests import time def submit_pdf_conversion_task(file_path_to_convert, rapid_api_key): multipart_form_data = { 'file': (file_path_to_convert, open(file_path_to_convert, 'rb')), 'authorName': (None, 'Sebastian'), 'title': (None, 'Final report'), 'keywords': (None, 'Reports, Final'), 'name': (None, 'Quarterly Final report') } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.post('https://petadata-document-conversion-suite.p.rapidapi.com/SubmitPDFConversionTask', files=multipart_form_data, headers=headers) if response.status_code != 200: raise Exception('Cannot create task') return response.text def get_conversion_task_status(task_id, rapid_api_key): params = { 'taskId': task_id } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.get('https://petadata-document-conversion-suite.p.rapidapi.com/GetConversionTaskStatus', headers=headers, params=params) if response.status_code != 200: raise Exception("Cannot check task status") return response.text def download_result(task_id, rapid_api_key): params = { 'taskId': task_id } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.get('https://petadata-document-conversion-suite.p.rapidapi.com/DownloadResult', headers=headers, params=params) if response.status_code != 200: raise Exception('Cannot download file') return response.content if __name__ == '__main__': rapid_api_key = '<YOUR RAPIDAPI KEY HERE>' file_path_to_convert = '<FILE PATH TO CONVERT>' task_id = submit_pdf_conversion_task(file_path_to_convert, rapid_api_key) retry_count = 0 while retry_count < 100: retry_count += 1 time.sleep(5) status = get_conversion_task_status(task_id, rapid_api_key) if status == 'Completed': file_bytes = download_result(task_id, rapid_api_key) with open('final_report.pdf', mode='wb') as binary_pdf: binary_pdf.write(file_bytes) break elif status == 'Waiting': continue elif status == 'Failed': raise Exception('Cannot convert file') else: raise Exception('Invalid status')

2 分钟 · 塞巴斯蒂安·克罗西奇

使用Python将任何图像转换为WebP

在本教程中,我们将介绍如何实现图像转换器的异步方法。您只需将源图像发送到 SubmitWebPConversionTask 或任何其他提交 API 方法(如 SubmitBmpConversionTask、SubmitDicomConversionTask 等)即可接收任务标识符作为响应。 收到任务标识符后,您需要通过调用 GetConversionTaskStatus 方法检查任务状态。只要任务状态等于“Waiting”,您需要在几秒钟延迟后调用相同的方法。一旦您从GetConversionTaskStatus方法收到“Completed”,您就可以调用DownloadResult方法来下载最终图像。您可以使用下面的 Python 实现。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 import requests import time def submit_webp_conversion_task(file_path_to_convert, rapid_api_key): multipart_form_data = { 'file': (file_path_to_convert, open(file_path_to_convert, 'rb')), 'lossy': (None, 'true') } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.post('https://image-converter4.p.rapidapi.com/submitWebPConversionTask', files=multipart_form_data, headers=headers) if response.status_code != 200: raise Exception('Cannot create task') return response.text def get_conversion_task_status(task_id, rapid_api_key): params = { 'taskId': task_id } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.get('https://image-converter4.p.rapidapi.com/getConversionTaskStatus', headers=headers, params=params) if response.status_code != 200: raise Exception("Cannot check task status") return response.text def download_result(task_id, rapid_api_key): params = { 'taskId': task_id } headers = { 'X-RapidAPI-Key': rapid_api_key } response = requests.get('https://image-converter4.p.rapidapi.com/downloadResult', headers=headers, params=params) if response.status_code != 200: raise Exception('Cannot download file') return response.content if __name__ == '__main__': rapid_api_key = '<YOUR RAPIDAPI KEY HERE>' file_path_to_convert = '<PATH OF SOURCE IMAGE TO CONVERT>' task_id = submit_webp_conversion_task(file_path_to_convert, rapid_api_key) retry_count = 0 while retry_count < 100: retry_count += 1 time.sleep(5) status = get_conversion_task_status(task_id, rapid_api_key) if status == 'Completed': file_bytes = download_result(task_id, rapid_api_key) with open('final_image.webp', mode='wb') as binary_pdf: binary_pdf.write(file_bytes) break elif status == 'Waiting': continue elif status == 'Failed': raise Exception('Cannot convert file') else: raise Exception('Invalid status')

2 分钟 · 塞巴斯蒂安·克罗西奇