django-renderpdf icon indicating copy to clipboard operation
django-renderpdf copied to clipboard

Django-renderpdf not compatible with Django S3 storage.

Open mjmare opened this issue 1 year ago • 3 comments

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

mjmare avatar Mar 27 '24 17:03 mjmare

The error is caught silently.

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

WhyNotHugo avatar Mar 31 '24 10:03 WhyNotHugo

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

WhyNotHugo avatar Mar 31 '24 10:03 WhyNotHugo

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

mjmare avatar Apr 14 '24 14:04 mjmare