Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneration of PDF from HTML with non-Latin characters using ITextRenderer does not work
    text
    copied!<p>This is the 2nd day I spend investigating with no results. At least now, I am able to ask something very specific.</p> <p>I am trying to write a valid HTML code that contains some non-Latin characters in a PDF file using <a href="http://itextpdf.com/" rel="nofollow">iText</a> and more specifically using <strong>ITextRenderer</strong> from <a href="http://code.google.com/p/flying-saucer/" rel="nofollow">Flying Saucer</a>.</p> <p>My short example/code starts by initializing a string variable doc with this value:</p> <pre><code>String doc = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\"&gt;" + "&lt;body&gt;Some greek characters: Καλημέρα Some greek characters" + "&lt;/body&gt;&lt;/html&gt;"; </code></pre> <p>Here is the code that I use for debugging purposes. I save this string to HTML file and then I open it through a browser just to double check that HTML content is valid and I can still read Greek characters:</p> <pre><code>//write for debugging purposes in an html file File newTextFile = new File("C:/work/test.html"); FileWriter fw = new FileWriter(newTextFile); fw.write(doc); fw.close(); </code></pre> <p>Next step is to try to write this value in the PDF file. This is my code:</p> <pre><code>ITextRenderer renderer = new ITextRenderer(); //add some fonts - if paths are not right, an exception will be thrown renderer.getFontResolver().addFont("c:/work/fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); renderer.getFontResolver().addFont("c:/work/fonts/TIMESBD.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); renderer.getFontResolver().addFont("c:/work/fonts/TIMESBI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); renderer.getFontResolver().addFont("c:/work/fonts/TIMESI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); documentBuilderFactory.setValidating(false); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); builder.setEntityResolver(FSEntityResolver.instance()); org.w3c.dom.Document document = builder.parse(new ByteArrayInputStream( doc.toString().getBytes("UTF-8"))); renderer.setDocument(document, null); renderer.layout(); renderer.createPDF(os); </code></pre> <p>The final outcome of my code is:</p> <p><strong>In HTML file</strong> I get: <em>Some greek characters: Καλημέρα Some greek characters</em> (expected)</p> <p><strong>In PDF file</strong> I get: <em>Some greek characters: Some greek characters</em> (<strong>unexpected</strong> - greek characters are ignored!!)</p> <p>Dependencies:</p> <ul> <li><p>java version "1.6.0_27" </p></li> <li><p>itext-2.0.8.jar</p></li> <li><p>de.huxhorn.lilith.3rdparty.flyingsaucer.core-renderer-8Pre2.jar</p></li> </ul> <p>I also have been experimented with much more fonts, but I guess that my problem has nothing to do with using wrong fonts. Any help is more than welcome.</p> <p>Thanx</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload