How to create a PDF File using Python

Introduction

In this tutorial I will show you how to write to pdf file using Python 3. I will also create header, footer along with body of the text in the generated pdf file. I will also decorate such text with color and style. I am going to use fpdf library to create pdf file.

In order to work with fpdf library for pdf generation you have to first include the fpdf library. Then you need to create an object of FPDF type. The FPDF constructor uses default values such as A4 size paper in portrait and the measure unit is in millimeter. You can also specify the same using the FPDF constructor in the following way:

FPDF('P', 'mm', 'A4')

In the above constructor P means portrait. You can specify landscape using L.

Measure unit can be specified either in pt, cm or in.

Related Posts:

The constructor does not provide any page, so you have to add a new page using add_page() before you write anything on it.

Before you can print text, it is mandatory to select a font with set_font, otherwise the document would be invalid.

Prerequisites

Python 3.8.0 – 3.9.1, fpdf 1.7.2 (pip install fpdf or pip install fpdf=1.7)

Installing FPDF

Install the required pdf library FPDF using the below command from command line tool in administrator mode.

pip install fpdf

Once installed you will get the below message on the command line tool:

create pdf file using python fpdf

Create PDF

Now I will create a Python script that will create or generate pdf file. I will explain each of the lines in the below source code.

The very first line includes the FPDF library. Make sure you had installed the library.

In the next line I create object from FPDF library. Explanations for other sections are given after the source code.

from fpdf import FPDF

pdf = FPDF()

#header of the pdf file
header = 'Header of PDF Report'
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
w = pdf.get_string_width(header) + 6
pdf.set_x((210 - w) / 2)
pdf.cell(w, 9, header, 0, 0, 'C')
pdf.line(20, 18, 210-20, 18)

pdf.ln(10)
pdf.set_font('Times', '', 12)
pdf.multi_cell(0, 5, 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.')

pdf.ln()
pdf.set_font('Arial', '', 12)
pdf.set_fill_color(200, 220, 255)
pdf.cell(0, 6, 'Chapter %d : %s' % (1, 'Sample Label'), 0, 1, 'L', 1)

pdf.ln()
pdf.set_font('Courier', 'B', 12)
pdf.multi_cell(0, 5, 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.')

pdf.ln()
pdf.set_font('', 'U', 11)
pdf.multi_cell(0, 6, 'It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using \'Content here, content here\', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for \'lorem ipsum\' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose.')

pdf.ln()
pdf.set_font('', 'I')
pdf.cell(0, 5, 'This is italic text')

#pdf.set_y(0) #on top of the page
pdf.set_y(-30) #30 CM from the bottom of the page
pdf.set_font('Arial', '', 8)
pdf.set_text_color(0)
pdf.cell(0, 5, 'Page ' + str(pdf.page_no()), 0, 0, 'C')

pdf.output('simple.pdf', 'F')

The below code snippets write heading of the pdf file. As I said earlier that before adding any text to the file you need to add a page using add_page() function.

You must set font before you write any text using set_font() function. Font family is Arial, type is bold (B) and size is 16. Other font type can be U (underline), I (italic).

I calculate the header string length and align the text in the center of the line using set_x(). Finally write text using cell(). You can find about cell() function.

Next we print a line below the heading using line().

header = 'Header of PDF Report'
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
w = pdf.get_string_width(header) + 6
pdf.set_x((210 - w) / 2)
pdf.cell(w, 9, header, 0, 0, 'C')
pdf.line(20, 18, 210-20, 18)

ln() function used to put line breaks. The parameter passed to this function specifies the height of the line.

set_fill_color() function draws background color with RGB (red, Green, Blue) values passed as parameters to this function.

multi_cell() function writes text when you have long text and you want to wrap the text into multiple lines.

set_text_color() function sets the color of the text. You need to pass the value to this function. For example, I have passed 0 to make the font color black.

page_no() returns the current page number of the pdf file.

Finally we save into simple.pdf file using output() function.

Testing the Application

Executing the above Python code will give you the following output as shown in the image:

create pdf file using python

The header and footer have been highlighted in the above image. The above pdf file is generated at the same location where you have put your Python script.

That’s all. Hope you got an idea how to create pdf file using fpdf library in Python 3.

Source Code

Download

2 thoughts on “How to create a PDF File using Python

  1. Thank You, Thank You!!!!

    I’ve been waiting for Python3 to work with FPDF for years! Your tutorial shows that it’s ready for my needs.

Leave a Reply

Your email address will not be published. Required fields are marked *