How to create a PDF file from HTML using PDFBox?

vsingh picture vsingh · Oct 31, 2013 · Viewed 54.7k times · Source

I am trying to create a PDF from HTML content.

public byte[] generatePdf(final XhtmlPDFGenerationRequest request) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PDDocument document = new PDDocument();
    InputStream stream = new ByteArrayInputStream(request.getContent()
            .getBytes());

    PDStream pdstream = new PDStream(document, stream);
    document.save(baos);
    document.close();
    return this.toByteArray(baos);

}

When I take this byte[] and save to a file, the file is blank. I am using PDStream to embed the input stream into the document

From the http://pdfbox.apache.org/apidocs/

public PDStream(PDDocument doc,
                InputStream str)
         throws IOException

Reads all data from the input stream and embeds it into the document, this will close the InputStream.

Answer

vsingh picture vsingh · Nov 5, 2013

I was looking for an HTML to PDF renderer. We were using iText. I was looking to do same with Apache PDFBox. But, it looks like it cannot be done.

I can either use Apache FOP or continue using iText.

Here is the iText solution if anyone is interested: Java Render XML Document as PDF

If you are looking for a solution for merging using PDF box, here it is Merge pdf files using Apache pdf box