0

I'm working on a project and have a need to take some images and combine them with a report. Most of the images I'll be working with are a known size. So I've hacked together the following to pull the images, resize them, and paste them into another image, which ultimatum becomes a pdf. In the below case, I know that 3 images fit nicely on a sheet of paper (I've been doing this manually for almost 10 years)

Later on I'll incorporate this into the report generation scripts, but for now, I'm good here.

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


#read the images
images = ['image1.png', 'image2.png', 'image3.png']
doc_numbers = ['img1', 'img2', 'img3']

new_image = Image.new('RGB',(595, 842), (255, 255, 255))
x = 10
y = 10

draw = ImageDraw.Draw(new_image)

for doc,path in zip(doc_numbers, images):
    image = Image.open(path)
    image = image.resize((450, 240))
    new_image.paste(image, (10, y))
    draw.text((400, (y + 240)), doc, (0, 0, 0))
    y = y + 270

## Save output image to PDF
new_image.save('output.pdf', save_all = True, quality=100)

My issue is we do have some images of unknown size. These can be anything from a small receipt you get at a gas pump to a full-size 8.5 x 11 sheet of paper, it really varies widely but I need the end report to use a minimum numbers of paper and have the receipts fit halfway decently.

Curious if anyone has some insight into how one would go about dealing with such a mess.

The end goal here is to be able to pull in (via a form) the individual information for each receipt, as they're generated. Then at routine intervals, create a report including the receipts, which are filed for future use.

0 Answers0