2012-09-18 91 views
0

我的環境是:FOP不產生可讀PDF

Glassfish的3.12 FOP-1.0 ,我試圖以產生經由一個servlet使用FOP的PDF文件。

XML文件是:

<?xml version='1.0' encoding='iso-8859-1'?> 
    <LIST> 
     <user> 
      <FIRSTNAME>Hi</FIRSTNAME> 
      <LASTNAME>Hello</LASTNAME> 
      <EMAIL>[email protected]</EMAIL> 
</user> 
     <user> 
      <FIRSTNAME>aa</FIRSTNAME> 
      <LASTNAME>ddd</LASTNAME> 
      <EMAIL>[email protected]</EMAIL> 
</user> 
</LIST> 

的XSL是:

<?xml version="1.0"?> 

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="LIST"> 
     <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 
      <fo:layout-master-set> 
       <fo:simple-page-master master-name="my-page"> 
        <fo:region-body margin="1in"/> 
       </fo:simple-page-master> 
      </fo:layout-master-set> 

      <fo:page-sequence master-reference="my-page"> 
       <fo:flow flow-name="xsl-region-body" font="12pt Times"> 

        <fo:table table-layout="fixed" width="100%"> 
         <fo:table-body> 
          <fo:table-row> 
           <fo:table-cell border="solid 1px black" 
       text-align="center" font-weight="bold"> 
            <fo:block> 
             First Name. 
            </fo:block> 
           </fo:table-cell> 
           <fo:table-cell border="solid 1px black" 
       text-align="center" font-weight="bold"> 
            <fo:block> 
             Last Name. 
            </fo:block> 
           </fo:table-cell> 
           <fo:table-cell border="solid 1px black" 
       text-align="center" font-weight="bold"> 
            <fo:block> 
             Email 
            </fo:block> 
           </fo:table-cell> 
          </fo:table-row> 
          <xsl:for-each select="./user"> 
           <fo:table-row> 
            <fo:table-cell border="solid 1px black" text-align="center"> 
             <fo:block> 
              <xsl:value-of select="position()" /> 
             </fo:block> 
            </fo:table-cell> 
            <fo:table-cell border="solid 1px black" text-align="center"> 
             <fo:block> 
              <xsl:value-of select="FIRSTNAME" /> 
             </fo:block> 
            </fo:table-cell> 
            <fo:table-cell border="solid 1px black" text-align="center"> 
             <fo:block> 
              <xsl:value-of select="LASTNAME" /> 
             </fo:block> 
            </fo:table-cell> 
            <fo:table-cell border="solid 1px black" text-align="center"> 
            <fo:block> 
             <xsl:value-of select="EMAIL" /> 
            </fo:block> 
           </fo:table-cell> 
           </fo:table-row> 
          </xsl:for-each> 
         </fo:table-body> 
        </fo:table> 

       </fo:flow> 
      </fo:page-sequence> 
      </fo:root> 
    </xsl:template> 

</xsl:stylesheet> 

和servlet代碼是:

try 
{ 
response.setContentType("application/pdf"); 
FopFactory fopFactory = FopFactory.newInstance(); 

      TransformerFactory tFactory = TransformerFactory.newInstance(); 
      ByteArrayOutputStream bout = new ByteArrayOutputStream(); 
      FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); 
      //Setup FOP 
      Fop fop = 
        fopFactory.newFop(MimeConstants.MIME_PDF,foUserAgent, bout); 
      //Setup Transformer 
      Source xsltSrc = new StreamSource(new File("test.xsl")); 
      Transformer transformer = tFactory.newTransformer(xsltSrc); 
      //Make sure the XSL transformation's result is piped through to FOP 
      Result res = new SAXResult(fop.getDefaultHandler()); 
      //Setup input 
      Source src = new StreamSource(new File("xmlfile2.xml")); 
      //Start the transformation and rendering process 

      transformer.transform(src, res); 
      //Prepare response 

      response.setContentLength(bout.size()); 

      //Send content to Browser 

      response.addHeader("Content-Disposition", 
        "attachment;filename=pdffile.pdf"); 
      response.getOutputStream().write(bout.toByteArray()); 
      response.getOutputStream().flush(); 



     } 

     catch(TransformerConfigurationException tce) 
     { 
       tce.printStackTrace(); 
     } 
     catch(FOPException fope) 
     { 
       fope.printStackTrace(); 
     } 
     catch(TransformerException te) 
     { 
       te.printStackTrace(); 

     } 

生成的PDF /下載,但WHI嘗試打開它,它說:

Adobe Reader could not open the file 'pdffile.pdf' because it is etiher not a supported file or because the file has been damaged. 

任何想法我錯過了什麼?

+0

有人幫忙嗎?謝謝 – Satya

回答

0

您還沒有關閉的OutputStream:

response.getOutputStream().close(); 
+0

即使在添加代碼後仍然會出現同樣的錯誤 – Satya

0

移動response.setContentType( 「應用程序/ PDF格式」);就在這些線上方:

response.addHeader("Content-Disposition", 
        "attachment;filename=pdffile.pdf"); 
      response.getOutputStream().write(bout.toByteArray()); 
      response.getOutputStream().flush(); 

而且代碼現在工作正常。謝謝