Search code examples
javaxmlpdfitextapache-fop

Java Render XML Document as PDF


I have an XML document currently stored as an in-memory string & want to render it as a PDF. In other words, the PDF content will be an XML document. The XML being rendered by the method is generic -- multiple types of XML documents might be sent in.

I'm having a bit difficulty figuring out how to accomplish using using various Java-based frameworks.

Apache FOP

It appears as if this framework require specific transformation for XML elements in the document to FOP entities. Since the method in questions must accept generic XML, I don't think this framework fits my requirement.

iText

I've tried rendering a document using a combination of iText/Flying Saucer (org.xhtmlrenderer) and while it does render a PDF, the content only contains space-separated data values and no xml elements or attributes. Using the code & test data below below:

File

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <elem1>value1</elem1>
  <elem2>value2</elem2>
</root>

Code

File inputFile = new File(PdfGenerator.class.getResource("test.xml").getFile());
OutputStream os = new FileOutputStream("c:\\temp\\Sample.pdf");
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(inputFile);
renderer.layout();
renderer.createPDF(os);
os.close();  

Results in a PDF that contains the content values value1 value2, but no tags.

My question is can someone provide a code snippet for rending a PDF containing XML content using one of the frameworks above, or is there another framework better suited to my needs?

Edit: I realize the same question was asked here, but it seems the solution presented requires intimate knowledge of the structure of the incoming XML doc in the css file.


Solution

  • This is the solution using itext . Your html content is in the request. And itext is not free. Check out its licensing requirement as it has changed in recent years although it is not very expensive.

    public class MyPDFGeneratorService {
    
        public byte[] generatePdf(final XhtmlPDFGenerationRequest request) {
            try {
    
                ITextRenderer renderer = new ITextRenderer();
                renderer.setDocument(this.getDocument(request.getContent()), null);
                renderer.layout();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                renderer.createPDF(baos);
                return this.toByteArray(baos);
    
            }
            catch (Exception e) {
                throw new PDFGenerationException(
                        "Unable to generate  PDF.", e);
            }
        }
    
        private Document getDocument(final String content) {
            InputSource is = new InputSource(new BufferedReader(new StringReader(
                    content)));
            return XMLResource.load(is).getDocument();
        }
    
    
        private byte[] toByteArray(final ByteArrayOutputStream baos)
            throws IOException {
        byte[] bytes = baos.toByteArray();
        baos.close();
        return bytes;
    
     }
    
    }