Creating PDF Documents With Python
Last Updated :
21 Mar, 2024
In this article, we will be learning how to create PDFs in Python. A very famous module named pypdf2 is used to modify and read existing pdfs but its major disadvantage is that it cannot create new pdf files. So Today we are looking to learn about another python module named report lab that helps us to create new pdf files and edit our heart's content on them.
Module Required:
Reportlab: This module is used to handle PDF files.
pip install reportlab
Step-by-step Approach:
Step 1:
We start by importing the modules and classes. Canvas is used to draw things on the pdf, ttfonts and pdfmetrics will help us to use custom TTF fonts in the pdf, and colours would help us to pick colours easily without remembering their hex values.
Python3
# importing modules
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib import colors
Step 2:
Next, we initialize all the things we would b writing and drawing in the document to specific variables to easily call them when needed.
Python3
# initializing variables with values
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Technology'
subTitle = 'The largest thing now!!'
textLines = [
'Technology makes us aware of',
'the world around us.',
]
image = 'image.jpg'
Step 3:
Next, we initialize a canvas object with the name of the pdf and set the title to be the document title.
Python3
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
Step 4:
Next, we register our external font to the reportlab fonts using pdfmetrics and TTFont and assigned it a name. Next, we set the new font with a size. Then we draw the string on the pdf using the drawCentredString function that takes the x and y values as the centre of the text to the written and the left, right, top and bottom of the text are adjusted accordingly. Note that we need the TTF file to be present in the folder to execute the commands.
Python3
# registering a external font in python
pdfmetrics.registerFont(
TTFont('abc', 'SakBunderan.ttf')
)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
Step 5:
Next for the subtitle, we do the same thing except this time the colour of the subtitle be blue, and this time we use a standard font that ships natively with report lab.
Python3
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)
Step 6:
Next, we draw a line and then enter several lines of text that we defined earlier inside a list. The first line defines the starting x and y position of the text. The next two lines set the font, font size and font colour of the text. The next two lines traverse through each element in the list and add it as a line to the text. The last line draws the text to the screen.
Python3
# drawing a line
pdf.line(30, 710, 550, 710)
# creating a multiline text using
# textline and for loop
text = pdf.beginText(40, 680)
text.setFont("Courier", 18)
text.setFillColor(colors.red)
for line in textLines:
text.textLine(line)
pdf.drawText(text)
Step 7:
At last, we draw a picture on the pdf using the drawInlineImage function in which the parameters are the path of the image and the x and y coordinates of the image. In this case, the image was in the same directory as the py file, so according to the relative path, we need to write only the name of the file with the extension, if it was in some other directory, a relevant correct relative path should be used.
Python3
# drawing a image at the
# specified (x.y) position
pdf.drawInlineImage(image, 130, 400)
# saving the pdf
pdf.save()
Below is the complete program:
Python3
# importing modules
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib import colors
# initializing variables with values
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Technology'
subTitle = 'The largest thing now!!'
textLines = [
'Technology makes us aware of',
'the world around us.',
]
image = 'image.jpg'
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
# registering a external font in python
pdfmetrics.registerFont(
TTFont('abc', 'SakBunderan.ttf')
)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)
# drawing a line
pdf.line(30, 710, 550, 710)
# creating a multiline text using
# textline and for loop
text = pdf.beginText(40, 680)
text.setFont("Courier", 18)
text.setFillColor(colors.red)
for line in textLines:
text.textLine(line)
pdf.drawText(text)
# drawing a image at the
# specified (x.y) position
pdf.drawInlineImage(image, 130, 400)
# saving the pdf
pdf.save()
Output:
PDF Image
Similar Reads
Create XML Documents using Python
Extensible Markup Language(XML), is a markup language that you can use to create your own tags. It was created by the World Wide Web Consortium (W3C) to overcome the limitations of HTML, which is the basis for all Web pages. XML is based on SGML - Standard Generalized Markup Language. It is used for
3 min read
Creating Ebooks with borb in Python
Creating ebooks can be a rewarding endeavor, whether for sharing knowledge, storytelling, or distributing documentation. The borb library in Python is a powerful tool that enables developers to generate PDFs with rich formatting and various multimedia elements. This article will guide you through th
3 min read
Creating Your Own Python IDE in Python
In this article, we are able to embark on an adventure to create your personal Python Integrated Development Environment (IDE) the usage of Python itself, with the assistance of the PyQt library. What is Python IDE?Python IDEs provide a characteristic-rich environment for coding, debugging, and goin
3 min read
How to Convert a PDF to Document using Python?
Converting PDF to Word document manually takes a lot of time, especially if you have many files. Python makes this task easy by automating the process. The pdf2docx module helps convert PDFs into editable Word documents quickly with just a few lines of code. Whether you need full control over the co
4 min read
How to create modules in Python 3 ?
Modules are simply python code having functions, classes, variables. Any python file with .py extension can be referenced as a module. Although there are some modules available through the python standard library which are installed through python installation, Other modules can be installed using t
4 min read
Creating and Viewing HTML files with Python
Python is one of the most versatile programming languages. It emphasizes code readability with extensive use of white space. It comes with the support of a vast collection of libraries which serve for various purposes, making our programming experience smoother and enjoyable. Python programs are us
3 min read
Python - Output Formatting
In Python, output formatting refers to the way data is presented when printed or logged. Proper formatting makes information more understandable and actionable. Python provides several ways to format strings effectively, ranging from old-style formatting to the newer f-string approach.Formatting Out
5 min read
Working with Page Break - Python .docx Module
Prerequisites: docx Word documents contain formatted text wrapped within three object levels. Lowest level- run objects, middle level- paragraph objects and highest level- document object. So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents
2 min read
Parsing PDFs in Python with Tika
Apache Tika is a library that is used for document type detection and content extraction from various file formats. Using this, one can develop a universal type detector and content extractor to extract both structured text and metadata from different types of documents such as spreadsheets, text do
2 min read
Parsing XML with DOM APIs in Python
The Document Object Model (DOM) is a programming interface for HTML and XML(Extensible markup language) documents. It defines the logical structure of documents and the way a document is accessed and manipulated. Parsing XML with DOM APIs in python is pretty simple. For the purpose of example we wil
2 min read