create pdf with fpdf in python. can not move image to the right in a loop

Viewed 1575

I use FPDF to generate a pdf with python. i have a problem for which i am looking for a solution. in a folder "images", there are pictures that I would like to display each on ONE page. I did that - maybe not elegant. unfortunately i can't move the picture to the right. it looks like pdf_set_y won't work in the loop.

from fpdf import FPDF

from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir('../images') if isfile(join('../images', f))]


pdf = FPDF('L')
pdf.add_page()
pdf.set_font('Arial', 'B', 16)

onlyfiles = ['VI 1.png', 'VI 2.png', 'VI 3.png']
y = 10

for image in onlyfiles:
    pdf.set_x(10)
    pdf.set_y(y)
    pdf.cell(10, 10, 'please help me' + image, 0, 0, 'L')
    y = y + 210 #to got to the next page

    pdf.set_x(120)
    pdf.set_y(50)
    pdf.image('../images/' + image, w=pdf.w/2.0, h=pdf.h/2.0)

pdf.output('problem.pdf', 'F')

Would be great if you have a solution/help for me. Thanks alot greets alex

3 Answers

I see the issue. You want to specify the x and y location in the call to pdf.image(). That assessment is based on the documentation for image here: https://pyfpdf.readthedocs.io/en/latest/reference/image/index.html

So you can instead do this (just showing for loop here):

for image in onlyfiles:
    pdf.set_x(10)
    pdf.set_y(y)
    pdf.cell(10, 10, 'please help me' + image, 0, 0, 'L')
    y = y + 210 # to go to the next page

    # increase `x` from 120 to, say, 150 to move the image to the right
    pdf.image('../images/' + image, x=120, y=50, w=pdf.w/2.0, h=pdf.h/2.0)
    #                        new -> ^^^^^  ^^^^

disclaimer: I am the author of pText the library I will use in this solution.

Let's start by creating an empty Document:

pdf: Document = Document()

# create empty page
page: Page = Page()

# add page to document
pdf.append_page(page)

Next we are going to load an Image using Pillow

import requests
from PIL import Image as PILImage

im = PILImage.open(
        requests.get(
            "https://365psd.com/images/listing/fbe/ups-logo-49752.jpg", stream=True
        ).raw
    )

Now we can create a LayoutElement Image and use its layout method

    Image(im).layout(
        page,
        bounding_box=Rectangle(Decimal(20), Decimal(724), Decimal(64), Decimal(64)),
    )

Keep in mind the origin (in PDF space) is at the lower left corner.

Lastly, we need to store the Document

# attempt to store PDF
with open("output.pdf", "wb") as out_file_handle:
    PDF.dumps(out_file_handle, pdf)

You can obtain pText either on GitHub, or using PyPi There are a ton more examples, check them out to find out more about working with images.

You can check pdfme library. It's the most powerful library in python to create PDF documents. You can add urls, footnotes, headers and footers, tables, anything you would need in a PDF document.

The only problem I see is that currently pdfme only supports jpg format for images. But if that's not a problem it will help you with your task.

Check the docs here.

Related