Forums

Django-weasyprint uses correct font on local but not on PAW.

Hi there,

I'm a bit lost here. I've looked at all the font/weasyprint related things I can find in these forums but I feel a bit out of my depth.

I'm trying to use Roboto in my WeasyTemplateView, which I have simply set in blog.css:

body {
font-family: 'Roboto', sans-serif !important;
}

I've run python manage.py collectstatic a few times along with fc-list to make sure that Roboto is present, which it is. I've also loaded the font in the html template:

<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">

None of these things have helped. I'm getting some kind of default, serif style font instead, only on PAW.

Does anyone have any advice for me? I've looked at https://weasyprint.readthedocs.io/en/stable/features.html#fonts and quite honestly have no idea what I'm looking at haha.

PS: Not really sure what information I could give or not give to help, but if you'd like to have a poke around, all of my code is sitting here - the bugged view is "ExportPDF" and the template is export.html in the "online_cv" app_dir: https://github.com/Kirkmania/Y2-Summer-Project-Blog

That's interesting -- it sound like Weasyprint isn't loading the font from the link, which does surprise me a bit. However, if you download the TTF version of Roboto then you can install it into your PythonAnywhere account.

Yeah I tried that with Segoe UI as that was the default bootstrap font being used. Tried it again with the roboto ttfs, and sure enough, fc-cache -f -v gives the result /home/Kirkmania/.fonts: caching, new cache contents: 16 fonts, 0 dirs, but WeasyPrint still isn't using it. I don't know what else to do! Maybe I need to explore the WeasyPrint code and see what I can understand.

Okay, I've just been reading this forum topic on WeasyPrint and it looks like the Cairo library might be the problem:

UserWarning: There are known rendering problems and missing features with cairo < 1.15.4.

It looks like 1.14.6-1 is still the version that's installed.

ah cool! you can try to upgrade cairo using these instructions then

I actually just checked and all of the package versions are the same as my local venv :( I can't figure out what's wrong.

Very odd. What do you get if you run this in Bash:

fc-list | grep -i roboto

...?

I get a bunch of results!

/home/Kirkmania/.fonts/Roboto/Roboto-Light.ttf: Roboto,Roboto Light:style=Light,Regular /home/Kirkmania/.fonts/Roboto/Roboto-Bold.ttf: Roboto:style=Bold /usr/share/fonts/truetype/roboto/hinted/RobotoCondensed-Regular.ttf: Roboto Condensed:style=Regular /usr/share/fonts/truetype/roboto/hinted/Roboto-Regular.ttf: Roboto:style=Regular /usr/share/fonts/truetype/roboto/hinted/Roboto-ThinItalic.ttf: Roboto:style=Thin Italic /home/Kirkmania/.fonts/Roboto/Roboto-LightItalic.ttf: Roboto,Roboto Light:style=Light Italic,Italic /usr/share/fonts/truetype/roboto/hinted/RobotoCondensed-Bold.ttf: Roboto Condensed:style=Bold /home/Kirkmania/.fonts/Roboto/Roboto-Italic.ttf: Roboto:style=Italic /usr/share/fonts/truetype/roboto/hinted/RobotoCondensed-BoldItalic.ttf: Roboto Condensed:style=Bold Italic /usr/share/fonts/truetype/roboto/hinted/Roboto-BoldItalic.ttf: Roboto:style=Bold Italic /usr/share/fonts/truetype/roboto/hinted/Roboto-Bold.ttf: Roboto:style=Bold /usr/share/fonts/truetype/roboto/hinted/Roboto-Italic.ttf: Roboto:style=Italic /usr/share/fonts/truetype/roboto/hinted/RobotoCondensed-Light.ttf: Roboto Condensed:style=Light /home/Kirkmania/.fonts/Roboto/Roboto-BlackItalic.ttf: Roboto,Roboto Black:style=Black Italic,Italic /usr/share/fonts/truetype/roboto/hinted/Roboto-Light.ttf: Roboto:style=Light /usr/share/fonts/truetype/roboto/hinted/Roboto-Thin.ttf: Roboto:style=Thin /usr/share/fonts/truetype/roboto/hinted/RobotoCondensed-Italic.ttf: Roboto Condensed:style=Italic /home/Kirkmania/.fonts/Roboto/Roboto-ThinItalic.ttf: Roboto,Roboto Thin:style=Thin Italic,Italic /usr/share/fonts/truetype/roboto/hinted/Roboto-Black.ttf: Roboto:style=Black /usr/share/fonts/truetype/roboto/hinted/Roboto-MediumItalic.ttf: Roboto:style=Medium Italic /usr/share/fonts/truetype/roboto/hinted/Roboto-LightItalic.ttf: Roboto:style=Light Italic /usr/share/fonts/truetype/roboto/hinted/Roboto-Medium.ttf: Roboto:style=Medium /home/Kirkmania/.fonts/Roboto/Roboto-BoldItalic.ttf: Roboto:style=Bold Italic /usr/share/fonts/truetype/roboto/hinted/RobotoCondensed-LightItalic.ttf: Roboto Condensed:style=Light Italic /home/Kirkmania/.fonts/Roboto/Roboto-Thin.ttf: Roboto,Roboto Thin:style=Thin,Regular /home/Kirkmania/.fonts/Roboto/Roboto-Medium.ttf: Roboto,Roboto Medium:style=Medium,Regular /usr/share/fonts/truetype/roboto/hinted/Roboto-BlackItalic.ttf: Roboto:style=Black Italic /home/Kirkmania/.fonts/Roboto/Roboto-Black.ttf: Roboto,Roboto Black:style=Black,Regular /home/Kirkmania/.fonts/Roboto/Roboto-MediumItalic.ttf: Roboto,Roboto Medium:style=Medium Italic,Italic /home/Kirkmania/.fonts/Roboto/Roboto-Regular.ttf: Roboto:style=Regular

OK, that's promising -- the fonts are definitely installed where weasyprint should be able to get hold of them. Are there any warning messages appearing in your website's server or error log when you access the view?

Okay, I possibly should have looked at the server/error log earlier, didn't really understand how to do that since usually I just look at the venv terminal on local, but PAW is always running hehe.

I do get some errors and warnings to do with fontconfigs. I get the error about cairo versions < 1.15.4 I pasted earlier, but I get this on local too with no issues for my use case.

I do also get these:

2020-08-27 19:01:38,655:   'There are known rendering problems and missing features with '
2020-08-27 19:01:39,011: Ignored `font-display: swap` at 5:3, descriptor not supported.
2020-08-27 19:01:39,085: Ignored `font-display: swap` at 12:3, descriptor not supported.
2020-08-27 19:01:39,116: Ignored `font-display: swap` at 19:3, descriptor not supported.
2020-08-27 19:01:39,153: Ignored `font-display: swap` at 26:3, descriptor not supported.
2020-08-27 19:01:39,187: Ignored `font-display: swap` at 33:3, descriptor not supported.
2020-08-27 19:01:39,216: Failed to load stylesheet at https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css : URLError: <urlopen error Tunnel connection failed: 403 Forbidden>
2020-08-27 19:01:49,227: Failed to load stylesheet at http://kirkmania.pythonanywhere.com/static/css/blog.css : timeout: timed out

First time I'm seeing these errors anywhere, and they don't show up when I access any other views, despite those same stylesheets being requested in every template I use. I checked the directory to make sure that blog.css is where it's supposed to be in static/css and it definitely is there.

Time to start googling!

EDIT: Okay, looks like I might need to add the bootstrap to a whitelist or store it in the local static directory, trying to figure out how to do this now.

Still not sure about the blog.css. That IS local already.

EDIT2: Alright, so I downloaded bootstrap css and just threw it in my static/css directory, so now I'm just getting two timeout errors instead. Clearly the issue is something to do with how weasyprint gets static files, I'm just not sure how to fix it. Maybe something to do with the url_fetcher? It still works fine on local which confuses me the most.

Could you share here your current errors?

Yeah, it's almost the same as the ones I've posted before, except I stored the bootstrap css in local static. Now, I just get two timeout errors:

2020-08-28 16:49:48,401: Failed to load stylesheet at http://kirkmania.pythonanywhere.com/static/css/boostrap4.5.2.css : timeout: timed out
2020-08-28 16:49:58,431: Failed to load stylesheet at http://kirkmania.pythonanywhere.com/static/css/blog.css : timeout: timed out

I also get the weasyprint warning about old Cairo versions, but I have that same warning on local which still works fine - I think my use case doesn't use any of the "new" features.

2020-08-27 23:37:32,272: /home/Kirkmania/.virtualenvs/kirkmania.pythonanywhere.com/lib/python3.6/site-packages/weasyprint/document.py:36: UserWarning: There are known rendering problems and missing features with cairo < 1.15.4. WeasyPrint may work with older versions, but please read the note about the needed cairo version on the "Install" page of the documentation before reporting bugs. http://weasyprint.readthedocs.io/en/latest/install.html

Accessing web resources in a free account needs to be done through the proxy. You will need to check the docs weasyprint and django-weasyprint to see how to configure it to use a proxy to access resources on the web. The details that you need to configure it with are here: https://help.pythonanywhere.com/pages/403ForbiddenError/