1

Pdf is genrating but its is coming blank ,I want to get a html content data in pdf without losing formatting, so i tried this code in this only blank pdf is genrating

    package config;
import com.lowagie.text.DocumentException;
import org.apache.commons.io.FileUtils;
import org.docx4j.org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class removeHtmlTag {
          public static void main(String [] args) throws DocumentException, IOException {
            FileUtils.writeByteArrayToFile(new File("removeHtmlTag.pdf"), toPdf("<b>YouAAA gotta walk and don't look back</b>"));
          }

          /**
           * Generate a PDF document
           * @param html HTML as a string
           * @return bytes of PDF document
           */
          private static byte[] toPdf(String html) throws DocumentException, IOException {
            final ITextRenderer renderer = new ITextRenderer();
            renderer.setDocumentFromString(html);
            renderer.layout();
            try (ByteArrayOutputStream fos = new ByteArrayOutputStream(html.length())) {
              renderer.createPDF(fos);
              return fos.toByteArray();
            }
          }

    }
Amigo
  • 21
  • 1
  • 6

1 Answers1

0

The reason is that you are using wrong ITextRenderer from docx4j package. Docx4j is supposed to be used for docx treatment, not for xhtml to PDF convertion. You should use, for example "Flying Saucer PDF Rendering", in this case pdf is Ok.

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

In this case import is

import org.xhtmlrenderer.pdf.ITextRenderer;

Besides, it is better to incapsulate xhtml stringinto html tags, like this

StringBuilder sb = new StringBuilder();
sb.append("<html>").append(System.lineSeparator())
.append("<body>").append(System.lineSeparator())
.append("<b>YouAAA gotta walk and don't look back</b>").append(System.lineSeparator())
.append("</body>").append(System.lineSeparator())
.append("</html>");
Alister
  • 91
  • 8
  • Getting this exception , Exception in thread "main" java.lang.NoClassDefFoundError: org/xhtmlrenderer/css/style/CssContext at config.removeHtmlTag.toPdf(removeHtmlTag.java:27) at config.removeHtmlTag.main(removeHtmlTag.java:18) Caused by: java.lang.ClassNotFoundException: org.xhtmlrenderer.css.style.CssContext at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 2 more – Amigo Mar 15 '19 at 03:31
  • Well, I tried the call, it worked in my project. Looks like you have no class org.xtmlrenderer.css.style.CssContex. According to this link https://github.com/flyingsaucerproject/flyingsaucer/blob/master/flying-saucer-core/src/main/java/org/xhtmlrenderer/css/style/CssContext.java Add another maven package flying-saucer-core, I added maven dependency inside answer – Alister Mar 15 '19 at 10:37