Note that there are some explanatory texts on larger screens.

plurals
  1. POEncoding issue while generating pdf file from HTML using ITextRenderer
    primarykey
    data
    text
    <p>I am trying to generate a pdf document using ITextRenderer that contains non-latin characters. In my case here is Bulgarian.</p> <p>Before calling ITextRenderer, I have a <em>String content</em> that after some processes (like parsing with tidy) looks like that (I am able to see this value through debugging)</p> <p><em>Sting content</em>:</p> <pre><code>td class="description"&gt;Вид на потока&lt;/td&gt; td class="description"&gt;Статус на потока&lt;/td&gt; </code></pre> <p>The above is just a part of my String. This <em>content</em> 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.</p> <p>After that, the following code takes place which creates a <em>document</em>, put it in <em>itextrenderer</em> and generate the <em>pdf</em> file. This code is already <strong>tested and working</strong> for contents of <strong>lating characters</strong> since I was able to successfully generate a pdf file for english language.</p> <p>The <strong>problem appears</strong> when I switch in another language (Bulgarian) with <strong>non latin characters</strong>. 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</p> <pre><code> 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)); </code></pre> <p>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 :)</p> <p>I am not sure if this adds any value, but I am using spring and this code runs inside a Controller</p> <p>Any help will be appreciated.</p> <p>Thanx </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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