Closed GitRon closed 2 years ago
Hi Ron, sorry for the late response, I hope you figured out a solution by now...
In any case, your first problem is "url resolver does not try to load the fonts". You should definitely see requests to fetch /static/fonts/MyFont-Regular.ttf
from where your server is running. I can't say why not from what you posted.
I definitely recomment using the static
template tag if those files are managed as static files with Django, for example:
@font-face {
font-family: "Fira Sans";
src: url({% static "fonts/fira/ttf/FiraSans-Regular.ttf" %}) format('truetype');
}
As for prefixing with file:
; that would bypass fetching the files via HTTP and try to load them from the disk. I would not recommend doing that. It's harder to debug, there is no logging, you already have a HTTP server serving the PDF and CSS etc, why not use it for the fonts?
If you insist make sure to use all the required slashes, e.g.:
@font-face {
font-family: "Fira Sans";
src: url(file://{% static "fonts/fira/ttf/FiraSans-Regular.ttf" %}) format('truetype');
}
Or better yet, leave the HTML as is and set WEASYPRINT_BASEURL = '/'
.
A file that would normally be served at /static/fonts/fira/ttf/FiraSans-Regular.ttf
should then have this absolute path: file:///static/fonts/fira/ttf/FiraSans-Regular.ttf
Final important note on font files: While investigating this I tried WeasyPrint 52 and >= 53 with various formats, support varies due to the libraries WeasyPrint uses underneath:
Sidenote: >= 53 does not apply "faux styling" to achieve bold or italic. Make sure you define font-faces providing the styles/weights, e.g.:
@font-face {
font-family: "Fira Sans";
src: url({% static "fonts/fira/ttf/FiraSans-Regular.ttf" %}) format('truetype');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: "Fira Sans";
src: url({% static "fonts/fira/ttf/FiraSans-Bold.ttf" %}) format('truetype');
font-weight: bold;
font-style: normal;
}
@fdemmer Thanks for the reply! I guess the path or the update did the trick. 👍 👍
Just being curious: I am using a CSS file for my styles, how did you integrate the static helper there?
Just being curious: I am using a CSS file for my styles, how did you integrate the static helper there?
I don't :(
My examples assume that CSS would be in a template's <style>
tag. Haven't looked into it in detail, but you could generate CSS or at least one with CSS variables with a template, e.g. this stackoverflow.
Nevertheless, your solution with the file:///static/...
works 👍
Hi there,
I am using the
WeasyTemplateResponseMixin
and trying to get a custom font to load. The font files are located in my django statics dir.Here's my code:
SCSS:
View:
Hints:
file:
Any ideas what I might be doing wrong? Unfortunately this feature is not covered in the docs AFAIK 😕
Thx! Ronny