Вопрос

I'm trying to create a PDF document with Apache Library FOP. It works well but I have to create a temp file to store an intermediate file (test.fop).

Here is my function:

@RequestMapping(value = "/pdf")
public void showPdfReport(OutputStream pdfOutStream) throws Exception
{
    // Setup
    FopFactory fopFactory = FopFactory.newInstance();
    TransformerFactory factory = TransformerFactory.newInstance();

    // Transformation XML to XML-FO
    StreamSource xsltSource = new StreamSource(servletContext.getResourceAsStream("resources/xsl/pdfTemplate.fo"));
    Transformer transformer = factory.newTransformer(xsltSource);
    OutputStream fopOutStream = new BufferedOutputStream(new FileOutputStream(new File(
            "C:/Temp/tests/test.fop")));
    StreamSource xmlSource = new StreamSource(servletContext.getResourceAsStream("resources/xsl/test.xml"));
    StreamResult fopResult = new StreamResult(fopOutStream);
    transformer.transform(xmlSource, fopResult);

    //  Transformation XSL-FO to PDF
    Source fopSrc = new StreamSource(new File("C:/Temp/tests/test.fop"));
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, pdfOutStream);
    Result res = new SAXResult(fop.getDefaultHandler());
    transformer = factory.newTransformer();
    transformer.transform(fopSrc, res);
}

Is it possible to store test.fop in a stream or any buffer instead of a file?

Это было полезно?

Решение

There's an example on the FOP website that explains exactly this. The SAX events emitted from the XSLT transformation are directly fed into FOP. No buffering in a file or in memory.

Другие советы

Sure, you can pass any Reader or InputStream to the StreamSource constructor

You could use StringReader or ByteArrayInputStream for example.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top