· 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.
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:
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:
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:
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:
- A drag-and-drop template editor
- Automatic PDF tracking
- Chromium-based rendering for accurate results
- 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:
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.
To start using Templated API, you need to get your API key that can be found on the API Integration tab on your 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.
and If we check response
we have the following
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!
Automate your content with Templated