我有一组.png格式的图片,需要将它们转换为一个pdf文件,每页9张图片,但不是按垂直顺序依次排列,而是横向填充整个页面宽度,然后再继续下一行。每次图片数量可能不同(如12、15张等)。
我尝试使用了fpdf工具。
但两者都将每张图片放在另一张下面。
我尝试使用了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()
但两者都将每张图片放在另一张下面。