fdemmer / django-weasyprint

A Django class-based view generating PDF resposes using WeasyPrint
Other
344 stars 53 forks source link

@font-face declared fonts not embedded in pdf #54

Closed oliverbienert closed 2 years ago

oliverbienert commented 3 years ago

Hello, I recently started using django-weasyprint in a project to produce nice pdf-reports. However, the fonts I declared in my css using @font-face rules were not embedded in the final pdf. This is probably due to a missing font_config attribute when django-weasyprint instanciates css objects. According to weasyprint docs, you should declare the font-config attribute also on css instance when using font-face rules. I tested it by writing a function view that directly uses weasyprint. So I downloaded the latest stable version of django-weasyprint and fixed it locally. I'll create a pull request.

The output of pdffonts command before the fix:

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
QHAAMN+DejaVuSans-Bold               TrueType          WinAnsi          yes yes yes      6  0
AYDOZU+DejaVuSans-ExtraLight         TrueType          WinAnsi          yes yes yes      7  0
UJBTBB+DejaVuSans                    TrueType          WinAnsi          yes yes yes      8  0

and after the fix:

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
BQMJZP+FiraSans-Bold                 Type 1C           WinAnsi          yes yes yes      6  0
FTQEDP+FiraSans-Light                Type 1C           WinAnsi          yes yes yes      7  0
KUVOMO+FiraSans-Regular              Type 1C           WinAnsi          yes yes yes      8  0

Regards Oliver

fdemmer commented 2 years ago

Thank you for your pull-request! I agree with @foarsitter and prefer keeping the get_font_config method.

Please check out the solution in the develop branch. Does this work for you?

oliverbienert commented 2 years ago

Sorry for my late response. I checked it out, but now I've the problem that Weasyprint requires pango >= 1.44. I still run Buster, so I am going to dist-upgrade, then come back here.

foarsitter commented 2 years ago

Bullseye will indeed solve the pango issue.

oliverbienert commented 2 years ago

Now running Bullseye and everything looks great so far. Your solution works for me, my fonts are now correctly embedded as shown below:

name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
IUFVIR+FiraSans                      CID Type 0C (OT)  Identity-H       yes yes yes     22  0
DYGETD+FiraSans                      CID Type 0C (OT)  Identity-H       yes yes yes     26  0
BKZRLP+FiraSans                      CID Type 0C (OT)  Identity-H       yes yes yes     30  0

The pdffonts output looks a bit different to the one shown above in my first post, though, as it does not show the font-weight anymore in the name column and the type and encoding columns also differ. But my PDF looks nice, so I don't care. Thank you for your work! Regards Oliver

fdemmer commented 2 years ago

thank you for verifying! fix released...