Google Web Fonts and PDF generation from HTML with wkhtmltopdf

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

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 Fonts to give users the possibility to customize the appearence of the document they edited, offering them the possibility to choose between a few fonts. It also works perfectly in a browser.

Problem is, I don't get the Google Fonts working when converting such HTML files to PDF with wkhtmltopdf. I read other people had the same issue.

Could anyone please help me fixing this?

EDIT: declaring @font-face directly in the CSS does not work either.

回答1:

To convert HTML to PDF by wkhtmltopdf try to avoid woff font face. Use the truetype format of the Google Web Fonts with base64 encode.

Recently I tried to use a Google web font from Google Web Fonts. In the browser it shows correctly but it doesn't show after converting HTML to PDF.

After searching the web extensively, at last, I found tools to encode fonts to the base64 format and also got CSS for @font-face.

Read the solution here.



回答2:

A simple solution: Base64-encode your font and embed it in the CSS:

@font-face {     font-family: 'OpenSans';     src: url(data:font/truetype;charset=utf-8;base64,AAEAAAATAQA... } 

FontSquirrel can do this for you through the advanced options of their Webfont Generator; Google and other fonts can be encoded with this tool.

I've used this solution with pdfkit and wicked_pdf, which work through wkhtmltopdf, so I presume this should work with the native wkhtmltopdf as well.



回答3:

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.



回答4:

I've just tested that using the @importnotation works:

I'm using django-wkhtmltopdf version 2.0.3



回答5:

I must have tried a gazillion variations to get this work (from local). I am trying to get Open Sans Condensed embedded into a pdf via WKHtmlToPdf.

I think it is important that you download and install the Open Sans Condensed ttf directly from google and install it. It is also important to reboot to allow other services access after install. I downloaded the ttf from font-squirrel originally and condensed was listed as "Open Sans" in windows/fonts, which is wrong, if you look after the google install it is listed as "Open Sans Condensed Light" so even google's local('Open Sans Cond Light') script is wrong.

As mentioned before you need to be explicit with the stretch and weights, so this is what has worked for me:

    @font-face {       font-family: 'Open Sans';       font-style: normal;       font-weight: 400;       src: local('Open Sans');     }      @font-face {       font-family: 'Open Sans Condensed';       font-stretch:condensed;       font-style: normal;       font-weight: 300;       src: local('Open Sans Condensed Light');     }  @@font-face { font-family: 'Open Sans Condensed Bold'; font-stretch:condensed; font-style: normal; font-weight: 700; src: local('Open Sans Condensed'), local('Open Sans Condensed Bold');} 


回答6:

Make sure you're not declaring what font you are printing with in your print stylesheet.



回答7:

I've been struggling with this for about two days now, my advice to you if none of the answers above work for you:

Install the font (ttf) in the machine that hosts the webserver and just require it in the css just like you would do with a common font.

body{     font-family: 'Lato'; } 

Now wkhtmltopdf should be able to include the font



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!