Is there any practical way to create a PDF from a list of images files, using Python?
In Perl I know that module. With it I can create a PDF in just 3 lines:
use PDF::FromImage;
...
my $pdf = PDF::FromImage->new;
$pdf->load_images(@allPagesDir);
$pdf->write_file($bookName . '.pdf');
I need to do something very similar to this, but in Python. I know the pyPdf module, but I would like something simple.
@Edit
If you came through Google, here's the code:
from fpdf import FPDF
from PIL import Image
def makePdf(pdfFileName, listPages, dir = ''):
if (dir):
dir += "/"
cover = Image.open(dir + str(listPages[0]) + ".jpg")
width, height = cover.size
pdf = FPDF(unit = "pt", format = [width, height])
for page in listPages:
pdf.add_page()
pdf.image(dir + str(page) + ".jpg", 0, 0)
pdf.output(dir + pdfFileName + ".pdf", "F")
Install FPDF for Python:
pip install fpdf
Now you can use the same logic:
from fpdf import FPDF
pdf = FPDF()
# imagelist is the list with all image filenames
for image in imagelist:
pdf.add_page()
pdf.image(image,x,y,w,h)
pdf.output("yourfile.pdf", "F")
You can find more info at the tutorial page or the official documentation.
Thank you, but I could not success. The images I'm working on are JPG; the FDPF not have native support for JPG. For this, need the PIL. As the PIL has no more support for Python 3, I installed PILLOW. However, the FDPF apparently does not recognize: "PIL not installed". For testing purposes, I tested with PNG images, however, results in the following error: "Not a PNG file: 0.png"
For A4 size paged pdfs, the value for w and h is 210 and 297, respectively.
The issue I encountered with this approach is that every other page is blank for some reason.
Can you tell what are the values for x,y,w and h ?
for standard (a4) pages, what should the values be for x,y,w,h? edit: looks like 0,0,210,297 works