How to Convert HTML to PDF with Python
Why Convert HTML to PDF with Python?
The pdfRest Convert to PDF API Tool is a powerful resource for developers who need to convert various file types to PDF. This tool simplifies the process of conversion by handling different file formats and providing a consistent output. Using Python to send an API call to Convert to PDF is practical for automating document workflows, integrating PDF generation into web services, or simply converting documents in batch processes.
For instance, a user might want to convert scanned TIFF images to PDFs for easier sharing and archiving, or to ensure compatibility with document viewers.
Convert HTML to PDF with Python Code Example
from requests_toolbelt import MultipartEncoder import requests import json pdf_endpoint_url = 'https://api.pdfrest.com/pdf' # The /pdf endpoint can take a single file, id, or url as input. # This sample passes an HTML file to the endpoint, but there's a variety of input file types that are accepted by this endpoint. # The 'text/html' string below is known as a MIME type, which is a label used to identify the type of a file so that it is handled properly by software. # Please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types for more information about MIME types. mp_encoder_pdf = MultipartEncoder( fields={ 'file': ('file_name.html', open('/path/to/file', 'rb'), 'text/html'), 'output' : 'example_pdf_out', } ) # Let's set the headers that the pdf endpoint expects. # Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below. headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_pdf.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here } print("Sending POST request to pdf endpoint...") response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers) print("Response status code: " + str(response.status_code)) if response.ok: response_json = response.json() print(json.dumps(response_json, indent = 2)) else: print(response.text) # If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.
This code is sourced from the pdf-rest-api-samples repository on GitHub.
Breaking Down the Python Code
Let's break down the provided Python code snippet:
from requests_toolbelt import MultipartEncoder import requests import json
This imports the necessary libraries. MultipartEncoder
is used for encoding files as multipart/form-data.
pdf_endpoint_url = 'https://api.pdfrest.com/pdf'
This sets the API endpoint URL for the pdfRest Convert to PDF service.
mp_encoder_pdf = MultipartEncoder( fields={ 'file': ('file_name.html', open('/path/to/file', 'rb'), 'text/html'), 'output' : 'example_pdf_out', } )
Here, a MultipartEncoder
object is created with the file to be converted and the desired output file name. The MIME type 'text/html' is specified for the input file.
headers = { 'Accept': 'application/json', 'Content-Type': mp_encoder_pdf.content_type, 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }
The headers for the request are set, including the API key for authentication.
response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers)
A POST request is made to the pdfRest API with the encoded file and headers.
if response.ok: response_json = response.json() print(json.dumps(response_json, indent = 2)) else: print(response.text)
After receiving the response, the code checks if the request was successful. If so, it prints the JSON response; otherwise, it prints the error text.
Beyond the Tutorial
In this tutorial, we've learned how to use Python to send an .html file to the pdfRest Convert to PDF API and receive a JSON response. By following these steps, you can integrate HTML to PDF conversion into your applications or automate document processing tasks. To further explore the capabilities of pdfRest API Tools, you can demo them in the API Lab and refer to the API Reference documentation at https://pdfrest.com/documentation/.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found in our GitHub repository.