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

Django-renderpdf not compatible with Django S3 storage. #63

Open mjmare opened 5 months ago

mjmare commented 5 months ago

Hi

I store all my static and media files on DigitalOcean Spaces (=S3 compatible). Normally this works fine. I noticed however that a static image is not rendered in the pdf. I turned on weasyprint logging and got this:

Step 1 - Fetching and parsing HTML - HTML string
Step 2 - Fetching and parsing CSS - CSS string
Step 2 - Fetching and parsing CSS - https://fonts.googleapis.com/css2?family=Exo:ital,wght@0,300;0,500;0,600;1,600&display=swap
Failed to load stylesheet at https://fonts.googleapis.com/css2?family=Exo:ital,wght@0,300;0,500;0,600;1,600&display=swap : AttributeError: 'StaticStorage' object has no attribute 'base_url'
Step 3 - Applying CSS
Ignored `mix-blend-mode:normal` at 1:11, unknown property.
Ignored `fill:#cccccc` at 1:11, unknown property.
Ignored `fill:#999999` at 1:11, unknown property.
Step 4 - Creating formatting structure
Failed to load image at 'https://ams3.digitaloceanspaces.com/cumulus-consult/static/images/logo.png?AWSAccessKeyId=DO007CFECQDF3ZG2KPAL&Signature=3cEjatZklzwowveTRdpBBviEb%2B8%3D&Expires=1711564413': AttributeError: 'StaticStorage' object has no attribute 'base_url'
Step 5 - Creating layout - Page 1
Step 6 - Creating PDF

The offending line is in helpers.py.:

 if url.startswith(staticfiles_storage.base_url):

base_url is not a valid property of the storage (which I have configured as S3Storage). The error is caught silently.

I was not able to work around this problem (tried a custom url_fetcher), so in the end I just embedded an SVG in the page.

TIA

WhyNotHugo commented 5 months ago

The error is caught silently.

Yeah, weasyprint silences all errors, which can be a bit annoying to debug.

WhyNotHugo commented 5 months ago

Can you try https://github.com/WhyNotHugo/django-renderpdf/pull/64 ?

mjmare commented 5 months ago

Sorry, don't know how to apply this change without a new release. I'm just a simple user.