use custom fonts with wkhtmltopdf

前端 未结 9 1455
难免孤独
难免孤独 2020-12-03 01:12

I am trying to use custom fonts in my PDF generated with wkhtmltopdf. I read that you can\'t use google webfonts and that wkhtmltopdf uses truetype .ttf file. Can anyone con

9条回答
  •  一向
    一向 (楼主)
    2020-12-03 01:49

    As stated above by Yardboy we've found that base64 encoding the truetype font in our CSS works well. But I wanted to share some additional insight.

    We use 4 custom fonts all uniquely named 'Light' 'Medium' etc..

    I use openssl toolkit to base64 encode with good results. But you could alternatively use fontsquirrel. Just be sure to strip out all other font types ('woff' 'otf' .. ). We found that using truetype exclusively worked mysteriously.

    Encode file, trim output and add to clipboard

    openssl base64 -in bold.ttf | tr -d '\n' | pbcopy
    

    In Windows you should install openssl and add it to path then

    openssl base64 -in bold.ttf | tr -d '\n' | clip
    

    or simply use this kind of websites

    Add to css font src property

       @font-face {
          font-family: 'Bold';
          font-style: normal;
          font-weight: normal;
          src: url(data:font/truetype;charset=utf-8;base64,BASE64...) format("truetype");
        }
    

    Rendered output will depend on your wkhtmltopdf version and flavor. Because our servers run Debian and I develop on OSX I tested on a VM locally.

提交回复
热议问题