Google Web Fonts and PDF generation from HTML with wkhtmltopdf

前端 未结 9 2152
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 18:58

I am using wkhtmltopdf to convert HTML files in PDF format; it gives surprisingly good results, rendering the PDF exactly as WebKit would do.

I am using Google Web F

9条回答
  •  萌比男神i
    2020-11-30 19:27

    I had this same problem and solved it by downloading the fonts and running it off of a local file in my web server with this font-face declaration:

    @font-face {
        font-family: 'PT Sans';
        src: url( '/public/inc/fonts/PTS55F-webfont.eot');
        src: url( '/public/inc/fonts/PTS55F-webfont.eot?#iefix') format('embedded-opentype'),
             url( '/public/inc/fonts/PTS55F-webfont.woff') format('woff'),
             url( '/public/inc/fonts/PTS55F-webfont.ttf') format('truetype');
        font-weight: normal;
        font-style: normal;
    }
    

    I was able to find this font on Font Squirrel, but your mileage may vary.

    I think what happens with the fonts off of Google is that it's trying to load only the format that it thinks this browser wants, which for WebKit is woff (I believe). But wkhtmltopdf can't embed a woff font in a PDF, and so defaults back to sans-serif. By declaring all of them, the TrueType font is included, which is what the PDF actually uses.

    Also, you need to define the font you want used as the first one in any font-family CSS definition or wkhtmltopdf will just ignore it.

提交回复
热议问题