To build anything, you have to search if there is a Python package already present for it.
Google different Python packages.
Visit fpdf and check the latest release.
Now visit fpdf2.
You will get the steps to download and install FPDF.
This is how you install fpdf2 package:
pip install fpdf2
If on mac, type:
pip3 install fpdf2
# here fpdf is a package which we have just installed # FPDF is a class, just like any other class we create # CMD+click on FPDF to view the actual class we have in the package from fpdf import FPDF # we create an object of this class using the FPDF constructor # why do we do this, so that we can access the methods which are there in the class # To this, we can set the orientation, unit in mm,cm,in and format as well pdf = FPDF() # using the object, we access the add_page() method, add_page creates a page for us. pdf.add_page() # while creating the page, we must always set a font as well, or else it wont work # family, style:bold, italic, underlined, size pdf.set_font("helvetica", "B", 16) #once we have the page and once we have set the font, we could now add some text to the page # A cell is a rectangular area which can contain text, it can have border as well, it can have a # background color and it can be aligned and placed anywhere on the page # 40 is width of the cell in mm, 10 is height of the cell in mm. pdf.cell(40, 10, "Hello World!") #document is closed and saved using the file name which we pass it pdf.output("sample.pdf")
Get a logo from the resources section and add it to the folder.
from fpdf import FPDF # inhetiting form the class class PDF(FPDF): # here we are overriding the header method def header(self): # Rendering logo: #def image(self, name, x=None, y=None, w=0,h=0,type='',link=''): #"Put an image on the page" self.image("logo.png", 10, 8, 33) # Setting font: helvetica bold 15 self.set_font("helvetica", "B", 15) # Moving cursor to the right: #by creating a cell/ empty cell with width 80 so that there can be space between logo and title self.cell(80) # Printing title: # create another cell and add title to it self.cell(30, 10, "Title", border=1, align="C") # Performing a line break: self.ln(20) def footer(self): # Position cursor at 1.5 cm from bottom: self.set_y(-15) # Setting font: helvetica italic 8 self.set_font("helvetica", "I", 8) # Printing page number: #To print the page number, a null value is passed as the cell width. # It means that the cell should extend up to the right margin of the page; # it is handy to center text. # The current page number is returned by the page_no method; #as for the total number of pages, # it is obtained by means of the special value {nb} which will be substituted on document # closure self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", align="C") # Instantiation of inherited class pdf = PDF() pdf.add_page() pdf.set_font("Times", size=12) # Create 40 lines and print them for i in range(1, 41): #The first argument, 0, sets the width of the cell to the maximum available width. This allows the text to wrap automatically based on the content. #The second argument, 10, specifies the height of the cell as 10 units (presumably in millimeters, the default unit of fpdf2). #The third argument, f"Printing line number {i}", is the content that will be displayed in the cell. It includes the line number using f-string formatting. #The new_x="LMARGIN" parameter instructs the cell() method to position the cell at the left margin of the page. #The new_y="NEXT" parameter positions the cell below the previous cell, allowing each line to be printed one below the other. pdf.cell(0, 10, f"Printing line number {i}", new_x="LMARGIN", new_y="NEXT") pdf.output("new-tuto2.pdf")
Get the file data from lorem ipsum
from fpdf import FPDF class PDF(FPDF): def header(self): # Setting font: helvetica bold 15 self.set_font("helvetica", "B", 15) # Calculating width of title and setting cursor position: width = self.get_string_width(self.title) + 6 # set_x, sets the position of cursor on the x axis self.set_x((210 - width) / 2) # Setting colors for frame, background and text: # setting the color for border self.set_draw_color(0, 80, 180) # setting the color for background, used to fill self.set_fill_color(230, 230, 0) # setting the color for text self.set_text_color(220, 50, 50) # Setting thickness of the frame (1 mm) self.set_line_width(1) # Printing title: self.cell( width, 9, self.title, border=1, new_x="LMARGIN", new_y="NEXT", align="C", fill=True, ) # Performing a line break: self.ln(10) def footer(self): # Setting position at 1.5 cm from bottom: self.set_y(-15) # Setting font: helvetica italic 8 self.set_font("helvetica", "I", 8) # Setting text color to gray: self.set_text_color(128) # Printing page number self.cell(0, 10, f"Page {self.page_no()}", align="C") def chapter_title(self, num, label): # Setting font: helvetica 12 self.set_font("helvetica", "", 12) # Setting background color self.set_fill_color(200, 220, 255) # Printing chapter name: self.cell( 0, 6, f"Chapter {num} : {label}", new_x="LMARGIN", new_y="NEXT", align="L", fill=True, ) # Performing a line break: self.ln(4) def chapter_body(self, filepath): # Reading text file: with open(filepath, "rb") as fh: txt = fh.read().decode("latin-1") # Setting font: Times 12 self.set_font("Times", size=12) # Printing justified text: # Printing a paragraph self.multi_cell(0, 5, txt) # Performing a line break: self.ln() # Final mention in italics: 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")
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
# because we want to read data in CSV format import csv from fpdf import FPDF from fpdf.fonts import FontFace # Open the countries filr and read the data in csv format. with open("countries.txt", encoding="utf8") as csv_file: # read every single line from the file and save it into a list # each item in the list represents the row data = list(csv.reader(csv_file, delimiter=",")) pdf = FPDF() pdf.set_font("helvetica", size=14) # Code for creating the Basic table: pdf.add_page() with pdf.table() as table: # Get every single row from the above data for data_row in data: row = table.row() # Get every single cell fromm the above data for datum in data_row: # to the table's row's cell, add that cell of data row.cell(datum) # Code for creating the Styled table: 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")
from fpdf import FPDF pdf = FPDF() # First page: 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() # Second page: pdf.add_page() pdf.image( "demo.png", 10, 10, 50, 0, "", "<https://pyfpdf.github.io/fpdf2/>" ) 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://pyfpdf.github.io/fpdf2/>"><https://pyfpdf.github.io/fpdf2/></a>, or on an image: the logo is clickable!""" ) pdf.output("tuto6.pdf")