我正在尝试生成pdf
下载文件。我有以下代码从xml
和xsl-fo
生成pdf。任何人都可以指出我在我的代码中缺少的东西。当我打开pdf时,它表示已损坏,如果我使用notepad++
打开该文件,则文件中的所有内容均为[B@4bf24bf2
。
public void generatePDF(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException {
PortletContext portletContext = resourceRequest.getPortletSession().getPortletContext();
URL url = portletContext.getResource("/WEB-INF/xsd/xsl-fo.xsl");
File xsltfile = new File(url.getPath());
StreamSource source = new StreamSource(new File(portletContext.getResource("/WEB-INF/xsd/service.xml").getPath()));
StreamSource transformSource = new StreamSource(xsltfile);
// Construct FopFactory
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
// Create ByteArrayOutputStream
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// Setup JAXP using identity transformer
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer xslfoTransformer;
try {
xslfoTransformer = tfactory.newTransformer(transformSource); // identity transformer
Fop fop;
try {
// Construct fop with desired output format
fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
Result res = new SAXResult(fop.getDefaultHandler());
try {
xslfoTransformer.transform(source, res);
byte[] pdfBytes = outStream.toByteArray();
resourceResponse.setContentLength(pdfBytes.length);
resourceResponse.setContentType("application/pdf");
resourceResponse.addProperty("Content-Disposition", "attachment;filename=fleetevents.pdf");
resourceResponse.getWriter().print(pdfBytes);
resourceResponse.getWriter().flush();
} catch (TransformerException exception ) {
logger.log(Level.SEVERE, exception.getMessage(), exception);
}
} catch (FOPException fopException) {
logger.log(Level.SEVERE, fopException.getMessage(), fopException);
}
} catch(TransformerConfigurationException exception) {
logger.log(Level.SEVERE, exception.getMessage(), exception);
} catch(TransformerFactoryConfigurationError exception) {
logger.log(Level.SEVERE, exception.getMessage(), exception);
}
}
我的样本xml
<?xml version="1.0" encoding="iso-8859-1"?>
<services>
<service>
<serviceId>11111</serviceId>
<openDate>2012-02-16</openDate>
<closeDate>2012-02-16</closeDate>
<orderStatus>Status2</orderStatus>
<description>INSPECT MACHINE</description>
<repairCost>$266.76</repairCost>
</service>
<service>
<serviceId>11111</serviceId>
<openDate>2012-02-16</openDate>
<closeDate>2012-02-16</closeDate>
<orderStatus>Status1</orderStatus>
<description>INSPECT MACHINE</description>
<repairCost>$266.76</repairCost>
</service>
</services>
和我的样本xsf-fo.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output encoding="iso-8859-1" />
<xsl:template match ="services">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="service">
<fo:region-body></fo:region-body>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="service">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:table>
<fo:table-body>
<fo:table-row>
<fo:table-cell border="solid 1px black"
text-align="center" font-weight="bold">
<fo:block>Service Id</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black"
text-align="center" font-weight="bold">
<fo:block>Open Date</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black"
text-align="center" font-weight="bold">
<fo:block>Close Date</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black"
text-align="center" font-weight="bold">
<fo:block>Order Status</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black"
text-align="center" font-weight="bold">
<fo:block>Description</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black"
text-align="center" font-weight="bold">
<fo:block>Repair Cost</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="./service">
<fo:table-row>
<fo:table-cell border="solid 1px bold" text-align="center">
<fo:block><xsl:value-of select="serviceId" /></fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px bold" text-align="center">
<fo:block><xsl:value-of select="openDate" /></fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px bold" text-align="center">
<fo:block><xsl:value-of select="closeDate" /></fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px bold" text-align="center">
<fo:block><xsl:value-of select="orderStatus" /></fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px bold" text-align="center">
<fo:block><xsl:value-of select="description" /></fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px bold" text-align="center">
<fo:block><xsl:value-of select="repairCost" /></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
更新
我想我明白了,我需要使用resourceResponse.getPortletOutputStream().write(pdfBytes);
resourceResponse.getPortletOutputStream().flush();
代替resourceResponse.getWriter().print(pdfBytes); resourceResponse.getWriter().flush();
答案 0 :(得分:0)
查看getWriter()与getPortletOutputStream()的定义,似乎getWriter将返回一个PrintWriter,它只会将文本写入响应。
而PortletOutputStream将写入二进制数据。由于你的xml中有很多特殊字符,我猜想Printwriter无法完成这项工作并且PortletOutputStream成功: http://portals.apache.org/pluto/portlet-2.0-apidocs/javax/portlet/MimeResponse.html