由DOMPDF生成的PDF中的Google Webfonts

Joh*_*nes 5 css dompdf google-webfonts

我在一个页面中使用两个webfonts,我使用dompdf转换为PDF.我在标题中有这个:

<link href='http://fonts.googleapis.com/css?family=Signika:600|Roboto+Condensed' rel='stylesheet' type='text/css'>
Run Code Online (Sandbox Code Playgroud)

然后我在CSS规则中使用它们

body {
  font-family: "Roboto Condensed", sans-serif;
  [ ... ]
}
h1 {
  font-family:'Signika', Arial, Helvetica, sans-serif;
  [ ... ]
}
Run Code Online (Sandbox Code Playgroud)

现在,当我生成PDF时,h1显示为"Signika"字体,但"Roboto Condensed"被Helvetica或其他一些标准的sans-serif字体替换.

如果我打开"预览"文件(即我在PDF生成脚本中包含的php页面),"Roboto Condensed"会按预期显示,但它不会进入PDF.但正如我写的那样,"Signika"就在PDF中,这对我来说有点奇怪.顺便说一句,我也试过直接包括字体-face规则为CSS规则p,div,li等但不会改变任何事情.

有什么建议我可以解决这个问题吗?


编辑/添加:

考虑到这一点,两种字体之间的区别在于Roboto Condensed在其名称中有一个空格.我想知道这是否会导致问题(即dompdf无法处理这样的字体名称)?但只要我从Google服务器获取字体,我就无法改变它.

Joh*_*nes 5

I found the solution myself:

As I had added to my question in an edit, the reason obviously was that the font-family name "Roboto Condensed" contains a space, which dompdf doesn't seem to like.

I downloaded the font, created three versions of it with the font generator on Fontsquirrel and put them on my server, together with this stylesheet:

@font-face {
    font-family: 'roboto_condensedregular';
    src: url('robotocondensed-regular-webfont.woff2') format('woff2'),
         url('robotocondensed-regular-webfont.woff') format('woff'),
         url('RobotoCondensed-Regular.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}
Run Code Online (Sandbox Code Playgroud)

Then, in my CSS rules I used that new font name roboto_condensedregular in font-family: roboto_condensedregular, sans-serif;

Now it works, also in the PDF.

  • 约翰内斯,非常感谢你!4 年后,DomPDF 仍然存在这个问题……真是令人难过。昨天为此浪费了一整天的时间。谢谢你一百万! (2认同)