繁体   English   中英

png图像到python中的一个pdf

[英]png images to one pdf in python

我有一个.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文档的更多信息: 图像

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM