WhyNotHugo / django-renderpdf

📄 Django app to render django templates as PDF files.
https://django-renderpdf.readthedocs.io/
ISC License
67 stars 7 forks source link

Allow / Enable presentation hints #38

Open jm-lamotte opened 2 years ago

jm-lamotte commented 2 years ago

Hello, Thanks for the code, it allowed me to very quickly render PDF in Django. One thing I could not do with your latest release is to resize images in a PDF. I looked around, and found the presentation_hints parameter that can be passed in the "HTML" or "write_pdf". I updated the last lines of renders.py file from

html = select_template(template).render(context)
HTML(string=html, base_url="not-used://", url_fetcher=url_fetcher,).write_pdf(
    target=file_,
)

to

html = select_template(template).render(context)
HTML(string=html, base_url="not-used://", url_fetcher=url_fetcher,).write_pdf(
    target=file_, presentation_hints = True,
) 

And now I can use width / height to resize the images that are included in my template.

I'll leave it to you to update or add another setting somewhere to allow this.

Thanks again.

WhyNotHugo commented 1 year ago

I don't see the presentation_hints argument in the weasyprint docs. Is this still required, or is it fixed?

Usually, setting the image via width="X" or via CSS should work fine, did that not work for you?

jm-lamotte commented 1 year ago

Hello, thanks for the feedback. It is actually presentationAL_hints (https://doc.courtbouillon.org/weasyprint/stable/api_reference.html).

My html file had the width setting on the images, but when rendering the PDF, it would not work (images were the original size, and out of bounds). So I found this setting, and changed my "renders.py" file. This https://stackoverflow.com/questions/48988707/pdf-output-using-weasyprint-not-showing-images-django allowed me to find the setting to add.

So I'm good with my edit to the code, only have to remember this if I upgrade django-renderpdf. Up to you to add a setting in renderpdf to activate presentational_hints in weasyprint.

Thanks again.

WhyNotHugo commented 1 year ago

So without presentation_hints = True images ignore the width= attribute?

Lemme experiment with this a bit, but it seems like an addition that would make sense. Though I think I have some logos in some documents where I specify the size and it works, so want to double check what's up there.