UTF-8 Encoding not working in Flying Saucer

Viewed 1462

I am using flying saucer to convert my html webpage to pdf,

I have input something like,

<a id="link0" href="title0" title="H2">1. Economical multisignal meter DP20  &#x25a0; &#x25b2;   &#x25a0;</a>

I want the hex entities to be displayed in character format, for that I am using following code,

public void convertHtmlToPdf(String html, String css, OutputStream out) {
    try {
        html = correctHtml(html);
        html = getFormedHTMLWithCSS(html, css);

        HtmlCleaner cleaner = new HtmlCleaner();
        TagNode rootTagNode = cleaner.clean(html);
        CleanerProperties cleanerProperties = cleaner.getProperties();
        XmlSerializer xmlSerializer = new PrettyXmlSerializer(cleanerProperties);
        String cleanedHtml = xmlSerializer.getAsString(rootTagNode);




        URL fontResourceURL = getClass().getResource("/Calibri.ttf");

        ITextRenderer r = new ITextRenderer();

        r.getFontResolver().addFont(fontResourceURL.getPath(), "UTF-8", BaseFont.EMBEDDED);
//or    r.getFontResolver().addFont(fontResourceURL.getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        r.setDocumentFromString(cleanedHtml);

        r.layout();
        r.createPDF(out);
        r.finishPDF();

    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage(), e);
    }
}

I too have tried the solution using CSS, that either does not work for me.

@font-face {

src: url('/Calibri.ttf');
-fs-pdf-font-embed: embed;
-fs-pdf-font-encoding: Identity-H;
}

I could not make out my mistake.

0 Answers
Related