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

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

Closed
danielmoessner opened this issue Dec 24, 2021 · 6 comments

Comments

@danielmoessner
Copy link

danielmoessner commented Dec 24, 2021

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

liZe commented Dec 27, 2021

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

liZe commented Jan 3, 2022

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

@danielmoessner
Copy link
Author

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

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

liZe commented May 7, 2023

Hi @ethernalarts,

According to your logs, the main problems are:

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

@ethernalarts
Copy link

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.

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

No branches or pull requests

3 participants