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
The first thing you do is create the FPDF() object and set the source directory where you have the images stored. sdir is that variable. You also need to set two variables to capture the height and width of the images respectively.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With