简体   繁体   中英

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());

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM