简体   繁体   English

在 wkhtmltopdf 中使用自定义字体

[英]use custom fonts with wkhtmltopdf

I am trying to use custom fonts in my PDF generated with wkhtmltopdf.我正在尝试在使用 wkhtmltopdf 生成的 PDF 中使用自定义字体。 I read that you can't use google webfonts and that wkhtmltopdf uses truetype .ttf file.我读到你不能使用 google webfonts 并且 wkhtmltopdf 使用 truetype .ttf 文件。 Can anyone confirm that?任何人都可以确认吗? So I downloaded a .ttf file from google webfont and put in on my server, then used the font face:所以我从 google webfont 下载了一个 .ttf 文件并放在我的服务器上,然后使用字体:

    @font-face {
        font-family: Jolly;
        src: url('../fonts/JollyLodger-Regular.ttf') format('truetype');
    }

and font family:和字体系列:

    <style type = "text/css">
        p { font-family: 'Jolly', cursive; }
    </style>

And now the text that is supposed to render with Jolly Lodger font doesn't appear at all, page is blank.现在应该用 Jolly Lodger 字体呈现的文本根本没有出现,页面是空白的。

What am I doing wrong?我究竟做错了什么?

PS: I tried with different .ttf files. PS:我尝试了不同的 .ttf 文件。

Since it is a Google Web font you need not to write @font-face in you style sheet just use following link tag in your source code:由于它是 Google Web 字体,因此您无需在样式表中写入@font-face ,只需在源代码中使用以下链接标签即可:

<link href='http://fonts.googleapis.com/css?family=Jolly+Lodger' rel='stylesheet' type='text/css'>

and

 <style type = "text/css">
    p { font-family: 'Jolly Lodger', cursive; }
</style>

will work.将工作。

By the way, in your code you are defining @font-face family as font-family: Jolly;顺便说一句,在您的代码中,您将@font-face系列定义为font-family: Jolly; and using it as p { font-family: 'Jolly Lodger', cursive; }并将其用作p { font-family: 'Jolly Lodger', cursive; } p { font-family: 'Jolly Lodger', cursive; } that is wrong, because it's mismatching font-family name. p { font-family: 'Jolly Lodger', cursive; }这是错误的,因为它是不匹配的字体系列名称。

I am betting you are calling wkhtmltopdf from a web wrapper using IIS.我敢打赌,您正在使用 IIS 从 Web 包装器调用 wkhtmltopdf。 That's why some folks are saying that it does work for them.这就是为什么有些人说它确实对他们有用。 They are probably calling wkhtmltopdf from the command line, in which case it can resolve relative links in CSS URL()s but if you are calling it from snappy or the like you can get around the problem by specifying a URL like say URL('http://localhost/myfolder/fonts/JollyLodger-Regular.ttf') instead.他们可能从命令行调用 wkhtmltopdf,在这种情况下,它可以解析 CSS URL()s 中的相对链接,但是如果你从 snappy 或类似调用它,你可以通过指定一个 URL 来解决这个问题,比如说URL('http://localhost/myfolder/fonts/JollyLodger-Regular.ttf')代替。

The inability to properly resolve relative links in CSS URL() tags appears to be broken in wkhtmltopdf 0.11.0_rc1 but some earlier versions might work okay.无法正确解析 CSS URL() 标签中的相关链接似乎在 wkhtmltopdf 0.11.0_rc1 中被破坏了,但一些早期版本可能可以正常工作。 The same problem happens with other CSS URL() tags like when calling a background images - resolving the file location is broken.其他 CSS URL() 标签也会发生同样的问题,例如在调用背景图像时 - 解析文件位置已损坏。 Interestingly though, in the case of images, if you use <IMG SRC=> , 0.11.0_rc1 is able to find the file, even if a relative path is provided.有趣的是,在图像的情况下,如果您使用<IMG SRC=> , 0.11.0_rc1 能够找到该文件,即使提供了相对路径。 The problem appears to be limited to URL() tags inside CSS.问题似乎仅限于 CSS 中的 URL() 标签。

EDIT: I found out that if you use file:/// with all forward slashes in the path then it works.编辑:我发现如果你在路径中使用file:///和所有斜杠,那么它就可以工作。 In other words URL('file:///D:/InetPub/wwwroot/myfolder/fonts/JollyLodger-Regular.ttf') should work.换句话说URL('file:///D:/InetPub/wwwroot/myfolder/fonts/JollyLodger-Regular.ttf')应该可以工作。

As stated above by Yardboy we've found that base64 encoding the truetype font in our CSS works well.正如Yardboy上面所说,我们发现在我们的 CSS 中使用 base64 编码 truetype 字体效果很好。 But I wanted to share some additional insight.但我想分享一些额外的见解。

We use 4 custom fonts all uniquely named 'Light' 'Medium' etc..我们使用 4 种自定义字体,所有字体都唯一命名为'Light' 'Medium'等。

I use openssl toolkit to base64 encode with good results.我使用 openssl 工具包进行 base64 编码,效果很好。 But you could alternatively use fontsquirrel .但是您也可以使用fontsquirrel Just be sure to strip out all other font types ( 'woff' 'otf' .. ).请务必删除所有其他字体类型( 'woff' 'otf' .. )。 We found that using truetype exclusively worked mysteriously.我们发现仅使用truetype就神秘莫测。

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在 Windows 中,您应该安装 openssl 并将其添加到路径中

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

or simply use this kind of websites或者干脆使用这种网站

Add to css font src property添加到 css 字体 src 属性

   @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.渲染输出将取决于您的wkhtmltopdf版本和风格。 Because our servers run Debian and I develop on OSX I tested on a VM locally.因为我们的服务器运行 Debian,我在 OSX 上开发,所以我在本地的 VM 上进行了测试。

If you have .ttf file or .woff file then use following syntax如果您有 .ttf 文件或 .woff 文件,请使用以下语法

@font-face {
   font-family: 'Sample Name';
   src: url(/PathToFolderWhereContained/fontName.ttf) format('truetype');
   }

don't use ttf that will not work rather use full name 'truetype' and if you have .woff then use 'woff' in format.不要使用不起作用的ttf ,而是使用全名'truetype' ,如果您有 .woff,则在格式中使用'woff' For my case it worked.对于我的情况,它起作用了。

However the best practice is to use links to Google fonts API that is best if not getting that matching your criteria then use this above technique it will work但是,最佳做法是使用指向 Google fonts API 的链接,如果没有获得与您的条件匹配的最佳链接,则使用上述技术即可

我发现 Arman H. 在这个问题上的解决方案(base64 编码字体)就像一个魅力: Google Web Fonts and PDF generation from HTML with wkhtmltopdf

I'll just add my answer here too, in case someone might need it.我也会在这里添加我的答案,以防有人可能需要它。

I've been using Rotativa which builds upon wkhtmltopdf, and what I ended up using was the following:我一直在使用Rotativa它建立在wkhtmltopdf,以及我最终使用是以下情况:

@font-face {
    font-family: "testfont";
    src: url("/UI/Fonts/609beecf-8d23-4a8c-bbf5-d22ee8db2fc9.woff") format("woff"),
         url("/UI/Fonts/1cd9ef2f-b358-4d39-8628-6481d9e1c8ce.svg#1cd9ef2f-b358-4d39-8628-6481d9e1c8ce") format("svg");
}

...and it seem's Rotativa is picking up the SVG-version. ...似乎 Rotativa 正在选择 SVG 版本。 If I reorder them it still works, but if I remove the SVG-version, it stops working.如果我重新排序它们仍然有效,但如果我删除 SVG 版本,它就会停止工作。

Might be worth a shot.可能值得一试。 Can't find any good documentation on why this is so, however找不到任何关于为什么会这样的好文档,但是

After doing all the workarounds suggested (some which actually worked), I came across a way easier solution:在完成了所有建议的解决方法(一些实际有效)之后,我发现了一个更简单的解决方案:

<style>
@import 'https://fonts.googleapis.com/css?family=Montserrat';
</style>

You can simply use the @import notation to include the font.您可以简单地使用@import符号来包含字体。

I personally had to set the font family on a div (I initially wanted a span )我个人不得不在div上设置字体系列(我最初想要一个span

<style>
@font-face {
    font-family: 'CenturyGothic';
    src: url("fonts/century-gothic.ttf") format('truetype');
}

.title {
    font-family: 'CenturyGothic', sans-serif;
    font-size: 22pt;
}
</style>

...

<div class="title">This is Century Gothic</div>

使用@import 不获取字体,您需要将 .ttf 文件添加到项目中

Just in case nothing has worked out for you yet:以防万一还没有解决任何问题:

Please try the standalone version of wkhtmltopdf instead of installable.请尝试wkhtmltopdf的独立版本而不是可安装版本。

I had a similar issue but its resolved by using:我有一个类似的问题,但它通过使用解决:

https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.mxe-cross-win64.7z https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.mxe-cross-win64.7z

Unzip the file > go to bin folder > run your command.解压缩文件 > 转到 bin 文件夹 > 运行您的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM