我正在尝试生成pdf
下载文件。xml
我有以下代码可以从和生成 pdf xsl-fo
。谁能指出我的代码中缺少的内容。当我打开 pdf 时,它显示已损坏,如果我使用notepad++
all is see[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();