I am working on a java spring boot service that generates PDF file and uploads to FCP bucket.
No issues with PDF generation and uploading to GCS bucket. The issue is on generated pdf file, if we have any Chinese characters they are missing in generated pdf. I'm using thymeleaf and flying-saucer-pdf to generate html content and then generate pdf file using the html content.
Generated HTML content using pdf template file prints Chinese characters but the final generated pdf file is not showing the same Chinese characters.
Here are the maven dependencies.
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.1.22</version>
</dependency>
Here is the code snippet that generates pdf. Not showing the method that generates htmlContent as i=it works fine and prints the htmlContent as expected
private String getPDFURL(String recordId, Request request, String fileName, int id) {
String pdfURL = null;
try {
Map<String, Object> model = buildThymleafInputModel(recordId, request);
String htmlContent = pdfGenerator.parseThymeleafTemplate(PDF_TEMPLATE, model, request);
logger.info("htmlContent : {}", htmlContent);
// Create a temporary PDF file
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(baos);
baos.close();
pdfURL = pdfUploaderService.uploadFiletoGCS(fileName, baos,String.valueOf(id), APPLICATION_PDF);
// Delete the temporary file
baos.reset(); // this will clear the byte array
} catch (Exception e) {
e.printStackTrace();
}
return pdfURL;
}
Also I tried loading different fonts like NotoSans-Regular, BabelStoneHan, BATANG, arial-unicode-ms, SimSun, mingliu using below code snippet but nothing worked.
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("/templates/NotoSans-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(baos);
baos.close();
Here is the html content that got generated using Thymeleaf. It shows Chinese charactes.
COPY POINTS: </strong> <span>Please put below copy (the landlord requests Chinese copy only):
即将开业
English translation FYI:
Opening soon
And here is the content from generated pdf foe the same.
COPY POINTS: </strong> <span>Please put below copy (the landlord requests Chinese copy only):
English translation FYI:
Opening soon
Is there something missing from my code? Not sure how to fix this.