2014-02-17 91 views
0

我試圖用Apache庫FOP創建一個PDF文檔。它運行良好,但我必須創建一個臨時文件來存儲中間文件(test.fop)。使用流來避免臨時文件

這裏是我的功能:

@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); 
} 

是否有可能test.fop存儲在流或任何緩衝,而不是一個文件?

+0

看看[BufferedOutputStream](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedOutputStream.html) – McDowell

回答

1

FOP網站上有一個example,可以解釋這一點。從XSLT轉換中發出的SAX事件直接送入FOP。在文件或內存中不緩衝。

相關問題