Create PDF Files with Python

2024-01-08 07:26:06

PDF (Portable Document Format) is a popular file format widely used for generating legal documents, contracts, reports, invoices, manuals, eBooks, and more. It provides a versatile and reliable format for sharing, storing and presenting electronic documents in a consistent manner, independent of any software, hardware or operating systems.

Given these advantages, automated generation of PDF documents is becoming increasingly important in various fields. To automate the PDF creation process in Python, you can write scripts that generate PDFs based on specific requirements or input data. This article will give detailed examples to demonstrate how to use Python to create PDF files programmatically.

Python PDF Generator Library

To generate PDF using Python, we will need to use the Spire.PDF for Python library. It is a powerful Python library that provide PDF generation and processing capabilities. With it, we can use python to create PDFs from scratch and add various PDF elements to PDF pages.

To install the Python PDF generator library, simply use the following pip command to install from PyPI:

pip install Spire.PDF

Background Knowledge

Before we start, let's learn some background about creating a PDF file using the Spire.PDF for Python library.

PDF Page: A page in Spire.PDF for Python is represented by PdfPageBase class, which consists of a client area and margins all around. The content area is for users to write various contents, and the margins are usually blank edges.

Coordinate System: As shown in the figure below, the origin of the coordinate system on the page is located at the top left corner of the client area, with the x-axis extending horizontally to the right and the y-axis extending vertically down. All elements added to the client area are based on the specified X and Y coordinates.

Create PDF Files with Python

Classes and Methods: The following table lists some of the core classes and methods used to create PDFs in Python.

Member Description
PdfDocument class Represents a PDF document model.
PdfPageBase class Represents a page in a PDF document.
PdfSolidBrush class Represents a brush that fills any object with a solid color.
PdfTrueTypeFont class Represents a true type font.
PdfStringFormat class Represents text format information, such as alignment, characters spacing and indent.
PdfTextWidget class Represents the text area with the ability to span several pages.
PdfTextLayout class Represents the text layout information.
PdfDocument.Pages.Add() method Adds a page to a PDF document.
PdfPageBase.Canvas.DrawString() method Draws string at the specified location on a page with specified font and brush objects.
PdfPageBase.Canvas.DrawImage() method Draws an image at a specified location on a page.
PdfTextWidget.Draw() method Draws the text widget at the specified location on a page.
PdfDocument.SaveToFile() method Saves the document to a PDF file.

How to Create PDF Using Python

The following are the main steps for creating PDF files in Python:

  • Install Spire.PDF for Python.
  • Import modules.
  • Create a PDF document through the PdfDocument class.
  • Add a page to the PDF using PdfDocument.Pages.Add() method and return an object of PdfPageBase class.
  • Create desired PDF brush and font.
  • Draw text string or text widget at a specified coordinate on the PDF page using PdfPageBase.Canvas.DrawString() or PdfTextWidget.Draw() method.
  • Save the PDF document using PdfDocument.SaveToFile() method.

Python to Create PDF Files from Scratch

The following code example demonstrates how to use Python to create a PDF file and insert text and images. With Spire.PDF for Python, you can also insert other PDF elements such as lists, hyperlinks, forms, and stamps.

  • 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 = PdfFont(PdfFontFamily.Helvetica, 14.0, PdfFontStyle.Bold)
paraFont = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular, True)

# 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, 40.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, 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)

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

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

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

Create PDF Files with Python

Python to Generate PDF from Text File

The following code example shows the process of reading text from a .txt file and drawing it to a 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()

Create PDF Files with Python

Python to Create a Multi-Column PDF

Multi-column PDF are commonly used in magazines or newspapers. The following code example shows the process of creating a two-column PDF by drawing text in two separate rectangular areas on a PDF page.

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

# Creates a PDF document
pdf = PdfDocument()

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

# Define paragraph text
s1 = "Databases allow access to various services which, in turn, allow you to access your accounts and perform transactions all across the internet. " + "For example, your bank's login page will ping a database to figure out if you've entered the right password and username. " + "Your favorite online shop pings your credit card's database to pull down the funds needed to buy that item you've been eyeing."
s2 = "Databases make research and data analysis much easier because they are highly structured storage areas of data and information. " + "This means businesses and organizations can easily analyze databases once they know how a database is structured. " + "Common structures and common database querying languages (e.g., SQL) make database analysis easy and efficient."

# Get width and height of page
pageWidth = page.GetClientSize().Width
pageHeight = page.GetClientSize().Height

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

# Set the text alignment
format = PdfStringFormat(PdfTextAlignment.Left)

# Draws text at a specified location on the page
page.Canvas.DrawString(s1, font, brush, RectangleF(10.0, 20.0, pageWidth / 2 - 8, pageHeight), format)
page.Canvas.DrawString(s2, font, brush, RectangleF(pageWidth / 2 + 8, 20.0, pageWidth / 2 - 8, pageHeight), format)

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

Create PDF Files with Python

Free License for Creating PDF in Python

You can get a free temporary license of Spire.PDF for Python to generate PDF documents without any watermarks and limitations.

Conclusion

This blog post has provided a step-by-step guide on how to create PDF files based on the coordinate system defined in the Spire.PDF for Python library. In the code samples, you can learn about the process and methods of inserting text, images into PDFs and converting TXT files to PDFs. If you want to explore other PDF processing and conversion features of the Python PDF library, you can check out its online documentation.

For any issues while using, reaching out our technical support team via email or forum.

See Also