How to Create PDF Using Python: A Comprehensive Guide

2024-01-08 07:26:06 Koohji

In the realm of Python development, generating PDF files programmatically is a crucial skill for building applications that automate reports, invoices, or digital documents. If you're searching for an efficient Python PDF generator that combines ease of use with robust functionality, Spire.PDF for Python stands out as a recommended choice. This comprehensive guide will walk you through the process of how to use Python to create PDF files. Covering the following:

Getting Started with Spire.PDF for Python

Python PDF library

Spire.PDF for Python is a feature-rich library that simplifies PDF generation with Python. Key advantages include:

  • Rich Feature Set: Add text, images, tables, hyperlinks, watermarks, and digital signatures.
  • Security: Encrypt PDFs with passwords and permissions.
  • Metadata Control: Set author, title, keywords, and more.
  • Cross-Platform Support: Works on Windows, macOS, and Linux.
  • No Dependencies: Operates without Adobe Acrobat or other third-party tools.

Installation

The Python PDF generator library can be installed via pip. Open your terminal or command prompt and run the following command:

pip install Spire.PDF

This installs the latest version of Spire.PDF from the Python Package Index (PyPI).

Understanding the Coordinate System in Spire.PDF

One of the key aspects of working with Spire.PDF is understanding its coordinate system, which defines how elements are positioned on a PDF page. Here's a breakdown of its key characteristics:

PDF Page

When creating a page, you can define its size and margins. Therefore, the PDF page in Spire.PDF for Python (represented by PdfPageBase class) usually consists of a content area and margins all around.

Origin Point and Axes

  • Origin: The origin of the coordinate system is located at the top-left corner of the content area.
  • X-axis: Extends horizontally from the left to the right.
  • Y-axis: Extends vertically from the top downward.

Coordinate system in Spire.PDF for accurate elements positioning

How to Create PDF Files Using Python

Create PDF from Scratch in Python

Let's start with a simple example to create a PDF document with a single page containing text and an image. With Spire.PDF for Python, you can also insert other PDF elements such as tables, lists, hyperlinks and digital signatures.

Below are the main classes or methods used for PDF creation.

PDF font and brush:

  • PdfSolidBrush class: Represents a brush that fills any object with a solid color.
  • PdfTrueTypeFont class: Represents true type fonts (e.g., Arial, Calibri, SimHei).
  • PdfFont class: Represents standard PDF fonts (Helvetica, Times Roman, Courier, Symbol, ZapfDingbats).

Draw text on page:

  • PdfCanvas.DrawString() method: Draws text string at a specified location on a page. This is more suitable for rendering short text strings, such as titles, labels. For long text, you need to manually insert line breaks, otherwise the text will be truncated.
  • PdfTextWidget class: Represents the text area with the ability to span several pages.
  • PdfTextWidget.Draw() method: Draws text widget within a specified rectangle area on a page. This is suitable for rendering long text, as the text will automatically wrap within the rectangular area.

Draw image on page:

  • PdfImage.FromFile() method: Loads an image.
  • PdfCanvas.DrawImage() method: Draws the loaded image at a specified location on a page.

Code Example:

  • Python
from spire.pdf.common import *
from spire.pdf import *

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add()

# Specify title text and paragraph content
titleText = "Spire.PDF for Python"
paraText = "Spire.PDF for Python is a professional PDF development component that enables developers to create, read, edit, convert, and save PDF files in Python programs without depending on any external applications or libraries. This Python PDF class library provides developers with various functions to create PDF files from scratch or process existing PDF documents completely through Python programs."

# Create solid brushes
titleBrush = PdfSolidBrush(PdfRGBColor(Color.get_Blue()))
paraBrush = PdfSolidBrush(PdfRGBColor(Color.get_Black()))

# Create fonts
titleFont = PdfTrueTypeFont("Arial", 14.0, PdfFontStyle.Bold, True)
paraFont = PdfFont(PdfFontFamily.TimesRoman, 12.0, PdfFontStyle.Regular)

# Set the text alignment
textAlignment = PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle)

# Draw title on the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, page.Canvas.ClientSize.Width / 2, 10.0, textAlignment)

# Create a PdfTextWidget object to hold the paragraph content
textWidget = PdfTextWidget(paraText, paraFont, paraBrush)

# Create a rectangle where the paragraph content will be placed
rect = RectangleF(PointF(0.0, 30.0), page.Canvas.ClientSize)

# Draw the widget on the page
textWidget.Draw(page, rect)

# Load an image
image = PdfImage.FromFile("Python.png")

# Draw the image at a specified location on the page
page.Canvas.DrawImage(image, 14.0, 100.0)

# Save the PDF document
pdf.SaveToFile("CreatePDF.pdf")
pdf.Close()

The generated PDF file:

Create a PDF document with text and image.

Generate PDF from a Text File with Python

You can also generate a PDF document from a text file using Python. This example reads the text content from a .txt file and the draws it to the specified location on a PDF page.

  • Python
from spire.pdf.common import *
from spire.pdf import *

def ReadFromTxt(fname: str) -> str:
    with open(fname, 'r') as f:
        text = f.read()
    return text

# Create a pdf document
pdf = PdfDocument()

# Add a page to the PDF
page = pdf.Pages.Add(PdfPageSize.A4(), PdfMargins(20.0, 20.0))

# Create a PdfFont and brush
font = PdfFont(PdfFontFamily.TimesRoman, 12.0)
brush = PdfBrushes.get_Black()

# Get content from a .txt file
text = ReadFromTxt("text.txt")

# Create a PdfTextWidget object to hold the text content
textWidget = PdfTextWidget(text, font, brush)

# Create a rectangle where the text content will be placed
rect = RectangleF(PointF(0.0, 50.0), page.Canvas.ClientSize)

# Set the text layout
textLayout = PdfTextLayout()
textLayout.Layout = PdfLayoutType.Paginate

# Draw the widget on the page
textWidget.Draw(page, rect, textLayout)

# Save the generated PDF file
pdf.SaveToFile("GeneratePdfFromText.pdf", FileFormat.PDF)
pdf.Close()

Result:

Convert a TXT file to a PDF file

More PDF Generation Examples

Refer to the below articles to learn how to use Python to create PDFs from many other file formats:

FAQs About Spire.PDF for Python

Q: Is Spire.PDF for Python free to use?

A: There’s a free version available but with certain limitations. You can request a 30-day trial license to fully test the commercial version.

Q: Does Spire.PDF support non-English languages (e.g., Chinese, Japanese)?

A: Yes, but you must use a font that supports the target language. For example:

  • Python
# To display Chinese text, use a font like "SimSun" or "Microsoft YaHei"
font = PdfTrueTypeFont("SimSun", 12.0, PdfFontStyle.Regular, True)

# To display Japanese text, use a font like "MS Gothic" or "Yu Gothic"
font = PdfTrueTypeFont("MS Gothic", 12.0, PdfFontStyle.Regular, True)

Q: Is there a way to encrypt the generated PDFs?

A: Yes, Spire.PD for Python provides Encrypt() method to password-protect the PDF files.

  • Python
# Encrypt the PDF file with an open password and a permission password
pdf.Security.Encrypt("openPsd", "permissionPsd", PdfPermissionsFlags.FillFields, PdfEncryptionKeySize.Key128Bit)

Q: Can I create interactive PDF forms with Spire.PDF?

A: Yes, Spire.PDF supports creating form fields (text boxes, checkboxes, radio buttons) and filling existing forms programmatically. Refer to the tutorial for examples.

Conclusion

Spire.PDF for Python simplifies the process of creating PDF files with Python, offering a balance of ease-of-use, performance, and advanced features. Whether you’re building simple text documents or complex data reports, its intuitive API make it an ideal choice for developers.

By following the installation guide, code examples, along with a clear grasp of its coordinate system, you can quickly integrate PDF generator functionality into your Python projects. Explore the official documentation to unlock advanced functionalities like barcodes, shapes, and PDF/A compliance.