I am trying to generate a pdf document using ITextRenderer that contains non-latin characters. In my case here is Bulgarian.
Before calling ITextRenderer, I have a String content that after some processes (like parsing with tidy) looks like that (I am able to see this value through debugging)
Sting content:
td class="description">Вид на потока</td>
td class="description">Статус на потока</td>
The above is just a part of my String. This content contains a valid html syntax. I just put here a small part of it to clarify that until this part, my encoding is right since I am able to read Bulgarian characters.
After that, the following code takes place which creates a document, put it in itextrenderer and generate the pdf file. This code is already tested and working for contents of lating characters since I was able to successfully generate a pdf file for english language.
The problem appears when I switch in another language (Bulgarian) with non latin characters. The generated PDF ignores all the bulgarian characters and the final result is a pdf with a lot of empty lines. This is the part of the code that generates the pdf
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setNamespaceAware(false);
dbf.setFeature("http://xml.org/sax/features/namespaces", false);
dbf.setFeature("http://xml.org/sax/features/validation", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(content.getBytes("UTF-8")));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
InputStream is = null;
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESBD.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESBI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(outputStream);
outputStream.close();
byte[] outputBytes = outputStream.toByteArray();
is = new ByteArrayInputStream(outputBytes);
response.setContentType("application");
response.addHeader("Content-Disposition", "attachment; filename=\"" + "exported.pdf" + "\"");
response.setContentLength(outputBytes.length);
response.getOutputStream().write(inputStreamToBytes(is));
I have tried several things (mainly related to encoding) but unfortunately I haven't found a solution yet. Probably I am missing something obvious here :)
I am not sure if this adds any value, but I am using spring and this code runs inside a Controller
Any help will be appreciated.
Thanx