Python fpdf Module – Beginners Guide

Let’s say you are building an application and you want to create functionality by which users can download their data locally to their system. The most suitable file format that you can give for downloading can be a PDF.

PDF stands for Portable Document Format, a widely used format for distributing documents while preserving actual formatting and guaranteeing the same output on different devices.

Now the question comes how will you convert text data to a PDF document? Well, in Python you can do this easily by implementing a unique module fpdf.

In this article, we will look at the fpdf module in detail and demonstrate it with examples. Let’s get started.

Also Read: How to Use Emoji Module in Python (With Examples)


Python fpdf Module – Quick Overview

The fpdf is a library for PDF document generation under Python. It offers us various built-in functions to convert text files as well as text data into PDF files. This is helpful especially when we want to represent the output of certain computations in a PDF format directly by the execution of the script.

By this, we automate the entire process of conversion of data into a PDF format without having to worry about the storage path/location, compatibility of the extension, etc, because with fpdf module, we can even have the converted PDF file at the local storage or to a VM or even to a public cloud platform.

In this article we will use the fpdf module for the following functionalities:

  • Conversion of text files into PDF
  • Conversion of text data into PDF

Before deep dive into both of them, let’s see the installation instructions for this module.


Installation Instructions

The fpdf module does not come with Python, we have to install it manually in order to create a PDF file.

Below are two ways to install fpdf module:

1. By downloading source code

You can use the below command to get the latest development version of fpdf library by downloading files. This method is recommended by official documentation.

hg clone https://code.google.com/p/pyfpdf/
cd pyfpdf
python setup.py install

2. By using PIP

Or for simplicity, you can also use PIP, which we recommend, especially for beginners as it is a one-liner and easier to execute. Just make sure you have PIP installed on your system. 

pip install fpdf

Converting Text Files to PDF Using fpdf Module

With the Python fpdf module, we can convert the files with .txt extensions i.e. text files into a PDF format easily with fpdf.output() function.

Following are the steps to convert a text file to PDF:

  1. Import the fpdf module. 
  2. Create an fpdf object using the FPDF() class.
  3. Use the add_page() function to have a white page for us to imprint the data on.
  4. Use the set_font() function to set the font.
  5. Open the text file with the file.open() function in read mode.
  6. Further, we need to traverse the data of every row using the cell() function and simultaneously get it ready for storage in a PDF format.
  7. Finally, use pdf.output() function to represent the traversed data into a PDF format at the specified location.

Example:

from fpdf import FPDF 
pdf = FPDF() 
  
pdf.add_page()
 
pdf.set_font("Arial", size = 25) 
 
# create a cell 
file = open("C:/Users/HP/OneDrive/Desktop/data.txt", "r") 
   
# insert the texts in pdf 
for g in file: 
    pdf.cell(200, 10, txt = g, ln = 1, align = 'C') 
    
 
pdf.output("op.pdf")

Text file:

Text file

Output:

Text Files to PDF

Converting Text Date to PDF Using fpdf Module

Apart from converting the .txt files into a PDF format, we can also convert the raw text data directly into a PDF form. On similar lines to the .txt conversion to .pdf, here we give raw text data as input and convert it into PDF format.

Following are the steps to convert text data to PDF:

  1. Import the fpdf module.
  2. Create an fpdf object using the FPDF() class.
  3. Use the add_page() function to have a white page for us to imprint the data on.
  4. Use the set_font() function to set the font family.
  5. Create a cell using the cell() function and pass the raw text data to it.
  6. We can create multiple such cells with text data through step 4.
  7. Use the pdf.output() function to have all the text data in the PDF format line by line.

Example:

from fpdf import FPDF 
pdf = FPDF() 
  
pdf.add_page() 
 
pdf.set_font("Arial", size = 25) 
 
# create a cell 
pdf.cell(200, 10, txt = "Technologies", 
        ln = 1, align = 'C') 
 
pdf.cell(200, 10, txt = "Come, learn Python with us!", 
        ln = 2, align = 'C') 
 
pdf.output("info.pdf")

Output:

Text Date to PDF

Conclusion

In this article, we have discovered a module fpdf for Python to generate PDF files. We have seen how to convert a text file into PDF using fpdf module as well as how to create a new PDF file from text data. This module is not the default Python module so make sure you have manually installed it before importing, for which the best way is to use PIP.

This module also lets you modify different parameters for the generated PDF file such as customize page format, margins, and units (e.g., mm, in, pt), enable automatic line breaks, automatic page breaks, work with Unicode (UTF-8), etc.

That’s it for this article, for more such posts related to Python programming, stay tuned with us.

Read More: NodeJS vs PHP

References

Ninad Pathak
Ninad Pathak
Articles: 55