Pergunta

I have HTML file with an external CSS. I want to create PDF from the HTML file, but the endcoing doesn't work. HTML file works fine, but after transfering to PDF, some characters in PDF are missing. (čřě...) It happens even if I set the Charset in PDFWriter constructor.

How do I solve this, please?

 public void createPDF() {
    try {

        Document document = new Document();


        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(username + ID + ".pdf"));

        document.open();
        String hovinko = username + ID + ".html";

        XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(hovinko), Charset.forName("UTF-8"));

        document.close();

        System.out.println("PDF Created!");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Foi útil?

Solução

Did you try to convert your special characters before writing them to your PDF?

yourHTMLString.replaceAll(oldChar, newChar);

ć = ć
ř = ř
ě = ě

If you need more special characters, visit this link.

EDIT: Then try this out, it worked for me:

BaseFont basefont = BaseFont.createFont("C:/Windows/Fonts/ARIAL.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            Font font = new Font(basefont, 12);
            document.add(new Paragraph("čřě", font));

Outras dicas

Try it with below logic. It worked for me:

InputStream is = new ByteArrayInputStream(hovinko.getBytes(Charset.forName("UTF-8")));
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, Charset.forName("UTF-8"));

I used xmlworker version 5.5.12 and itextpdf version 5.5.12.

I was strugling with sam problem (Polish special signs). For me solution was to write a good font-family in html code.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top