Search code examples
javabackendwkhtmltopdfhtml-to-pdf

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


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?


Solution

  • 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).https://i.sstatic.net/AQhhq.png