Generating PDF in Python

My favorite lib is pdf-reports

Page content

Generating PDF Reports Using Python

Python, with its extensive libraries and modules, offers powerful tools for generating professional PDF reports.

This guide explores various methods to create PDF documents using Python, covering popular libraries like ReportLab, FPDF, Xhtml2pdf, WeasyPrint, Jinja2, pdf-reports and PdfKit.

python on the report

Why Generate PDF Reports with Python?

  • Automation: Python scripts can automate report generation, saving time and reducing human error.
  • Customization: Tailor reports to specific needs by adding text, images, tables, and graphs.
  • Consistency: Ensure branding consistency across reports with standardized templates.
  • Versatility: Generate invoices, business reports, data summaries, or any other document type.

ReportLab

ReportLab is a versatile library that allows you to create PDF documents programmatically. It supports adding text, images, tables, and graphics, making it suitable for various report types.

Pros:

  • Rich feature set
  • High-level API for easy use
  • Supports advanced features like annotations and interactive forms

Cons:

  • Steeper learning curve compared to other libraries
  • Less intuitive API for some tasks

Using ReportLab

Installation: Install ReportLab using pip:

pip install reportlab

Basic Setup:

from reportlab.pdfgen import canvas
c = canvas.Canvas('hello.pdf')
# Adding Text:
python c.drawString(100, 800, "Hello World")
# Saving the PDF:
python c.save()

FPDF

FPDF is a simple yet powerful PDF creation library written in pure Python. It’s ideal for generating basic PDF documents with minimal code.

Pros:

  • Easy to learn and use
  • Lightweight and fast
  • Supports Unicode characters

Cons:

  • Limited feature set compared to ReportLab
  • Less control over element placement on the page

Using FPDF

Installation: Install FPDF using pip:

pip install fpdf2
Basic Setup:
from fpdf import FPDF pdf = FPDF()

# Adding Text:
python pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, 'Hello World')
# Saving the PDF:
python pdf.output('hello.pdf', 'F')

Xhtml2pdf

Xhtml2pdf is a CSS/HTML to PDF converter that uses ReportLab under the hood. It’s perfect for generating PDFs from HTML templates, making it an excellent choice for web-to-PDF conversions.

Pros:

  • Converts HTML and CSS to PDF
  • Supports complex layouts and styling
  • Integrates well with web frameworks like Django

Cons:

  • Requires knowledge of HTML and CSS
  • May not be suitable for simple, non-web-based reports

Using Xhtml2pdf

Installation: Install Xhtml2pdf using pip:

pip install xhtml2pdf

Generating a PDF from HTML: python

from xhtml2pdf import pisa
html = "<h1>Hello World</h1>"
with open('hello.pdf', 'wb') as output_file:
  pisa_status = pisa.CreatePDF(html, dest=output_file)

WeasyPrint

WeasyPrint is a versatile Python library that allows developers to convert HTML and CSS into PDF documents with high-quality rendering. It’s particularly useful for generating reports, invoices, or any other document that requires precise formatting. Below are several examples demonstrating how to use WeasyPrint in various scenarios.

Features

  • Converts HTML and CSS to PDF documents.
  • Handles dynamic data rendering with the help of templating engines like Jinja2.

Installation:

pip install weasyprint

Here is a simple example of generating a PDF from an HTML string:

from weasyprint import HTML

html_string = """
<html>
<head><title>Sample PDF</title></head>
<body>
<h1>Hello, WeasyPrint!</h1>
<p>This is a sample PDF generated using WeasyPrint.</p>
</body>
</html>
"""

HTML(string=html_string).write_pdf("sample.pdf")

In this example, the HTML class from WeasyPrint is used to convert an HTML string into a PDF file named “sample.pdf”.

Loading HTML from Files

You can also load HTML content directly from files:

from weasyprint import HTML

# Load HTML from a file
HTML('sample.html').write_pdf('output.pdf')

This method is useful when you have your HTML content stored in an external file. Using CSS Stylesheets

WeasyPrint supports the use of CSS stylesheets to style your PDF documents. Here’s how you can apply a CSS file:

from weasyprint import HTML

# Load HTML and CSS from files
HTML('sample.html').write_pdf('output.pdf', stylesheets=['sample.css'])

This allows for more complex styling, making it easier to create visually appealing PDFs .

Jinja2

Jinja2 is a powerful templating engine for Python that allows you to generate dynamic content by embedding Python expressions, conditionals, and loops within templates. This guide will walk you through various examples of using Jinja2 in Python, from basic variable substitution to more advanced features like template inheritance.

Jinja2 key features

  • A templating engine that can be used in conjunction with WeasyPrint or ReportLab for dynamically generating HTML content.
  • Ideal for embedding data into predefined templates.
  • Installation:
pip install jinja2

see examples on official jinja2 documentation page: https://jinja.palletsprojects.com/en/3.0.x/

pdfkit

pdfkit is a powerful Python library that simplifies the process of converting HTML content into PDF documents. It acts as a wrapper for the wkhtmltopdf utility, leveraging WebKit to render HTML pages accurately in PDF format. This guide will walk you through various examples and use cases of generating PDFs using pdfkit.

Key features

Installation:

# and ensure wkhtmltopdf is installed.

Basic Usage - Converting HTML String to PDF

You can convert a simple HTML string into a PDF file using pdfkit.from_string():

import pdfkit

html_sample = """
<html>
<head><title>Sample PDF</title></head>
<body>
<h1>This is a heading</h1>
<p>First line.</p>
<p>Second line.</p>
<p>Third line.</p>
<p>Fourth line.</p>
</body>
</html>
"""

pdfkit.from_string(html_sample, 'output.pdf')

This will generate a PDF file named output.pdf in the current directory.

Converting Local HTML File to PDF

If you have an HTML file stored locally, use pdfkit.from_file():

import pdfkit

pdfkit.from_file('local.html', 'sample.pdf')

PDF Reports Library (pdf-reports)

Creating PDF reports using the pdf-reports library in Python can be a powerful way to generate professional-looking documents from HTML or Pug templates. This library leverages modern components via the Semantic UI framework and provides routines for embedding tables, plots, and other elements into your PDFs.

The pdf-reports library is designed to create visually appealing PDF reports from HTML or Pug templates. It supports Python 3.x officially but can also run on Python 2.x with the appropriate version of weasyprint. This makes it a versatile tool for generating dynamic and well-structured documents programmatically.

Key Features

  • Modern Components: Utilizes Semantic UI framework for modern-looking components.
  • Embedding Elements: Allows embedding tables, plots, and other elements into the PDFs.
  • Template Support: Supports HTML and Pug templates for flexible report design.

Example Usage of pdf-reports

Installation:

pip install pdf-reports

Here is a basic example of how to use the pdf-reports library:

from pdf_reports import PDFReport

# Define your template file (template.pug)
template = """
doctype html
html
  head
    title= title
  body
    h1= title
    p This is a sample report.
"""

# Create a PDF report instance
report = PDFReport(template)

# Set the context for the template
context = {
    'title': 'Sample Report'
}

# Generate the PDF
pdf = report.generate(context, output='sample_report.pdf')

Take-off on PDF generation in Python

Python offers a range of powerful libraries for generating PDF reports. Whether you’re creating invoices, business reports, or data summaries, these tools allow you to automate the process, customize your documents, and ensure branding consistency. Choose the library that best fits your needs and start creating professional PDFs today!