Create PDF file in Python using fpdf package

In this tutorial, we are going to learn to create pdf in python using fpdf package. In python, there are lots of methods for creating a pdf file using the various package in python fpdf is most easy and understandable.

How to create a pdf file in python

1st we have to install the fpdf package in python. So for python 3 user use command:

pip install fpdf

or you can go through the link but I suggest you do install through the command  because of it quite easier:

https://code.google.com/p/pyfpdf/

Now you are all set to create a pdf file in Python. So let’s get started.

import the package’s class FPDF :

#for python 3 install fpdf package "pip install fpdf".
from fpdf import FPDF

now create the class and add pdf page.

#class object FPDF() which is predefiend in side the package fpdf.
document=FPDF()
document.add_page()

till now we have  created pdf page successfully now we have to set the font size of the pdf page:

#font size setting of the page 
document.set_font("Arial", size=15)

now we are going to display some text on pdf page :

#txt message will displayed on pdf page  at the center.
document.cell(200, 10, txt="this is the programming of creating pdf file", ln=1, align="L")

filename and page size like A4 or A3 or something like this …

#pdf file naming.
document.output("pdf_file_name.pdf")
#creating page format A4 Or A3 Or ...
document=FPDF(orientation='P', unit='mm', format='A3')

whole program in a single window:

#for python 3 install fpdf package "pip install fpdf".
from fpdf import FPDF
#class object FPDF() which is predefiend in side the package fpdf.
document=FPDF()
document.add_page()
#font size setting of the page 
document.set_font("Arial", size=15)
#txt message will displayed on pdf page  at the center.
document.cell(200, 10, txt="this is the programming of creating pdf file", ln=1, align="L")
#pdf file naming.
document.output("pdf_file_name.pdf")
#creating page format A4 Or A3 Or ...
document=FPDF(orientation='P', unit='mm', format='A3')
print("pdf has been created successfully....")

Output:

pdf has been created successfully.......

Also read,

2 responses to “Create PDF file in Python using fpdf package”

  1. S says:

    If I run this again, It should be appended to the next page and should not overwrite the existing one. Is there any way to do that?

  2. Arif Durmuş says:

    from fpdf import FPDF

    class PDF(FPDF):
    def header(self):
    self.set_font(‘Arial’, ‘B’, 16)
    self.cell(0, 10, ‘2025 OZEL MOBILYA FIYAT LISTESI’, 0, 1, ‘C’)
    self.ln(10)

    def footer(self):
    self.set_y(-15)
    self.set_font(‘Arial’, ‘I’, 8)
    self.cell(0, 10, ‘Not: Fiyatlarimiza KDV dahil degildir.’, 0, 0, ‘C’)

    def create_pdf():
    pdf = PDF()
    pdf.add_page()
    pdf.set_font(“Arial”, size=12)

    # Tablo Başlıkları
    pdf.set_fill_color(200, 220, 255)
    pdf.cell(90, 10, “Urun Adi & Ozellik”, 1, 0, ‘L’, 1)
    pdf.cell(40, 10, “Fiyat”, 1, 1, ‘C’, 1)

    # Ürünler (Türkçe karakter sorunu olmaması için karakterleri düzelttim)
    urunler = [
    (“Konsol Alt Modul\n(Boyali Full MDF)”, “17.500 TL”),
    (“Konsol Aynasi\n(Boyali MDF)”, “3.250 TL”),
    (“Acilir Yemek Masasi\n(Boyali Full MDF)”, “16.500 TL”),
    (“Sandalye (6 Adet)\n(Istenilen renk kumas)”, “19.000 TL”),
    (“Unite Alt Blok\n(Boyali Full MDF)”, “16.500 TL”),
    (“Unite Ust Blok\n(Boyali Full MDF)”, “2.500 TL”),
    (“Orta Sehpa\n(Boyali Full MDF)”, “7.000 TL”),
    ]

    for urun, fiyat in urunler:
    # Satır yüksekliği
    pdf.cell(90, 15, urun, 1, 0, ‘L’)
    pdf.cell(40, 15, fiyat, 1, 1, ‘C’)

    pdf.output(“Mobilya_Fiyat_Listesi.pdf”)
    print(“PDF dosyaniz olusturuldu!”)

    create_pdf()

Leave a Reply

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