Karthikeyan
Karthikeyan

Reputation: 2001

Images are not coming in PDF, using ITextRenderer

Am trying to convert my html content into PDF. Am having some text as well as images (PNG) in my html document. After converting using iTextRenderer only images are not coming in PDF, rest of the text contents are coming properly. Am printing my html content too (using Sysout) image tag is coming properly but not displaying in PDF.

Please find my java code below.

ITextRenderer renderer = new ITextRenderer();
Document resultDoc = (Document) result.getNode();
renderer.setDocument(resultDoc, "");
renderer.layout();


ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);
baos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());

Upvotes: 4

Views: 5635

Answers (1)

Karthikeyan
Karthikeyan

Reputation: 2001

I have fixed this issue by changing the dependencies. Earlier i was using the following dependencies which is not parsing image into PDF file.

<!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/core-renderer -->
    <dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>core-renderer</artifactId> 
    <version>R8pre2</version>
    </dependency> 

Then i replaced it with the below dependencies and its worked fine. No code change required.

  <!-- https://mvnrepository.com/artifact/org.xhtmlrenderer/flying-saucer-pdf-itext5 -->
    <dependency>
        <groupId>org.xhtmlrenderer</groupId>
        <artifactId>flying-saucer-pdf-itext5</artifactId>
        <version>9.1.13</version>
    </dependency>

Upvotes: 2

Related Questions