Converting images to PDF. Final file has blank pages before and after.

Kushal Kumaran kushal at locationd.net
Mon Nov 30 21:24:59 EST 2020


On Mon, Nov 30 2020 at 05:20:30 PM, Michael Baca <jesterdev at gmail.com> wrote:
> Hello, new to the group, rather new to programming. 
>
> I'm writing a program that takes images and converts them into PDF's. It works after quite a few days of trying, however the final file has a blank page inserted before and after each page containing the images. 
>
> This uses FPDF to do the conversion. I've been up and down trying to figure out where I'm adding an extra page, so it might be an FPDF issue. 
>
> def multi_convert(pdf_Filename, file_path):
>     if (dir):
>         file_list = []
>         print(""), print("")
>         print("Converting... This may take awhile depending on the number of images.")
>         
>         for entry in os.scandir(file_path):
>             if (entry.path.endswith(".jpg") or entry.path.endswith(".png")) and entry.is_file():
>                 file_list.append(entry.path)
>     else:
>         print("Error: ")
>         print("Invalid Directory - {}", dir)
>     cover = Image.open(str(file_list[0])) 
>     width, height = cover.size
>
>     pdf = FPDF(unit="pt", format=[width, height])
>
>     for page in file_list:
>         pdf.add_page()

I've never used FPDF, but have you considered not doing the add_page
explicitly here?  The documentation at
https://pyfpdf.readthedocs.io/en/latest/reference/image/index.html might
be useful; see especially the behaviour specified when the "y" parameter
is not specified, as in this case.

>         pdf.image(str(page))
>
>     pdf.output(file_path + pdf_Filename + ".pdf", "F")
>     exit()

If you're unable to resolve the issue, you should report it to pyfpdf at
https://github.com/reingart/pyfpdf/issues (after checking if this is a
known problem).

-- 
regards,
kushal


More information about the Python-list mailing list