· tutorials · 5 min read

How to Convert HTML to PDF with xhtml2pdf

Dive into the seamless conversion of HTML to PDF in Python using the xhtml2pdf library, featuring clear code examples for a quick start.

Dive into the seamless conversion of HTML to PDF in Python using the xhtml2pdf library, featuring clear code examples for a quick start.

Introduction

xhtml2pdf is a popular Python library that facilitates the conversion of HTML to PDF format. It leverages HTML and CSS to create PDFs, making it an ideal choice for Python developers looking to generate PDF documents from web content. This article guides you through the process of using xhtml2pdf, complete with straightforward code examples.

Why xhtml2pdf?

  • Ease of Use: Simple and intuitive API, making it accessible for beginners.
  • CSS Support: Good support for CSS to style PDF documents.
  • Flexibility: Works well for both simple and moderately complex PDF generation tasks.

Getting Started with xhtml2pdf

Step 1: Install xhtml2pdf

First, you need to install xhtml2pdf. You can do this easily using pip:

Terminal window
pip install xhtml2pdf

Step 2: Convert HTML to PDF

With xhtml2pdf, you can convert a simple HTML string to a PDF file or read from an HTML file. Here’s an example of converting an HTML string to a PDF:

from xhtml2pdf import pisa
from io import BytesIO
# Define your HTML content
html_content = """
<html>
<head>
<title>Convert HTML to PDF</title>
</head>
<body>
<h1>Welcome to xhtml2pdf!</h1>
<p>This is a simple HTML to PDF conversion example.</p>
</body>
</html>
"""
# Create a BytesIO buffer for the PDF
output = BytesIO()
# Convert HTML content to PDF
pdf_status = pisa.CreatePDF(BytesIO(html_content.encode('utf-8')), dest=output)
# Check if there was an error
if pdf_status.err:
print("An error occurred.")
else:
# Save the PDF to a file
with open("output.pdf", "wb") as pdf_file:
pdf_file.write(output.getvalue())
print("PDF created successfully.")

In this example, pisa.CreatePDF is used to convert the HTML content into a PDF. The result is written into a BytesIO buffer, which is then saved to a file.

Converting HTML Files to PDF

To convert an HTML file to a PDF, you can modify the above example to read the HTML content from a file:

from xhtml2pdf import pisa
# Function to convert HTML to PDF
def convert_html_to_pdf(source_html, output_filename):
with open(source_html, 'r') as html_file:
html_content = html_file.read()
with open(output_filename, 'wb') as pdf_file:
pisa_status = pisa.CreatePDF(html_content, dest=pdf_file)
if pisa_status.err:
return False
return True
# Convert an HTML file to a PDF file
if convert_html_to_pdf('input.html', 'output.pdf'):
print("PDF created successfully.")
else:
print("An error occurred.")

This function reads the HTML content from source_html and writes the converted PDF to output_filename. The pisa.CreatePDF function does the conversion.

Other Python libraries

There are other Python libraries capable of converting HTML to PDF and you can find more information about it in this article on How To Convert HTML to PDF with Python.

Simplifying PDF Generation with Templated API

While xhtml2pdf is a powerful tool for Python developers, it may have limitations when dealing with complex template management, PDF tracking, and advanced rendering. Templated offers a more comprehensive solution by providing:

  1. A drag-and-drop template editor
  2. Automatic PDF tracking
  3. Chromium-based rendering for accurate results
  4. A scalable API approach

With Templated, you can streamline PDF creation, saving time and ensuring high-quality output.

This approach simplifies the process and eliminates the need for extensive manual template management and tracking. To get started with PDF generation using Templated, follow the documentation and harness the power of this efficient solution.

1. Create PDFs with templates

Using Templated you can design your PDF template using a drag-and-drop editor and then create PDFs programmatically.
Check the video below to see an example of the editor:

After logging in you will see your Dashboard where you can manage your templates or create new ones:

How to create a template from the dashboard

From your Dashboard, you can design your own templates or customize a existing one from our Template Gallery. Bellow is the Certificate of Achievement Template you can use.
There are 100+ free templates available that you can pick and customize to your needs.

Shows the certicate template in the Template Editor

To start using Templated API, you need to get your API key that can be found on the API Integration tab on your dashboard.

Shows where to get the API key from the Dashboard

Now that you have your Templated account ready, let’s see how you can integrate your application with the API. In this example we will be using a certificate template to generate PDFs.

import requests
import json
url = "https://api.templated.io/v1/render"
payload = json.dumps({
"template": "template_id",
"format": "pdf",
"layers": {
"date": {
"text": "02/10/2024"
},
"name": {
"text": "John Doe"
},
"signature": {
"text": "Dr. Mark Brown"
},
"details": {
"text": "This certificate is awarded to John Doe in recognition of their successful completion of Computer Science Degree on 02/10/2024."
}
}
})
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ${YOUR_API_KEY}'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

and If we check response we have the following

{
"renderUrl":"PDF_URL",
"status":"success",
"template":"YOUR_TEMPLATE_ID"
}

In the code above, using Templated to convert HTML to PDF is pretty simple. No additional libraries need to be installed. You only need to make a single API call, providing your data as the request body. That’s all there is to it!

You can use the renderUrl from the response to download or distribute the generated PDF.

Conclusion

While xhtml2pdf provides a solid solution for Python developers to convert HTML to PDF, Templated offers a more comprehensive and user-friendly approach, especially for complex scenarios involving template management and advanced rendering.

PDF generation is now a standard part of many applications, and it shouldn’t be a source of stress for developers. We’ve explored how to use xhtml2pdf for straightforward PDF generation in Python. However, for more complex scenarios like template management, Templated offers a seamless solution through simple API calls.

To get started with Templated and streamline your PDF generation process, sign up for a free account and begin automating your PDFs today!

Generate images and PDFs with a simple API

Automate your marketing, social media visuals, banners, PDFs and more with our
 API and no-code integrations

Learn More
Back to Blog

Ready to start generating your images and PDFs?

Sign up to our free trial and try it for yourself

See our latest posts

View all posts »
How To Automate Job Posting Images for LinkedIn

How To Automate Job Posting Images for LinkedIn

Learn how to automate job posting images for LinkedIn using Templated, Google Sheets, and Zapier, saving time while keeping your visuals consistent.

4 Methods To Find the DPI of a PDF

4 Methods To Find the DPI of a PDF

This article will cover 4 best methods to identify the DPI of a PDF.

How To Automate Instagram Posts using Google Sheets & Make

How To Automate Instagram Posts using Google Sheets & Make

Learn how to automate your Instagram posts using Templated and Google Sheets, saving time and ensuring a steady flow of engaging content for your audience.

How to Generate Images from Google Sheets with Zapier

How to Generate Images from Google Sheets with Zapier

Learn how to automate image generation using data from Google Sheets and Templated's powerful API with Zapier. This step-by-step guide will help you create a no-code workflow to streamline your visual content creation process.