Kozea / WeasyPrint

The awesome document factory
https://weasyprint.org
BSD 3-Clause "New" or "Revised" License
7.12k stars 682 forks source link

Is it possible that weasyprint doesn't correctly load css files, specified in the html file, inside a docker container? #1528

Closed danielmoessner closed 2 years ago

danielmoessner commented 2 years ago

Using weasyprint 53.4 inside a django project

I've had the following code, which failed in production inside a docker container: html

{% load static %}
<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <title>Leistungsverzeichnis</title>
    <link rel="stylesheet" href="{% static 'pdf.css' %}">
    <style>
        @page {
            size: A4 landscape;
        }
    </style>
</head>
<body>
{% include 'search/description/content.html' %}
</body>
</html>

python: BACKEND_URL looks like https://api.example.com

def render_to_response(self, context, **response_kwargs):
        rendered = render_to_string(self.template_name, context)
        name = self.object.name
        html = HTML(string=rendered, base_url=settings.BACKEND_URL)
        pdf = html.write_pdf()
        response = HttpResponse(pdf)
        response['Content-Type'] = 'application/pdf'
        response['Content-Disposition'] = 'inline; filename="{}.pdf"'.format(name)
        return response

and the styles were applied correctly locally in development mode, but it all failed once i've put it inside a docker container and deployed.

Now this always works:

def generate_pdf(self):
        rendered = self.get_html()
        css = CSS(os.path.join(settings.BASE_DIR, 'static/dist/pdf.css'))
        html = HTML(string=rendered)
        pdf = html.write_pdf(stylesheets=[css])
        pdf_file = io.BytesIO(pdf)
        return pdf_file

Might there be a reason why the css doesn't load inside a docker container?

I've thought it might have to do with ALLOWED_HOSTS in django, because weasyprint might make a request to the application itself to get the css file, but that doesn't seem to be the case. I don't know how weasyprint loads the css files within the html file.

I'm curious on what's going on here.

liZe commented 2 years ago

Hello!

The best way to know what’s going on is to use logging: you’ll know why WeasyPrint doesn’t load the CSS file.

And if you use Django with WeasyPrint, you can try Django-WeasyPrint!

liZe commented 2 years ago

@danielmoessner Did you find the time to try the logging module?

danielmoessner commented 2 years ago

Sorry, I haven't tried it again yet. Not sure when I'll find the time, as for the moment it works with CSS(). I'll close this issue for now and reopen as soon as I find out more.

ethernalarts commented 1 year ago

@liZe thanks for your insight. With logging i was ablel to finally see why weasyprint wasn't applying my tailwindcss stylesheet to my pdf output file. I have attached the log file incase you have any solution steps to offer. Thanks

weasyprint.log

liZe commented 1 year ago

Hi @ethernalarts,

According to your logs, the main problems are:

(The warnings are quite self-explanatory, aren’t they?)

ethernalarts commented 1 year ago

Hi @ethernalarts,

According to your logs, the main problems are:

(The warnings are quite self-explanatory, aren’t they?)

Hello @liZe ,

Thank you for your prompt response and links. Yes, i guess they are quite self-explanatory. The volume of errors can be overwhelming at times. I'll check out your links and get back to you. Once again thank you.