Convert HTML to PDF using REST API in Python

As a Python programmer, you have the capability to programmatically transform your HTML files into PDF documents in a cloud-based environment. This conversion process can prove invaluable for record-keeping purposes and for distributing HTML content in a more portable PDF format. In this guide, we will explore the process of utilizing a REST API in Python to convert HTML into PDF documents.

The following topics shall be covered in this article:

HTML to PDF Conversion REST API and Python SDK

For converting HTML files to PDF, I will be using the Python SDK of GroupDocs.Conversion Cloud API. It allows you to convert your documents and images of any supported file format to any format you need. You can easily convert between over 50 types of documents and images such as Word, PowerPoint, Excel, PDF, HTML, CAD, raster images, etc.

You can install GroupDocs.Conversion Cloud to your Python project using the following command in the console:

pip install groupdocs_conversion_cloud

Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your ID and secret, please add in the code as shown below:

client_id = "659fe7da-715b-4744-a0f7-cf469a392b73"
client_secret = "b377c36cfa28fa69960ebac6b6e36421"
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = ""

Convert HTML to PDF using REST API in Python

You can convert your HTML file into PDF documents by following the simple steps mentioned below:

Upload the Document

Firstly, upload the HTML file to the cloud using the code example given below:

# create instance of the API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("sample.html", "C:\\Files\\sample.html", my_storage)
# upload sample file
response = file_api.upload_file(request)

As a result, the uploaded HTML file will be available in the files section of your dashboard on the cloud.

Convert HTML to PDF in Python

You can easily convert HTML to PDF documents programmatically by following the steps given below:

  • Create an instance of ConvertApi
  • Create an instance of the ConvertSettings
  • Set the HTML file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Define PdfConvertOptions if required
  • Optionally set various properties such as dpi, margin_top, margin_left, fit_window, etc.
  • Create ConvertDocumentRequest with ConvertSettings
  • Get results by calling the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert your HTML file to a PDF document using a REST API.

# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.html"
settings.format = "pdf"
settings.output_path = "converted"
# PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.center_window = True # Set position of the document's window on the screen
convertOptions.display_doc_title = True
convertOptions.dpi = 1024.0
convertOptions.fit_window = False
convertOptions.from_page = 1 # Start conversion from FromPage page
convertOptions.linearize = False # Linearize PDF Document for the Web
convertOptions.margin_top = 5
convertOptions.margin_left = 5
convertOptions.unembed_fonts = True # Make fonts not embedded
convertOptions.remove_pdfa_compliance = False # Remove Pdf-A Compliance
settings.convert_options = convertOptions
# Convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# convert
result = convert_api.convert_document(request)
# Done
print("Document converted: " + result[0].path)
Convert HTML to PDF using REST API in Python

Convert HTML to PDF using REST API in Python

Download the Converted File

The above code sample will save the converted PDF file on the cloud. You can download it using the following code sample:

# API initialization
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("converted\\sample.pdf", my_storage)
# download file
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")

Convert HTML to PDF and Add Watermark

You can convert HTML to PDF documents and add watermarks to converted documents programmatically by following the steps given below:

  • Create an instance of the ConvertApi
  • Create an instance of the ConvertSettings
  • Set the HTML file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Create an instance of the WatermarkOptions
  • Set Watermark Text, Color, Width, Height, etc.
  • Define the PdfConvertOptions and assign WatermarkOptions
  • Create ConvertDocumentRequest with ConvertSettings
  • Convert by calling the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert an HTML file to a PDF document and add a watermark to the converted PDF document using a REST API in Python. Please follow the steps mentioned earlier to upload and download files.

# api initialization
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.html"
settings.format = "pdf"
settings.output_path = "converted"
# define watermark options
watermark = groupdocs_conversion_cloud.WatermarkOptions()
watermark.text = "THIS IS SAMPLE WATERMARK"
watermark.bold = True;
watermark.font_size = 34;
watermark.color = "Gray";
watermark.background = False;
watermark.rotation_angle = 30;
watermark.left = 90;
watermark.top = 500;
# define PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.watermark_options = watermark
settings.convert_options = convertOptions
# create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# convert
result = convert_api.convert_document(request)
print("Document converted successfully: " + result[0].url)
Convert HTML to PDF and Add Watermark

Convert HTML to PDF and Add Watermark

HTML to PDF Conversion without using Cloud Storage

You can convert HTML to PDF documents without using cloud storage by following the steps given below:

  • Create an instance of the ConvertApi
  • Create ConvertDocumentDirectRequest and pass requested document format and the input file path
  • Get results by calling the convert_document_direct_()_ method with ConvertDocumentDirectRequest
  • Optionally, save the output file to the local path using shutil.move() method

The following code example shows how to convert an HTML file to a PDF document without using cloud storage. You will pass the input file in the request body and receive the output file in the API response.

# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare request
request = groupdocs_conversion_cloud.ConvertDocumentDirectRequest("pdf", "C:\\Files\\sample.html")
# Convert
result = convert_api.convert_document_direct(request)
# Move downloaded file to your working directory
shutil.move(result, "C:\\Files\\")

Convert HTML to PDF and Download Directly

You can easily convert HTML to PDF documents programmatically by following the steps given below:

  • Create an instance of ConvertApi
  • Create an instance of the ConvertSettings
  • Set the HTML file path
  • Assign “pdf” to the format
  • Set “None” to the output path
  • Create ConvertDocumentRequest with ConvertSettings
  • Get results by calling the convert_document_download() method
  • Optionally, save the output file to the local path using shutil.move() method

The following code example shows how to convert an HTML file to a PDF document and download it directly using a REST API in Python. The API shall return the converted PDF file in response. Please follow the steps mentioned earlier to upload a file.

# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "sample.html"
settings.format = "pdf"
settings.output_path = None # leave OutputPath will result the output as document IOStream
# Prepare request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert
response = convert_api.convert_document_download(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")

Try Online

Please try the following free online HTML to PDF conversion tool, which is developed using the above API. https://products.groupdocs.app/conversion/html-to-pdf

Conclusion

In this article, you’ve gained insights into several valuable aspects. First, you’ve discovered the process of converting HTML files into PDF documents using Python in a cloud-based environment. Additionally, you’ve learned how to perform this conversion without relying on cloud storage, enabling programmatic flexibility.

Furthermore, you’ve acquired knowledge on enhancing PDF documents by adding watermarks using Python. The article has also elucidated the step-by-step procedure for programmatically uploading an HTML file to the cloud and subsequently downloading the converted PDF file.

For a deeper understanding and further exploration, the documentation for GroupDocs.Conversion Cloud API is available. Additionally, we offer an interactive API Reference section, allowing you to visualize and interact with our APIs directly from your browser.

If you encounter any uncertainties or require assistance, please do not hesitate to reach out to our community on the forum. We are here to provide support and clarification.

See Also