Java renders XML documents as PDF

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

I have encountered some difficulties in using various Java - based frameworks

Apache FOP

It seems that this framework needs to specifically transform XML elements in documents into FOP entities Since the method in question must accept general XML, I don't think this framework meets my requirements

IText's

I try to use iText / flying Navigator (org. Xhtmlrenderer) to render documents. Although it does render PDF, the content only contains space separated data values without XML elements or attributes Use code & test data as follows:

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 PDF with content value value1 Value2 but no tag

My question is, can someone provide a code fragment for resending PDF containing XML content using one of the above frameworks, or is there another framework more suitable for my needs?

Editor: I realized that the same question was asked @ L_ 403_ 2@, but it seems that the proposed solution needs to have an in-depth understanding of the structure of the incoming XML document in the CSS file

Solution

This is a solution using iText Your HTML content is in the request And iText is not free View its licensing requirements as it has changed in recent years, although it is not 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;

 }

}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>