【问题标题】:png images to one pdf in python在python中将png图像转换为一个pdf
【发布时间】:2017-04-15 20:16:09
【问题描述】:

我有一个 .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()

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

【问题讨论】:

    标签: python pdf png fpdf


    【解决方案1】:

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

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

    有关 FPDF 文档的更多信息:image

    【讨论】:

      猜你喜欢
      • 2018-04-03
      • 2012-10-16
      • 2020-08-07
      • 2013-12-11
      • 2014-12-23
      • 2022-06-13
      • 2016-09-14
      • 2018-09-04
      • 2011-06-27
      相关资源
      最近更新 更多