Create PDF Using Python

Ashish Pratap Singh
4 min readJan 20, 2023

--

PDF (Portable Document Format) is a widely-used format for storing and sharing documents. It is a versatile format that can be used to create a variety of documents, including text documents, images, and even interactive forms. In this article, we will discuss how to convert data into a PDF file using Python.

Convert Data into PDF

Python offers several libraries that can be used to create PDF files, including reportlab, FPDF, and PyFPDF. Each library has its own set of features and capabilities, so it's important to choose the one that best suits your needs. In this article, we will focus on the FPDF libraries, as this is the most popular and widely-used library for creating PDF files in Python.

FPDF is a library for Python that allows developers to create PDF files using a simple and intuitive syntax. PyFPDF is built on top of the FPDF library, which is a widely-used, open-source library for creating PDF files in PHP. PyFPDF brings the functionality of the FPDF library to Python, making it easy for Python developers to create PDF files without needing to learn a new set of tools.

To get started with FPDF, you will first need to install the library using pip:
pip install fpdf

Once the library is installed, you can begin creating PDF files by importing the FPDF class and extending it to create your own custom class. The following example shows how to create a simple PDF file that contains a table with three columns and two rows, and you’ll also know that how to plot image in PDF as well:

from fpdf import FPDF
import matplotlib.pyplot as plt


class PDF(FPDF):
def header(self):
self.set_font('Arial', 'B', 15)
self.cell(80)
self.cell(30, 10, 'Creating Report', 0, 0, 'C')
self.ln(20)

def footer(self):
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.cell(0, 10, 'Page ' + str(self.page_no()), 0, 0, 'C')

def chapter_title(self, num, label):
self.set_font('Arial', '', 12)
self.set_fill_color(200, 220, 255)
self.cell(0, 6, 'Plot Image:', 0, 1, 'L', 1)
# plotting/saving images in PDF
self.save_image(20, 40)
self.save_image(100, 40)
self.ln(50)
self.cell(0, 6, 'Details:', 0, 1, 'L', 1)
self.ln()
self.multi_cell(0, 5, 'Yesterday Reports:')
self.ln(5)
self.create_table()

# generating image using matplotlib library
def save_image(self, x_axis, y_axis):
name = "x_y.png"
x = [1,2,3,4,5]
y = [1,2,3,4,5]
plt.plot(x,y)
plt.savefig(name)
self.ln()
self.image(name, x = x_axis, y = y_axis, w = 80, h = 60, type = '', link = '')

def create_table(self):
# set color
self.set_font('Arial', 'B', 12)
# self.set_text_color(255, 255, 255)
# self.set_fill_color(255, 0, 0)
# self.set_draw_color(255, 255, 255)
# Create table header
self.cell(40, 10, 'Name', 1, 0, 'C')
self.cell(35, 10, 'Age', 1, 0, 'C')
self.cell(45, 10, 'Address', 1, 0, 'C')
self.ln()

self.set_font('Arial', '', 12)
# self.set_text_color(0, 0, 0)
# self.set_fill_color(255, 255, 255)
# Create table rows
self.cell(40, 10, 'Ashish', 1, 0, 'C')
self.cell(35, 10, '30', 1, 0, 'C')
self.cell(45, 10, 'Hyderabad', 1, 0, 'C')
self.ln()
self.cell(40, 10, 'Ashish', 1, 0, 'C')
self.cell(35, 10, '30', 1, 0, 'C')
self.cell(45, 10, 'Hyderabad', 1, 0, 'C')
self.ln()

pdf = PDF()
pdf.add_page()
# pdf.save_image(20, 40)
# pdf.save_image(100, 40)
pdf.chapter_title(1, 'Data')
# pdf.create_table()
pdf.output('data2.pdf', 'F')

In this example, a new class PDF is created that inherits from the FPDF class. The class contains a single method table() that creates a table with three columns, two rows and two images. The FPDF.cell() method is used to create the cells and add the data to the table. The FPDF.ln() method is used to add a line break after each row. Finally, the FPDF.output() method is used to convert the collected data into data.pdf file.

Table with three columns and 2 rows
Plotting Image after generated using Matplotlib Library

The FPDF.output() method is used to output the contents of a PDF document to a file or browser. The first argument of this method is the filename to which the PDF document should be saved. In the example you provided, the output is saved to a file named "data.pdf".

The second argument of the output method is the destination where the document is sent. It can take the following values:

  • ‘F’ (save to a local file)
  • ‘D’ (download PDF file with the name given by name)
  • ‘I’ (display the PDF in the browser but do not save it)
  • ‘S’ (return the document as a string)

The most common value used is ‘F’ because it allows you to save the PDF file to a specific location on your computer.

The *.output() method is the last method to be called in the script, as it finalizes the PDF document and saves it to the specified location.

Thanks for giving your time for reading this article. Please leave a clap if you like this article.

--

--

Responses (1)