Python Add Images To PDF Documents

PDF

In this example I am going to show you how to add images to PDF documents or files. you may need to add or include images into pdf files along with text information. There are a number of advantages while using pdf as a document for various purposes, such as:

  • PDF format reports allows professionals to edit, share, collaborate and ensure the security of the content within digital documents.
  • Reports are mostly generated in PDF format because a PDF file is a “read only” document that cannot be altered without leaving an electronic footprint whereas other formats like image, word, excel etc. can easily be altered without leaving an electronic footprint.
  • PDF files are compatible across multiple platforms whereas a word document may not be viewable easily in Mac system.

Prerequisites

Python 3.9.1, fpdf (pip install fpdf), Pillow (pip install pillow)

Add Images to PDF

Here I am going to show you how to add images to pdf file. Here I am going to show how to add a single image as well as multiple images to pdf files.

The function which is used to add an image to pdf document is given below:

fpdf.image(name, x = None, y = None, w = 0, h = 0, type = '', link = '')

Parameters

name:

Path or URL of the image.

x:

Abscissa of the upper-left corner. If not specified or equal to None, the current abscissa is used (version 1.7.1 and up).

y:

Ordinate of the upper-left corner. If not specified or equal to None, the current ordinate is used; moreover, a page break is triggered first if necessary (in case automatic page breaking is enabled) and, after the call, the current ordinate is moved to the bottom of the image (version 1.7.1 and up).

w:

Width of the image in the page. If not specified or equal to zero, it is automatically calculated.

h:

Height of the image in the page. If not specified or equal to zero, it is automatically calculated.

type:

Image format. Possible values are (case insensitive): JPG, JPEG, PNG and GIF. If not specified, the type is inferred from the file extension.

link:

URL or identifier returned by add_link.

In the following Python program I am reading images from the current directory and adding image to the pdf file.

As a first step you need to add a page to the pdf document before you add any text or image to the pdf document.

If you want to add each image to a single pdf page then for each image you need to add a new page otherwise the pdf page will be automatically added while images are being added one by one.

While I am reading files from the current directory I am also validating the image files by their extension (.png or .jpg or .jpeg).

import os
from fpdf import FPDF
#from PIL import Image

#pdf = FPDF(orientation = 'L')
pdf = FPDF()
pdf.add_page() #add a page first

#i = Image.open('flower.png')
#i_name = os.path.basename('flower.png')
#w, h = i.size
pdf.image('flower.png')
pdf.output("image.pdf", "F")

dirs = os.listdir('.')

pdf1 = FPDF()
#pdf1.add_page()
for img in dirs:
    if os.path.isfile(img) and os.path.splitext(img)[1].lower() in ('.jpg', '.jpeg', '.png'):
        #print(img)
        pdf1.add_page()
        pdf1.image(img)

pdf1.output("images.pdf", "F")

Test adding Images to PDF

Once you execute the above python script, the images will be added to your PDF document:

add images to pdf

Source Code

Download

Leave a Reply

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