我的环境是:
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>a@a.com</EMAIL>
</user>
<user>
<FIRSTNAME>aa</FIRSTNAME>
<LASTNAME>ddd</LASTNAME>
<EMAIL>a@kk.com</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,但是当我尝试打开它时,它说:
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 :(得分:0)
您尚未关闭OutputStream:
response.getOutputStream().close();
答案 1 :(得分:0)
移动了response.setContentType(“application / pdf”);就在这些线之上:
response.addHeader("Content-Disposition",
"attachment;filename=pdffile.pdf");
response.getOutputStream().write(bout.toByteArray());
response.getOutputStream().flush();
现在代码工作正常。感谢