In this article, you’ll discover 10 ways to generate PDFs in Python, focusing on open source libraries that cater to different needs.

You’ll gain insights into various libraries available for Python PDF generation, understanding their features and practical use cases. By exploring these options, you can select the most suitable library for your project requirements and effectively streamline the PDF generation process.
The importance of PDFs in various industries
PDFs have become an essential format across various industries due to their ability to preserve document formatting and ensure consistent viewing experiences. In business, they’re essential for contracts, reports, and invoices, while in education, they’re used for ebooks and research papers. The legal, healthcare, and government sectors also rely on PDFs for official documents, medical records, and tax forms due to their security features and universal compatibility.
Beyond business and government, PDFs are vital in e-commerce for product catalogs and receipts, in finance for financial statements, and in marketing for brochures and newsletters. They’re also commonly used for data reports, analytics, and public service announcements.
Keep reading to learn about the top 10 libraries for generating PDFs in Python.
1. Generating PDFs with FPDF
FPDF is a lightweight PDF generation library for Python that allows you to create PDFs from scratch. It’s particularly well-suited for generating simple PDFs with text, images, and basic formatting. FPDF is easy to use and doesn’t have any external dependencies.
Key features
-
Text and image insertion — Easily add text, images, and lines to your PDF.
-
Multipage PDFs— Create multipage PDFs with simple commands.
-
Formatting options — Customize fonts, colors, and text alignment.
-
Lightweight and fast — FPDF is efficient and requires no external dependencies.
Installation
Install FPDF using pip:
pip install fpdf
Usage example
Here’s a simple example of how to create a PDF with FPDF:
from fpdf import FPDF # Create instance of an FPDF class. pdf = FPDF() # Add a page. pdf.add_page() # Set the font. pdf.set_font("Arial", size=12) # Add a cell. pdf.cell(200, 10, txt="Hello, this is a PDF generated using FPDF!", ln=True, align='C') # Save the PDF with the name `.pdf`. pdf.output("output.pdf") print("PDF generated successfully!")
In this example, you created a PDF document, added a single page, and inserted a line of centered text. The PDF was then saved to a file named output.pdf
. FPDF provides a simple and effective way to generate PDFs with basic content and formatting.
Advanced example: Generating a multipage PDF with images and custom formatting
In this example, you’ll create a Python script to generate a multipage PDF that includes both text and images. You’ll use the fpdf
library to:
-
Add multiple pages to the PDF.
-
Insert custom text on each page.
-
Include an image (e.g.
example.jpg
) on each page.
Ensure you have an image file (e.g. example.jpg
) you want to include in the PDF. Place this image in the same directory as your script.
Save the following Python code in a .py
file (e.g. generate_pdf.py
):
from fpdf import FPDF # Create an instance of am FPDF class. pdf = FPDF() # Add multiple pages. for i in range(3): pdf.add_page() # Add a new page. pdf.set_font("Arial", size=16) # Set the font for text. pdf.cell(200, 10, txt=f"Page {i+1}", ln=True, align='C') # Add text to the page. pdf.image("example.jpg", x=10, y=30, w=100) # Add an image to the page. # Output the PDF to a file. pdf.output("multi_page.pdf") print("Multipage PDF with images generated successfully!")
Open your terminal, navigate to the folder where the script and image are stored, and run:
python generate_pdf.py
This will generate a multi_page.pdf
file in the same directory. Each page will have custom text and the example.jpg
image placed at specified coordinates.
Use cases: Generating PDFs with FPDF
-
Simple PDF creation — Quickly generate PDFs with text, images, and basic formatting for reports or invoices.
-
Multipage documents — Easily create multipage PDFs with custom text and images.
-
Custom formatting — Design basic PDFs with different fonts, colors, and text alignment.
-
Image insertion — Add images to PDFs for visual content such as flyers or brochures.
-
No dependencies — Ideal for projects that require lightweight PDF generation without additional dependencies.
Why choose FPDF?
-
Lightweight and fast — Ideal for generating simple PDFs without external dependencies, making it great for small projects.
-
Ease of use — Its straightforward API allows for rapid development and quick results with minimal setup.
-
Flexibility — Even though it’s lightweight, it still offers essential features like text, images, and page management, making it a solid choice for basic PDF generation needs.
2. Generating PDFs with ReportLab
ReportLab is an open source Python library designed for creating and manipulating PDF documents. It’s known for its robustness and flexibility, making it a powerful tool for programmatically generating complex documents and reports.
Key features
-
PDF generation — Easily create PDFs with text, images, charts, and custom graphics. ReportLab provides all the tools necessary to construct a wide variety of document types.
-
Advanced graphics — With ReportLab, you can add complex graphical elements to your documents, such as lines, shapes, curves, and even custom-designed illustrations.
-
Document templates — It supports the use of templates, making it easier to create documents with a consistent layout and formatting.
-
Custom fonts and styles — ReportLab provides fine-tuned control over the appearance of your PDF documents, including support for custom fonts, colors, and styles.
Licensing
ReportLab offers a community edition under the ReportLab Open Source License, which is free and suitable for most non-commercial uses. For commercial applications and additional features, ReportLab also provides a commercial edition with extended capabilities and support.
Installation
To install ReportLab, use pip:
pip install reportlab
Alternatively, if you’re using a system like Anaconda
, you can install it via conda
:
conda install -c conda-forge reportlab
Usage example
Here’s a basic example of how to generate a PDF using ReportLab:
from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas # Create a PDF file. c = canvas.Canvas("example.pdf", pagesize=letter) # Draw some text. c.drawString(100, 750, "Hello, this is a PDF generated with ReportLab!") # Save the PDF. c.save()
In this example, a PDF file named example.pdf
is created with a simple line of text. ReportLab’s capabilities extend far beyond this example, allowing for the creation of complex and professional documents.
More advanced features
ReportLab offers more advanced features, outlined below.
Adding graphics
You can draw more than just text — for example, lines, shapes, and even custom images:
from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas c = canvas.Canvas("example_graphics.pdf", pagesize=letter) # Draw a line. c.line(50, 700, 550, 700) # Draw a rectangle. c.rect(50, 600, 200, 100) # Draw an image. c.drawImage("example_image.jpg", 300, 500, width=100, height=100) c.save()
Creating charts
You can also generate various types of charts using ReportLab’s rlbcharts
module. It allows you to create bar charts, line charts, and pie charts with extensive customization.
Use cases for ReportLab
ReportLab is particularly well-suited for situations that require:
-
Complex layouts — When you need to generate PDF documents with custom layouts, such as detailed reports, invoices, or brochures.
-
Advanced graphics and charts — If your documents need to include dynamic charts, graphs, or other complex visualizations.
-
Template-based documents — If you need to automate the generation of documents with a consistent design, such as invoice generation, certificates, or automated reports.
-
Professional print quality — ReportLab is ideal for creating documents intended for professional printing or distribution, where high-quality graphics and typography are required.
Why choose ReportLab?
-
Advanced features — ReportLab stands out for its ability to generate complex layouts, including advanced charts, graphs, and custom graphics.
-
High customization — It provides detailed control over the appearance of your documents, making it perfect for professional-grade reports and invoices.
-
Template support — Its template feature allows for efficient automation of repeated document formats, saving you time on large projects.
If you’re looking to generate detailed reports with charts, graphs, and complex page layouts, ReportLab is a fantastic choice. However, for simpler PDFs or documents that don’t require advanced graphical elements, other libraries like FPDF
or Pillow
might be more lightweight and suitable alternatives.
3. Generating PDFs with PDFKit
PDFKit is a Python library that simplifies the process of converting HTML to PDF using the wkhtmltopdf
command-line tool. It allows you to generate high-quality PDFs from HTML content, making it suitable for scenarios where you need to create PDFs from web-based content or styled documents.
Key features
-
HTML-to-PDF conversion — Convert HTML and CSS content into well-formatted PDF documents.
-
Support for complex layouts — Handle complex HTML structures and CSS styles effectively.
-
Easy integration — Simple to integrate into Python applications for generating PDFs.
Installation
To use PDFKit, install both the pdfkit
library and the wkhtmltopdf
tool. Follow the steps below to set it up.
-
Install the PDFKit library:
pip install pdfkit
-
Download and install
wkhtmltopdf
from its official website, or use a package manager.
-
For macOS (Homebrew):
brew install wkhtmltopdf
-
For Ubuntu/Debian:
sudo apt-get install wkhtmltopdf
-
For Windows:
Download the installer from the official website and follow the installation instructions.
Converting HTML to PDF
Here’s a simple guide for converting an HTML file into a PDF:
import pdfkit # Specify the path to your HTML file. html_file = 'example.html' # Define the output PDF file name. output_pdf = 'output.pdf' # Convert HTML to PDF. pdfkit.from_file(html_file, output_pdf)
Use cases
-
Web content conversion — Perfect for converting webpages into PDF documents.
-
Report generation — Create detailed and styled reports in PDF format.
-
ebooks and documentation — Generate well-formatted ebooks and documentation from HTML content.
Why choose PDFKit?
-
HTML-to-PDF conversion — Perfect for developers needing to convert HTML content into high-quality, styled PDFs.
-
Simplicity — The simple API makes it quick to integrate into Python projects, ideal for converting web-based content without a complicated setup.
-
Web-based content — It’s highly effective for generating PDF versions of existing webpages, making it useful for content like reports or ebooks.
To learn more about converting HTML to PDF using Python, check out our blog post:
4. Generating PDFs with WeasyPrint
WeasyPrint is a Python library that converts HTML and CSS into high-quality PDF documents. It leverages modern web standards to ensure the resulting PDFs maintain the same layout and styling as the HTML source, making it a great tool for generating print-ready documents.
Key features
-
HTML- and CSS-to-PDF conversion — Supports complex HTML and CSS, including modern layout techniques.
-
Advanced styling — Handles stylesheets and responsive design effectively.
-
Unicode and multilingual support — Includes comprehensive support for different languages and character sets.
-
SVG support — Allows embedding and rendering of SVG graphics in PDFs.
Installation
To use WeasyPrint, install the weasyprint
library, along with its dependencies.
Here’s how to install it:
pip install weasyprint
WeasyPrint also requires some additional system dependencies for full functionality. On macOS, Ubuntu/Debian, or Windows, ensure you have the necessary packages installed. For Ubuntu/Debian, you might need:
sudo apt-get install libffi-dev libpq-dev
Usage example
To illustrate the capabilities of WeasyPrint, consider the following example that generates a styled PDF document:
from weasyprint import HTML # Define HTML content. html_content = ''' <!DOCTYPE html> <html> <head> <title>Sample PDF</title> <style> body { font-family: Arial, sans-serif; } h1 { color: #333; } </style> </head> <body> <h1>Hello, this is a PDF generated using WeasyPrint!</h1> <p>This PDF is created from HTML content with CSS styling.</p> </body> </html> ''' # Convert HTML to PDF. HTML(string=html_content).write_pdf("output.pdf") print("PDF generated successfully!")
In this example, HTML(string=html_content).write_pdf("output.pdf")
converts the provided HTML content into a PDF file named output.pdf
.
Use cases
-
Web content conversion — Perfect for converting webpages into PDF documents.
-
Report generation — Create detailed and styled reports in PDF format.
-
Invoices and receipts — Generate professional-looking invoices and receipts from HTML templates.
-
ebooks and documentation — Produce well-formatted ebooks and documentation from HTML content.
WeasyPrint’s ability to accurately render HTML and CSS makes it an excellent choice for developers looking to generate PDFs that need to closely match the original web content.
Why choose WeasyPrint?
-
Web standards support — It excels at handling complex HTML and CSS layouts, ensuring your PDFs closely match the original web content.
-
Advanced styling and multilingual support — With full support for modern CSS, responsive design, and different character sets, it’s great for creating print-ready documents.
-
Easy integration — It seamlessly integrates with Python, providing an accessible and robust solution for generating PDFs from web-based content.
To learn more about WeasyPrint, visit our How to generate PDF from HTML using Python blog post.
5. Generating PDFs with Playwright for Python
Playwright for Python is a modern automation library that supports multiple browsers, including Chromium, Firefox, and WebKit. It allows you to perform end-to-end testing and automation of web applications, and it can also be used to generate PDFs from webpages.
Key features
-
Cross-browser support — Works with Chromium, Firefox, and WebKit for consistent results across different browsers.
-
PDF generation — Render webpages to PDF with precise control over layout and styling.
-
Headless mode — Operate in headless mode for faster and more efficient rendering.
-
Rich APIs — Provides extensive APIs for interacting with webpages, including taking screenshots and generating PDFs.
Installation
To use Playwright for Python, install the playwright
library and its necessary dependencies.
-
Install Playwright:
pip install playwright
Usage example
Here’s a simple example of how to use Playwright to generate a PDF from a webpage:
from playwright.sync_api import sync_playwright # Initialize Playwright. with sync_playwright() as p: # Launch a browser. browser = p.chromium.launch() # or p.firefox.launch() or p.webkit.launch() page = browser.new_page() # Navigate to a webpage. page.goto('https://github.com/microsoft/playwright-python') # Generate PDF from the webpage. page.pdf(path='output.pdf', format='A4') # Close the browser. browser.close() print("PDF generated successfully!")
In this example, Playwright navigates to https://github.com/microsoft/playwright-python, generates a PDF of the page, and saves it as output.pdf
.
Use cases: Generating PDFs with Playwright for Python
-
Webpage to PDF — Convert any webpage to PDF, preserving layout and styling.
-
Automated reports — Automate the generation of reports directly from web applications, ideal for saving and sharing analysis.
-
Marketing materials — Generate PDFs for flyers, brochures, and newsletters from web-based designs.
-
Page snapshotting — Archive webpages as PDFs to preserve their appearance at a specific time.
-
Dynamic content — Capture PDFs with the latest data from dynamic pages (e.g. filling out forms or displaying reports).
Why choose Playwright for Python?
-
Browser automation — It allows you to simulate a real browser environment, perfect for generating PDFs from webpages as they’d appear in an actual browser.
-
Multi-browser support — Unlike some other libraries, Playwright works across multiple browsers (Chromium, Firefox, and WebKit), offering flexibility in testing and generating PDFs.
-
End-to-end testing — In addition to PDF generation, Playwright is a great tool for testing and automating web applications, giving it versatility for developers working with dynamic content.
6. Generating PDFs with img2pdf
img2pdf is a lightweight Python library that converts images into PDF documents. It’s ideal for scenarios where you need to create a PDF from one or more image files, such as scanned documents, photographs, or graphics.
Key features
-
Image-to-PDF conversion — Easily converts various image formats (JPEG, PNG, TIFF, and more) into PDF documents, supporting most common image file types.
-
Multiple images — You can combine multiple images into one single PDF, ensuring all images are included in the same document without any hassle.
-
Lossless compression — Maintains the original quality of the images, ensuring no degradation or quality loss during the conversion process.
-
Efficient and lightweight — This library is designed to be small and fast, making it ideal for high-performance applications.
-
Simple API — The API is designed to be straightforward, so you can start generating PDFs with minimal setup.
Installation
You can install img2pdf with pip by running the following command:
pip install img2pdf
This command will install the latest version of the library from the Python Package Index (PyPI).
Usage example
Here’s a basic example of how to use img2pdf to convert images to a PDF:
import img2pdf # List of image file paths. image_files = ['image1.jpg', 'image2.png', 'image3.tiff'] # Convert images to PDF. with open('output.pdf', 'wb') as f: f.write(img2pdf.convert(image_files)) print("PDF generated successfully!")
In this example, img2pdf.convert()
takes a list of image file paths and writes them into a PDF file named output.pdf
.
Use cases
-
Scanned documents — Converting scanned paper documents (often stored as images) into PDFs for easy sharing and archiving.
-
Photography portfolios — Combining photographs into a PDF for creating portfolios or print-ready albums.
-
Image-only PDF reports — Generating PDF reports where images need to be included without extra text or formatting.
Why choose img2pdf
img2pdf is an excellent choice when you need to:
-
Convert multiple images to one PDF — When you have a collection of images (e.g. scanned documents, photographs, graphics) and want to compile them into a single, printable PDF.
-
Maintain image quality — If retaining the original quality of the images is crucial (for example, in the case of scanned pages or professional photography), img2pdf offers lossless compression, making sure the quality of your images stays intact.
-
Handle large image collections — If you need to quickly batch convert many images into a PDF, img2pdf’s simple API and efficient performance make it an ideal solution.
In short, img2pdf excels at converting a collection of images into one unified PDF document while preserving image quality, making it a fantastic tool for various use cases that involve working with image files and PDFs.
7. Using Pillow with img2pdf
Pillow is a popular Python library for image processing, offering features like resizing, cropping, and filtering images. When combined with img2pdf, you can preprocess images before converting them into a PDF, giving you more control over a document’s final appearance.
Key features
-
Image manipulation — Resize, crop, rotate, or apply filters to images before conversion.
-
Format conversion— Convert images to different formats (e.g. JPEG, PNG) as needed before creating a PDF.
-
Seamless integration— Use Pillow to preprocess images and then pass them directly to img2pdf for PDF generation.
Installation
To use Pillow with img2pdf, install both libraries using pip:
pip install Pillow img2pdf
Code examples
The following examples show how to preprocess images and convert them into PDFs.
Example 1: Basic image preprocessing and PDF conversion
Here’s an example of how to use Pillow and img2pdf together to preprocess images and convert them into a PDF:
from PIL import Image import img2pdf # Open an image using Pillow. image = Image.open('input.jpg') # Resize the image (optional). image = image.resize((800, 600)) # Convert the image to another format if needed (optional). image = image.convert('RGB') # Save the modified image temporarily. image.save('modified_image.jpg') # Convert the modified image to PDF. with open('output.pdf', 'wb') as f: f.write(img2pdf.convert('modified_image.jpg')) print("PDF generated successfully!")
In this example:
-
Pillow is used to open an image and apply some basic processing, such as resizing and format conversion.
-
The processed image is then saved temporarily before being passed to img2pdf for PDF conversion.
Example 2: Combining multiple preprocessed images into a single PDF
Here’s an example of how to preprocess multiple images and combine them into a single PDF document:
from PIL import Image import img2pdf # List of image file paths. image_files = ['image1.jpg', 'image2.png', 'image3.tiff'] # Preprocess images. processed_images = [] for image_file in image_files: image = Image.open(image_file) image = image.resize((800, 600)) # Resize image (optional). image = image.convert('RGB') # Convert format (optional). processed_image_path = f'processed_{image_file}' image.save(processed_image_path) processed_images.append(processed_image_path) # Convert preprocessed images to PDF. with open('output.pdf', 'wb') as f: f.write(img2pdf.convert(processed_images)) print("PDF generated successfully!")
In the code example, the paths to the image files are specified first. Using Pillow, each image is resized and converted, then saved temporarily. These preprocessed images are then combined into a single PDF using img2pdf
. This process ensures the images are uniform in format and dimensions before creating the final PDF document.
Use cases
-
Document standardization — Preprocess and standardize the size and format of images before creating a PDF.
-
Enhanced visual quality — Apply filters or adjustments to images to improve visual quality before conversion.
-
Consistent layouts — Ensure all images in the PDF have a consistent appearance and layout.
-
Archiving and compilation — Create high-quality, standardized PDFs from a collection of images for archiving or distribution.
Why use Pillow with img2pdf?
-
Flexibility — Preprocess images to achieve the desired look and feel before converting them to PDF.
-
Quality control — Maintain control over image quality and format during the conversion process.
-
Efficiency — Seamlessly integrate image preprocessing and PDF generation in a single workflow.
Using Pillow with img2pdf provides a powerful combination for generating high-quality PDFs from images, giving you full control over image preprocessing and the final document’s appearance.
8. Generating PDFs with xhtml2pdf
xhtml2pdf is a Python library that simplifies the process of converting HTML and CSS documents into PDFs. It allows you to leverage web technologies to design and style your documents, which can then be converted into high-quality PDF files. This makes it a great choice for applications where you need to dynamically generate PDFs from HTML content, such as web-based reports, invoices, or brochures.
Key features
-
HTML-to-PDF conversion — Converts HTML documents into PDFs while preserving the layout and styles defined in the CSS.
-
Support for complex layouts — Handles multipage documents, headers, footers, and various CSS properties to create professional-looking PDFs.
-
Embedded fonts and images — Supports embedding fonts and images in the PDF, ensuring the final document matches the original design.
Installation
To use xhtml2pdf, you can install it via pip:
pip install xhtml2pdf
Usage example
Here’s a simple example of how to convert an HTML file to a PDF using xhtml2pdf:
from xhtml2pdf import pisa # Define a function to convert HTML to PDF. def convert_html_to_pdf(source_html, output_filename): # Open output file for writing (binary mode). with open(output_filename, "wb") as output_file: # Convert HTML to PDF. pisa_status = pisa.CreatePDF(source_html, dest=output_file) # Return `true` if the conversion was successful. return pisa_status.err == 0 # HTML content to be converted. html_content = """ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Sample PDF</title> <style> h1 { color: #2E86C1; } p { font-size: 14px; } </style> </head> <body> <h1>Hello, PDF!</h1> <p>This is a PDF generated from HTML using xhtml2pdf.</p> </body> </html> """ # Convert HTML to PDF. if convert_html_to_pdf(html_content, "output.pdf"): print("PDF generated successfully!") else: print("PDF generation failed!")
In this example, xhtml2pdf is used to convert a simple HTML string into a PDF file named output.pdf
. The library handles the HTML structure and CSS styling, allowing you to produce a well-formatted PDF.
Use cases
-
Web-based reports — Generate PDF reports dynamically from HTML templates.
-
Invoices and receipts — Create printable invoices and receipts with embedded styles and images.
-
Brochures and flyers— Design and produce professional-looking brochures and flyers using HTML and CSS.
-
Documentation — Convert web-based documentation into well-formatted PDFs for offline use.
Why choose xhtml2pdf?
-
Ease of use — Leverage your existing knowledge of HTML and CSS to create PDFs.
-
Flexibility — Supports complex layouts, multiple pages, headers, footers, and more.
-
Quality — Ensures the PDF output closely matches the original HTML design, with support for embedded fonts and images.
xhtml2pdf is a powerful and versatile tool for generating PDFs from HTML and CSS, making it a great choice for developers looking to create well-formatted, professional-quality PDFs from web content.
9. Generating PDFs with pdfdocument
pdfdocument is a straightforward Python library designed for generating PDFs with minimal setup and complexity. It offers a simple API to create PDF files, making it ideal for cases where you need to generate PDFs quickly without dealing with the intricacies of more complex libraries like ReportLab or WeasyPrint.
Key features
-
Easy to use — The API is designed to be intuitive, enabling you to create PDFs with just a few lines of code.
-
Basic PDF content — Supports adding text, images, and basic formatting to your PDFs.
-
Lightweight — The library is lightweight, making it a good choice for simple use cases where you don’t need advanced features.
Installation
You can install pdfdocument via pip:
pip install pdfdocument
Usage example
Here’s a simple example of how to use pdfdocument to generate a PDF:
from pdfdocument.document import PDFDocument # Create a PDF document. pdf = PDFDocument("output.pdf") # Start the PDF. pdf.init_report() # Add a title and some text. pdf.h1("Hello, PDFDocument!") pdf.p("This is a PDF generated using the pdfdocument library.") # Add an image (optional). # pdf.image("path_to_image.jpg", width=200) # Finalize and save the PDF. pdf.generate() print("PDF generated successfully!")
In this example, pdfdocument is used to create a PDF file named output.pdf
. The code adds a title and a paragraph of text, demonstrating how easily you can generate a basic PDF. The generate()
method finalizes and saves the document.
Advanced usage
-
Simple reports — The
init_report()
method initializes the PDF as a report, but you can customize this to suit different document types. -
Adding images — You can easily insert images into the PDF, making it suitable for generating simple visual reports or documents with logos.
-
Customization — While pdfdocument is simple, you can still apply basic formatting, such as different heading levels and paragraphs.
Why choose pdfdocument?
-
Ease of use — The intuitive API allows for quick PDF generation with minimal code.
-
Simplicity — Ideal for simple use cases where you don’t need advanced features.
-
Lightweight — A lightweight solution for generating PDFs without the overhead of more complex libraries.
pdfdocument is a great tool for developers looking to generate PDFs quickly and easily, without needing to delve into more complex libraries. It’s particularly suitable for simple reports, letters, memos, and other basic documents.
10. Generating PDFs with Nutrient API
Nutrient DWS API is a robust and feature-rich API designed for working with PDFs. It offers comprehensive capabilities for creating, editing, annotating, and processing PDF documents. Although Nutrient is a commercial product, its powerful API makes it a valuable tool for developers who need advanced PDF functionalities beyond basic generation.
Key features
-
High-quality PDF rendering — Generates PDFs that are accurate and visually consistent across platforms.
-
Advanced PDF features — Supports complex PDF operations, including form filling, digital signatures, annotations, and more.
-
REST API — The API is platform-agnostic, enabling you to interact with it from any environment, including Python, via HTTP requests.
-
Customization — Offers extensive options for customizing PDF output, including fonts, layouts, and security settings.
Installation
To use Nutrient DWS API, you need to sign up for an API key and use the provided endpoints. There’s no need for a specific Python package, as you can interact with the API using HTTP requests, typically via libraries like requests
.
Example code
Here’s an example of how to generate a PDF using Nutrient’s PDF Generator API in Python:
import requests import json # Define the HTML part of the document. instructions = { 'parts': [ { 'html': 'index.html' } ] } # Send the request to the Nutrient API. response = requests.request( 'POST', 'https://api.nutrient.io/build', headers={ 'Authorization': 'Bearer {YOUR_API_KEY}' # Replace with your API key. }, files={ 'index.html': open('index.html', 'rb'), }, data={ 'instructions': json.dumps(instructions) }, stream=True ) # Save the resulting PDF. if response.ok: with open('result.pdf', 'wb') as fd: for chunk in response.iter_content(chunk_size=8096): fd.write(chunk) else: print(response.text) exit()
To generate a PDF, the instructions dictionary specifies that the PDF should be generated from the index.html
file. The code then sends a POST
request to the Nutrient API with the HTML content. The API processes this data and returns a PDF file. If the request is successful, the PDF is saved as result.pdf
. If there’s an error, the response is printed for troubleshooting.
Getting started
-
Sign up — Visit the Nutrient website and sign up for an account.
-
Request an API key — After signing up, obtain an API key from the dashboard.
-
Pricing — Nutrient offers flexible pricing based on your needs. You can start with a trial period and then choose a plan that fits your requirements.
When to use Nutrient DWS API
Nutrient DWS API is ideal for developers needing a robust, scalable solution for PDF generation, particularly when working with complex documents or requiring extensive customization.
Advanced usage
-
Dynamic PDF content — Combine the API with dynamic data sources to generate customized PDFs on the fly, such as invoices, reports, or certificates.
-
Integration with other services — Use the API in conjunction with other Nutrient services, such as annotation or form filling, to create comprehensive PDF solutions.
-
Scalability — The API is designed to handle large-scale PDF generation tasks, making it suitable for enterprise-level applications.
Nutrient DWS API is a powerful tool for generating and manipulating PDFs, offering extensive customization options and advanced features to meet the needs of developers working on complex projects.
Comparison of PDF generation libraries
Library | Ease of use | Functionality | Performance | Community support |
---|---|---|---|---|
Nutrient API | Moderate | High | High | Moderate |
xhtml2pdf | Easy | Moderate | Moderate | High |
Pillow with img2pdf | Easy | Moderate | High | High |
img2pdf | Easy | Low | High | Low |
WeasyPrint | Moderate | High | Moderate | High |
PDFKit | Moderate | High | Moderate | High |
ReportLab | Moderate | High | Moderate | High |
FPDF | Easy | Low | High | Moderate |
Playwright for Python | Moderate | High | Moderate | High |
pdfdocument | Easy | Low | High | Low |
Key highlights
-
Nutrient API — Great for advanced features (form filling, digital signatures) with high performance but moderate community support.
-
xhtml2pdf — Easy to use for simple HTML-to-PDF tasks with high community support.
-
Pillow with img2pdf — Efficient for image handling and PDF creation, supported by a strong community.
-
img2pdf — A straightforward tool for image-to-PDF conversion with high performance but limited functionality.
-
WeasyPrint and PDFKit — Excellent for complex HTML/CSS rendering, with good performance and high community support.
-
ReportLab — Ideal for advanced PDF customization with high functionality but moderate ease of use.
-
FPDF and pdfdocument — Simple for basic PDFs but lacking advanced features, with moderate community support.
-
Playwright for Python — Strong for web-based PDF generation but requires browser automation knowledge.
For simple PDFs, go with img2pdf or FPDF. For advanced features and HTML/CSS support, WeasyPrint, PDFKit, or Nutrient API are ideal. Meanwhile, ReportLab excels at customization for complex tasks.
Common issues while generating PDFs in Python
1. Handling large documents
-
Issue:
Generating large PDFs with many pages, images, or complex content can lead to high memory consumption and slow processing times.
-
Solutions and best practices:
-
Incremental PDF writing — Instead of generating an entire document in memory, write pages to disk incrementally using libraries like
reportlab
with streaming options. -
Optimize image sizes — Compress and resize images before adding them to the PDF to reduce memory usage.
-
Chunk processing — Divide content generation into smaller chunks and process them sequentially to avoid memory overload.
-
Example:
pdf = FPDF() for _ in range(1000): # Instead of loading all pages at once, process incrementally. pdf.add_page() pdf.cell(200, 10, txt="Chunk processing example", ln=True) pdf.output("large_output.pdf")
This example generates 1,000 pages, processing each one incrementally without loading all pages at once, improving memory usage.
2. Managing memory usage
-
Issue:
Memory leaks or excessive memory consumption when processing multiple PDF files or handling large datasets.
-
Solutions and best practices:
-
Use generators — Leverage Python generators to process data lazily instead of loading everything into memory at once.
-
Garbage collection — Explicitly clear unused objects using
gc.collect()
after processing large chunks of data. -
Use streaming APIs — Libraries like
pdfkit
support streaming outputs instead of storing content in memory.
-
Example:
import gc from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.cell(200, 10, "Memory management example", ln=True) pdf.output("output.pdf") # Clear memory del pdf gc.collect()
This example creates a PDF, and then it explicitly clears the memory by deleting the object and invoking garbage collection to optimize memory usage.
3. Ensuring cross-platform compatibility
-
Issue:
PDF output may look different on various operating systems due to font availability or encoding issues.
-
Solutions and best practices:
-
Embed fonts — Use built-in font embedding features in libraries like
FPDF
orreportlab
to ensure consistency across platforms. -
Use standard fonts — Stick to common fonts like Helvetica, Times, and Courier to avoid OS-specific font dependencies.
-
Encoding handling — Always specify text encoding (e.g. UTF-8) to avoid compatibility issues when generating multilingual PDFs.
-
Example:
pdf.set_font("Arial", size=12, style='B') # Use cross-platform fonts. pdf.set_auto_page_break(auto=True, margin=15)
This example uses a built-in font (Arial) and sets page breaks, ensuring compatibility across different systems by using consistent font handling.
4. Optimizing performance
-
Issue:
PDF generation can be slow, especially with large datasets, high-resolution images, or complex formatting.
-
Solutions and best practices:
-
Minimize draw calls — Reduce the number of drawing operations by batching similar elements together.
-
Use cached resources — Cache repeated elements (e.g. logos, headers) to avoid redundant processing.
-
Asynchronous processing — Use asynchronous processing for high-performance document generation in web applications.
-
Example:
from concurrent.futures import ThreadPoolExecutor def generate_pdf_chunk(data): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.cell(200, 10, txt=data, ln=True) pdf.output(f"chunk_{data}.pdf") with ThreadPoolExecutor() as executor: executor.map(generate_pdf_chunk, ["Page 1", "Page 2", "Page 3"])
This example demonstrates parallel PDF generation with a thread pool, allowing chunks of data to be processed simultaneously, improving overall speed.
5. Maintaining layout consistency
-
Issue:
Inconsistent layout issues arise when adding dynamic content such as tables, charts, or paragraphs.
-
Solutions and best practices:
-
Define layout templates — Use a consistent document template to standardize layout across all generated PDFs.
-
Auto-adjust layout — Use libraries that support automatic content fitting and page breaks, such as
reportlab
. -
Test different screen sizes — Check the final PDF layout on different devices to ensure consistency.
-
Example:
pdf.set_auto_page_break(auto=True, margin=10) pdf.multi_cell(0, 10, "This is a long paragraph that will wrap automatically.")
This example uses multi_cell
to handle long paragraphs automatically, ensuring text wraps properly without breaking the layout.
By following these solutions and best practices, developers can efficiently generate PDFs in Python while overcoming common challenges related to performance, compatibility, and security.
Conclusion
This article covered 10 methods for generating PDFs in Python — from simple tools like FPDF and img2pdf, to advanced options like ReportLab, WeasyPrint, and Nutrient.
Each library has features suited to different use cases, such as creating basic documents or complex PDFs with custom styling. Incorporating these strategies into your Python projects elevates your application’s functionality.
While Nutrient DWS API comes at a higher price point than open source libraries, its capabilities may justify the investment for businesses needing robust features not typically available in free alternatives. Start your free trial today and get 100 API credits to explore its full potential.
FAQ
Below are some frequently asked questions about generating PDFs in Python.