py-pdf / fpdf2

Simple PDF generation for Python
https://py-pdf.github.io/fpdf2/
GNU Lesser General Public License v3.0
1.12k stars 254 forks source link

`keep_aspect_ratio` support for images in templates #1118

Open Clock-Speed opened 8 months ago

Clock-Speed commented 8 months ago

Hey everyone! Been really liking this project so far but it's missing one feature that would make it awesome for my use case.

I would like the ability to insert images into a template where the images are scaled while maintaining the aspect ratio. This is already possible when adding an image directly to a PDF but the functionality is missing when using templates.

The solution I propose would introduce a new optional boolean field for the templates that allows for the use of the keep_aspect_ratio flag for images. The usage could look like the following example:

from fpdf import FPDF, FlexTemplate
elements = [
    {"name":"image", "type":"I", "x1":0, "y1":0, "x2":50, "y2":50, "keep_aspect_ratio":True,},
    {"name":"label", "type":"T", "x1":0, "y1":52, "x2":50, "y2":57, "text":"Label",},
]
pdf = FPDF()
pdf.add_page()
templ = FlexTemplate(pdf, elements)
temp["image"
templ["label"] = "Offset: 50 / 50 mm"
templ.render(offsetx=50, offsety=50)

Having looked through the code this should be quite simple as the image handler for templates simply calls the image function on the pdf, so the flag can just be passed through. Unfortunately, I do not currently have the time to make the PR myself due to other commitments but would love to see this implemented! Ended up doing the PR myself anyways as the change was quite easy.

Thanks!

Lucas-C commented 8 months ago

Hi @Clock-Speed and welcome!

Thank you very much for the feature proposal, which seems very relevant to me, and for the PR! 👍

Clock-Speed commented 8 months ago

Hey there @Lucas-C thanks for the warm welcome!

I was just wondering, if/when the PR does get merged, how far off do you think it'd be till it is included in a PyPy deployment?

Lucas-C commented 8 months ago

I was just wondering, if/when the PR does get merged, how far off do you think it'd be till it is included in a PyPy deployment?

You mean the delay it will take for this feature to appear in a new fpdf2 release?

Given that we had a release a few days ago, I'd say in 4 to 6 weeks.

Clock-Speed commented 8 months ago

Ah dang, I really wanted to use it in one of my projects, no worries though. Any advice on how to go about making a release for internal use while we wait for it to be included in an actual release?

Lucas-C commented 8 months ago

Ah dang, I really wanted to use it in one of my projects, no worries though. Any advice on how to go about making a release for internal use while we wait for it to be included in an actual release?

You can easily install a version of fpdf2 from any branch on any repo:

pip install git+https://github.com/py-pdf/fpdf2.git@master

So you can even install your PR branch:

pip install git+https://github.com/Clock-Speed/fpdf2.git@template-keep-aspect-ratio
Clock-Speed commented 8 months ago

Oh cool, you learn something new every day! :)

Lucas-C commented 2 months ago

PR https://github.com/py-pdf/fpdf2/pull/1119 has been closed.

This issue is up-for-grabs! 🙂