在python中将png图像转换为一个pdf [英] png images to one pdf in python

查看:2008
本文介绍了在python中将png图像转换为一个pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.png图像列表.我需要将它们全部转换为一个pdf,每页9张图像,而不是将它们垂直垂直放置,而是填满所有宽度,然后再继续下一行. 每次的图片数量可以不同(12,... 15)

我尝试过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")

,还有wkhtmltopdf

template = Template('''<!doctype html>
<html>
<body>
        <div style="display: flex; flex-direction: row">
        {% for image in images %}
            <img src="{{ image }}" />
        {% endfor %}
        </div>
</body>
</html>''')
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()

但他们俩都将每张图片放在另一张图片下

解决方案

只需使用FPDF将每个图像放置在所需的坐标上即可:

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

有关FPDF文档的更多信息:图像

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('''<!doctype html>
<html>
<body>
        <div style="display: flex; flex-direction: row">
        {% for image in images %}
            <img src="{{ image }}" />
        {% endfor %}
        </div>
</body>
</html>''')
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中将png图像转换为一个pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆