Java, wkhtmltopdf,HTML转PDF并不是所有字体都能正常工作。

huangapple go评论66阅读模式
英文:

Java ,wkhtmltopdf, HTML to PDF not all fonts works correctly

问题

我正在使用wkhtmltopdf将HTML(字符串而非文件)生成PDF。
在开始创建PDF之前,我会将所有字体添加到HTML文件中。

htmlTemplate = htmlTemplate.replaceAll("${fontsPlaceholder}", ResourcesCache.getInstance().getFontsCSSCache());

所有字体都在HTML内部,并且看起来像这样:

@font-face {
        font-family: 'Abril_Fatface-Regular';
        src: url(data:font/ttf;base64,AAEAA....

但是当我尝试使用字体属性,如粗体、斜体等,然后生成PDF时,结果不正确。PDF中的字段使用“regular”字体,而在HTML中设置为粗体。

那么,为什么并非所有字体在wkhtmltopdf中都能正常工作?是否有人解决过类似的问题?

英文:

I am using wkhtmltopdf to generate PDF from HTML (string not file).
Before I start creating PDF, I add all fonts to HTML file

htmlTemplate = htmlTemplate.replaceAll("\$\\{fontsPlaceholder}", ResourcesCache.getInstance().getFontsCSSCache());

and all fonts are inside of html, and look like

@font-face {
        font-family: 'Abril_Fatface-Regular';
        src: url(data:font/ttf;base64,AAEAA....

But when I tried to use font properties, bold, italic etc, and then make the pdf, this is not working correctly, and field use 'regular' font in pdf, but in html set bold..

So, why not all fonts working good in wkhtmltopdf, does someone fixed issue like this?

答案1

得分: 1

我已经解决了这个问题。问题出在字体上,而不是在wkhtmltopdf库上。
如果您想要正确使用font属性,您需要确保在您的字体中设置了“Preferred Family”。要检查或设置此项,您可以使用FontForge应用程序。
打开FontForge,然后导入您的字体,点击“Element/FontInfo/TTF Names”,并更改“Preferred Family”(每个字体都需要设置为唯一值)。Java, wkhtmltopdf,HTML转PDF并不是所有字体都能正常工作。

英文:

I have been solve this issue. The problem was with fonts, not with wkhtmltopdf lib.
If you want to use font-property correct, you need to be sure that 'Preferred Family' is set in your font. To check or to set this you can with FontForge app.
Open FontForge, then import your font, and click 'Element/FontInfo/TTF Names', and change 'Preferred Family' (it's need to be unique for each font).Java, wkhtmltopdf,HTML转PDF并不是所有字体都能正常工作。

huangapple
  • 本文由 发表于 2020年7月27日 21:53:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63116856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定