How to Create a PDF Document from Scratch with Python
Creating a PDF document programmatically provides a powerful way to generate customized and dynamic content. Rather than relying on pre-existing templates or manually editing files, this approach allows developers to generate PDFs on-the-fly using code. With complete control over the layout, content, styling, and data integration, you can generate documents with complex structures, incorporate images and graphics, add tables, apply styling and formatting, and dynamically populate content based on real-time data.
In this article, I am going to introduce how to create a simple PDF document from scratch in Python using Spire.PDF for Python.
Install Dependency
This solution requires Spire.PDF for Python to be installed as the dependency, which is a Python library for reading, creating and manipulating PDF documents in a Python program. You can install Spire.PDF for Python by executing the following pip command.
pip install Spire.PDF
Prerequisite Knowledge
A page in Spire.PDF (represented by PdfPageBase) consists of client area and margins all around. The content area is for users to write various contents, and the margins are usually blank edges.
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 must be based on the specified coordinates.
For instance, to add or draw an image on a page, you could use the PdfPageBase.Canvas.DrawImage(PdfImage image, float x, float y) method, where x and y determine the starting position for drawing the image.
Create a PDF Document from Scratch in Python
The following code snippet creates a simple PDF document which contains a title, a paragraph, an image and a list. In addition, Spire.PDF for Python supports the addition of many more elements such as shapes, tables, form fields and hyperlinks to a PDF document.
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
doc = PdfDocument()
# Set margins
doc.PageSettings.Margins = PdfMargins(35.0, 35.0, 35.0, 35.0)
# Add a page
page = doc.Pages.Add()
# Specify heading text
titleText = "What’s Python"
# Create solid brushes
titleBrush = PdfSolidBrush(PdfRGBColor(Color.get_Purple()))
paraBrush = PdfSolidBrush(PdfRGBColor(Color.get_Black()))
# Create true type fonts
titleFont = PdfTrueTypeFont("Times New Roman", 18.0, 1, True)
paraFont = PdfTrueTypeFont("Times New Roman", 13.0, 0, True)
# Set the text alignment via PdfStringFormat class
format = PdfStringFormat()
format.Alignment = PdfTextAlignment.Center
# Draw heading on the center of the page
page.Canvas.DrawString(titleText, titleFont, titleBrush, (float)(page.Canvas.ClientSize.Width / 2), 20.0, format)
# Get paragraph content from a .txt file
paraText = "Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code \
readability with the use of significant indentation.Python is dynamically typed and garbage-collected. It \
supports multiple programming paradigms,including structured (particularly procedural), object-oriented and functional programming."
# Create a PdfTextWidget object to hold the paragrah content
widget = PdfTextWidget(paraText, paraFont, paraBrush)
# Create a rectangle where the paragraph content will be placed
rect = RectangleF(0.0, 50.0, page.Canvas.ClientSize.Width, page.Canvas.ClientSize.Height)
# Set the PdfLayoutType to Paginate to make the content paginated automatically
layout = PdfTextLayout()
layout.Layout = PdfLayoutType.Paginate
# Draw the widget on the page
layoutResult = widget.Draw(page, rect, layout)
# Draw an image on the page
image = PdfImage.FromFile("C:/Users/Administrator/Desktop/python.png")
page.Canvas.DrawImage(image, 0.0, layoutResult.Bounds.Bottom + 10.0)
# Create a list
# Create a numbered list
listContent = """Easy to Learn and Use
Expressive Language
Cross-platform Language
Free and Open Source
Object-oriented Language
Easy to Maintain
Extensible Feature
Database Supprot"""
list = PdfSortedList(listContent);
# Set the font, indent, text indent, brush of the list
list.Font = paraFont;
list.Indent = 2.0
list.TextIndent = 4.0
list.Brush = paraBrush
list.Marker = PdfOrderedMarker(PdfNumberStyle.LowerLatin, paraFont)
# Draw list on the page at the specified location
list.Draw(page.Canvas, (float)(image.PhysicalDimension.Width) + 30.0, layoutResult.Bounds.Bottom + 20.0)
# Save to file
doc.SaveToFile("output/CreatePdfDocument.pdf")
doc.Dispose()
P.S. There is a red watermark in the generated PDF document, which can be removed by applying a commercial license. Currently, this library does not provide a free version for users to generate PDFs without watermarks.