Andriy Kryvtsun
Andriy Kryvtsun

Reputation: 3344

Cyrillic letters showing while PDF creation via iText

I have HTML page with Cyrillic letters and I use iText library for conversion from HTML to PDF. I don't use iText directly and use XMLWorkerHelper for doing conversion:

OutputStream file = new FileOutputStream(outputFile);
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();
InputStream is = new ByteArrayInputStream(htmlContentString.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
writer.close();
file.close();

Unfortunately, I can't see Cyrillic letters in output PDF file at all.

How can I prepare source HTML file (some font tags, CSS attributes etc.) for getting PDF output properly?

Upvotes: 1

Views: 834

Answers (1)

Andriy Kryvtsun
Andriy Kryvtsun

Reputation: 3344

The reason of the problem is iText internally uses a font without Cyrillic letters for PDF rendering.

So the solution is defining a font with Cyrillic letters for any PDF elements (e.g. default Windows Arial font). It's possible to do via specifying CSS file in parseXHtml call as a parameter:

...
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is, getStylesStream());
...
private InputStream getStylesStream() {
    return PdfReport.class.getResourceAsStream("/reports/styles.css");
}

and in styles.css should be

* {
    /* Enforces to use font with Cyrillic letters */
    font-family: Arial;
}

However, this approach looses any default iText styling. So I copied in my styles.css all content of default.css file from xmlworker.jar.

Unfortunately, ability to change content of default.css after its loading in memory (via XMLWorkerHelper.getInstance().getDefaultCSS()) is prohibited.

Upvotes: 3

Related Questions