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();
}
}
}
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.
In this case import is
Besides, it is better to incapsulate xhtml stringinto html tags, like this