I'm trying to convert an html to pdf using flying saucer pdf library, but I have an issue with the font I should have. It seems that the font imported is not recognized.
Below my html code:
<html>
<head>
<meta charset="utf-8" />
<title>My card - v_prenom v_nom</title>
<style>
p {
margin: 0;
font-family:'Poppins',Arial,Helvetica,sans-serif !important;
}
</style>
<link href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900%26display=swap" rel="stylesheet" />
</head>
<body>
...
<td style="text-align:center;">
<p style="font-size:10px"> <strong>Card<br/>Test</strong></p>
</td>
...
</body>
...
And my java code:
outputStream = new FileOutputStream(outputFolder);
ITextRenderer renderer = new ITextRenderer();
FileReader fr=new FileReader(path+data_in+"template.html");
BufferedReader br= new BufferedReader(fr);
StringBuilder content=new StringBuilder(1024);
String s;
while((s=br.readLine())!=null) {
content.append(s);
}
String html_string = content.toString();
renderer.setDocumentFromString(html_string);
renderer.layout();
renderer.createPDF(outputStream);
outputStream.close();
The result pdf is generated but not using the font that I wanted, any idea of where the problem can come ?
Thank you.
Flying-saucer doesn't support the Web Open Font Format (WOFF) fonts, which is used by google fonts.
If you want to use Poppins, you must download the ttf
file and include it in the java code:
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("font/Poppins/Poppins-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
OutputStream out = new FileOutputStream(new File("so.pdf"));
renderer.setDocumentFromString(html);
renderer.layout();
renderer.createPDF(out);
out.close();