Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep_aspect_ratio support for images in templates #1118

Open
Clock-Speed opened this issue Feb 21, 2024 · 7 comments
Open

keep_aspect_ratio support for images in templates #1118

Clock-Speed opened this issue Feb 21, 2024 · 7 comments

Comments

@Clock-Speed
Copy link

Clock-Speed commented Feb 21, 2024

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
Copy link
Member

Lucas-C commented Feb 21, 2024

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
Copy link
Author

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
Copy link
Member

Lucas-C commented Feb 21, 2024

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
Copy link
Author

Clock-Speed commented Feb 21, 2024

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
Copy link
Member

Lucas-C commented Feb 22, 2024

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
Copy link
Author

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

@Lucas-C
Copy link
Member

Lucas-C commented Aug 20, 2024

PR #1119 has been closed.

This issue is up-for-grabs! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants