Lecture 1: Installing Required Packages.

This is how you install fpdf2 package:

pip install fpdf2

If on mac, type:

pip3 install fpdf2


Lecture 2: Creating Our First Page.

from fpdf import FPDF

pdf = FPDF()

pdf.add_page()
pdf.set_font("helvetica", "B", 16)
pdf.cell(40, 10, "Hello World!")
pdf.output("sample.pdf")


Lecture 3: Adding header footer & logo

Get a logo from the resources section and add it to the folder.

from fpdf import FPDF

# inhetiting form the class
class PDF(FPDF):
    
    def header(self):
        
        self.image("logo.png", 10, 8, 33)
      
        self.set_font("helvetica", "B", 15)
        
        self.cell(80)
       
        self.cell(30, 10, "Title", border=1, align="C")
       
        self.ln(20)

    def footer(self):
        
        self.set_y(-15)
      
        self.set_font("helvetica", "I", 8)
       
        self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C")


pdf = PDF()
pdf.add_page()
pdf.set_font("Times", size=12)


for i in range(1, 41):
    pdf.cell(0, 10, f"Printing line number {i}", new_x="LMARGIN", new_y="NEXT")
pdf.output("new-tuto2.pdf")



Lecture : Reading Data From File

Get the file data from lorem ipsum

from fpdf import FPDF

class PDF(FPDF):
    def header(self):
       
        self.set_font("helvetica", "B", 15)
       
        width = self.get_string_width(self.title) + 6
       
        self.set_x((210 - width) / 2)
       
        self.set_draw_color(0, 80, 180)
        
        self.set_fill_color(230, 230, 0)
       
        self.set_text_color(220, 50, 50)
       
        self.set_line_width(1)
       
        self.cell(
            width,
            9,
            self.title,
            border=1,
            new_x="LMARGIN",
            new_y="NEXT",
            align="C",
            fill=True,
        )
        
        self.ln(10)

    def footer(self):
       
        self.set_y(-15)
        
        self.set_font("helvetica", "I", 8)
        
        self.set_text_color(128)
       
        self.cell(0, 10, f"Page {self.page_no()}", align="C")

    def chapter_title(self, num, label):
       
        self.set_font("helvetica", "", 12)
        
        self.set_fill_color(200, 220, 255)
       
        self.cell(
            0,
            6,
            f"Chapter {num} : {label}",
            new_x="LMARGIN",
            new_y="NEXT",
            align="L",
            fill=True,
        )
       
        self.ln(4)

    def chapter_body(self, filepath):
        
        with open(filepath, "rb") as fh:
            txt = fh.read().decode("latin-1")
       
        self.set_font("Times", size=12)
        
        self.multi_cell(0, 5, txt)
       
        self.ln()
       
        self.set_font(style="I")
        self.cell(0, 5, "(end of excerpt)")

    def print_chapter(self, num, title, filepath):
        self.add_page()
        self.chapter_title(num, title)
        self.chapter_body(filepath)

pdf = PDF()
pdf.set_title("100 Ways to learn programming")
pdf.set_author("Ashutosh Pawar")
pdf.print_chapter(1, "GETTING STARTED WITH PROGRAMMING", "para.txt")
pdf.print_chapter(2, "WHICH PROGRAMMING LANGUAGE TO LEARN", "para.txt")
pdf.output("sample.pdf")


Lecture: Creating table:

countries.txt data:

Country,Capital,Area (sq km),Population
Algeria,Algiers,2381740,33770000
American Samoa,Pago Pago,199,57500
Andorra,Andorra la Vella,468,72400
Angola,Luanda,1246700,12531000
Anguilla,The Valley,102,14100
Antigua Barbuda,Saint John,443,69800
Argentina,Buenos Aires,2766890,40677000
Armenia,Yerevan,29800,2969000
Aruba,Oranjestad,193,101500
Australia,Canberra,7686850,20601000
Austria,Vienna,83858,8206000
Azerbaijan,Baku,86600,8178000
Bahamas,Nassau,13940,307500
Bahrain,Manama,665,718300
Bangladesh,Dhaka,144000,153547000
Barbados,Bridgetown,431,282000
Belarus,Minsk,207600,9686000
Belgium,Brussels,30510,10404000
Belize,Belmopan,22966,301300
Benin,Porto Novo,112620,8295000
Bermuda,Hamilton,53,66500
Bhutan,Thimphu,47000,682300
Bolivia,Sucre,1098580,9248000
Bosnia,Sarajevo,51129,4590000
Botswana,Gaborone,600370,1842000
Brazil,Brasilia,8511965,191909000
British Virgin Isl,Road Town,153,24000
Brunei,Bandar Seri ,5770,381400
import csv
from fpdf import FPDF
from fpdf.fonts import FontFace


with open("countries.txt", encoding="utf8") as csv_file:
   
    data = list(csv.reader(csv_file, delimiter=","))

pdf = FPDF()
pdf.set_font("helvetica", size=14)


pdf.add_page()
with pdf.table() as table:
  
    for data_row in data:
        row = table.row()
      
        for datum in data_row:
           
            row.cell(datum)


pdf.add_page()
pdf.set_draw_color(255, 0, 0)
pdf.set_line_width(0.3)
headings_style = FontFace(emphasis="BOLD", color=255, fill_color=(255, 100, 0))
with pdf.table(
    borders_layout="NO_HORIZONTAL_LINES",
    cell_fill_color=(224, 235, 255),
    #cell_fill_mode=lambda i, j: i % 2,
    col_widths=(42, 39, 35, 42),
    headings_style=headings_style,
    line_height=6,
    text_align=("LEFT", "CENTER", "RIGHT", "RIGHT"),
    width=160,
) as table:
    for data_row in data:
        row = table.row()
        for datum in data_row:
            row.cell(datum)

pdf.output("tuto5.pdf")


Lecture: Adding Links & HTML To PDF Page.

from fpdf import FPDF

pdf = FPDF()


pdf.add_page()
pdf.set_font("helvetica", size=20)
pdf.write(5, "To find out what's new in self tutorial, click ")
pdf.set_font(style="U")
link = pdf.add_link(page=2)
pdf.write(5, "here", link)
pdf.set_font()


pdf.add_page()
pdf.image(
    "demo.png", 10, 10, 50, 0, "", "<https://www.google.com>"
)
pdf.set_left_margin(60)
pdf.set_font_size(18)
pdf.write_html(
    """You can print text mixing different styles using HTML tags: <b>bold</b>, <i>italic</i>,
<u>underlined</u>, or <b><i><u>all at once</u></i></b>!
<br><br>You can also insert links on text, such as <a href="<https://www.google.com>"><https://www.google.com></a>,
or on an image: the logo is clickable!"""
)
pdf.output("tuto6.pdf")