Html to Pdf converted pdf has "#" for some characters

150 Views Asked by At

I'm converting HTML to PDF with openhtmltopdf library, but the converted PDF uses "#" instead of characters like "İ","ş","Ş","ç","Ç","Ö". How can I solve it?

Java code:

ByteArrayOutputStream renderedPdfBytes = new ByteArrayOutputStream();
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withHtmlContent(htmlContent, "/");
builder.toStream(renderedPdfBytes);
builder.run();
renderedPdfBytes.close();
byte[] renderedPdf = renderedPdfBytes.toByteArray(); 

try (FileOutputStream fos = new FileOutputStream(pdfFile)) {
    fos.write(renderedPdf);
}

Html content:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <span class="close-icon close"></span>
        <div class="modal-content">
            <div class="modal-header"><h2>İade Prosedürü 27.09.2023</h2></div>
            <div class="modal-body" style="margin-bottom: 50px;"><p>İade edilmek istenen.</p></div>
        </div>
    </body>
</html>

HTML View:

enter image description here

Converted PDF View:

enter image description here

1

There are 1 best solutions below

3
Digbijay Gautam On

If you are experiencing issues with characters not being displayed correctly in the PDF generated using the openhtmltopdf library, it may be related to font embedding or font support. You can modify the code to include custom fonts with proper character support. Here is the example of it.

PdfRendererBuilder builder = new PdfRendererBuilder();
// Use Noto Sans font (or any other font with proper character support)
builder.useFont(() -> {
    try (InputStream inputStream = HtmlToPdfConverter.class.getResourceAsStream("/path/to/NotoSans-Regular.ttf")) {
        return FontUtil.createFont(inputStream);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
});