python 图片转pdf,在python中将png图像转换为一个pdf

I have a list of .png images. I need to convert all of them into one pdf, 9 images per page , but not to place them one after another vertically, but fill in all the width, and only then continue to next row.

Amount of pictures can be different each time (12, ... 15)

I have tried fpdf

from fpdf import FPDF

list_of_images = [1.png, 2.png, ... 15.png]

w = 70

h = 60

pdf = FPDF(orientation = 'L')

for image in list_of_images:

pdf.image(image, w=sizew, h=sizeh)

pdf.output("Memory_usage.pdf", "F")

and also wkhtmltopdf

template = Template('''

{% for image in images %}

{% endfor %}

''')

list_of_images = [1.png, 2.png, ... 15.png]

html = template.render(images=list_of_pict)

with open("my_new_file.html", "wb") as fh:

fh.write(html)

p = subprocess.Popen(['wkhtmltopdf', '-', 'Memory_usage.pdf'], stdin=subprocess.PIPE, universal_newlines=True)

p.communicate(html)

p.wait()

but both of them place each picture one under another

解决方案

Just place each image at required coordinates using FPDF:

pdf.image(image, x=50, y=100, w=sizew, h=sizeh)

More info on FPDF documentation: image

你可能感兴趣的:(python,图片转pdf)