Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • PDF Invoice Generator
Menu

Python 3 FPDF Library Script to Add Text & Images to PDF Document & Save it as Output

Posted on May 16, 2023

 

 

To add text and images to a PDF document and save it as output using the Python 3 fpdf library, you can follow these steps:

 

 

Step 1: Install the fpdf library using pip.

 

 

pip install fpdf

 

 

Step 3: Create a custom class that inherits from the FPDF class.

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fpdf import FPDF
 
class PDF(FPDF):
    def header(self):
        # Add header content if needed
        pass
 
    def footer(self):
        # Add footer content if needed
        pass
 
    def chapter_title(self, title):
        # Add chapter title
        self.set_font('Arial', 'B', 16)
        self.cell(0, 10, title, ln=True, align='C')
        self.ln(10)
 
    def chapter_body(self, content):
        # Add chapter body content
        self.set_font('Arial', '', 12)
        self.multi_cell(0, 10, content)
        self.ln(10)
 
    def add_image(self, image_path, x, y, width, height):
        # Add image to the PDF
        self.image(image_path, x, y, width, height)
 
    def add_text(self, text):
        # Add text to the PDF
        self.set_font('Arial', '', 12)
        self.multi_cell(0, 10, text)
        self.ln(10)

 

 

As you can see we have defined all the methods inside the class to add the text and images inside the pdf document with formatting. Now we need to create a new object of the above class as shown below

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Create a new PDF instance
pdf = PDF()
 
# Set up the PDF document
pdf.set_title('My PDF Document')
pdf.set_author('John Doe')
 
# Add a new page
pdf.add_page()
 
# Add content to the PDF
pdf.chapter_title('Chapter 1: Introduction')
pdf.chapter_body('This is the introduction of the document.')
 
# Add an image to the PDF
pdf.add_image('image.jpg', x=10, y=50, width=100, height=100)
 
# Add more text to the PDF
pdf.add_text('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
pdf.add_text('Pellentesque ac orci eget libero mattis eleifend.')
 
# Save the PDF as output
pdf.output('output.pdf')

 

Recent Posts

  • Node.js Express Project to Remove Background of Images Using Rembg & Formidable Library in Browser
  • Node.js Tutorial to Remove Background From Image Using Rembg & Sharp Library in Command Line
  • Python 3 Flask Project to Remove Background of Multiple Images Using Rembg Library in Browser
  • Python 3 Rembg Library Script to Bulk Process Multiple Images and Remove Background in Command Line
  • Python 3 Rembg Library Script to Remove Background From Image in Command Line
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme