我正在尝试使用 Apache Fop 和 Java 生成 PDF,但生成的 Pdf 始终是空白页。它都嵌套在 Web 应用程序中,服务器是 glassfish。
有人有建议吗?
这是我的xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" encoding="utf-8" />
<xsl:template match="/">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="DealerOverview">
<fo:region-body margin-top="4cm" margin-bottom="2cm" />
<fo:region-before extent="3.5" />
<fo:region-after extent="3.5" />
</fo:simple-page-master>
</fo:layout-master-set>
<xsl:apply-templates select="DealerOverview" />
</fo:root>
</xsl:template>
<xsl:template match="DealerOverview">
<fo:page-sequence master-reference="DealerOverview">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:table table-layout="fixed">
<fo:table-body>
<fo:table-row>
<fo:table-cell text-align="center" font-weight="bold">
<fo:block>Brand</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" font-weight="bold">
<fo:block>TargetYear</fo:block>
</fo:table-cell>
<fo:table-cell text-align="center" font-weight="bold">
<fo:block>TargetPrevYear</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="DealerOverview/Brands/Brand">
<fo:table-row>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="BrandId" />
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="TargetYear" />
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="TargetPrevYear" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</xsl:template>
<!-- TODO: Auto-generated template -->
</xsl:stylesheet>
示例 XML 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<DealerOverview>
<DealerDetails>
<Version>testForDealer</Version>
<Date>Oct, 1, 2013</Date>
<State>Released to Dealer</State>
<Dealer>Ungeheuer Automobile GmBH</Dealer>
<BrandOverview>All</BrandOverview>
</DealerDetails>
<Brands>
<Brand id="BM">
<BrandId>BM</BrandId>
<TargetYear>500</TargetYear>
<TargetPrevYear>1000</TargetPrevYear>
</Brand>
</Brands>
</DealerOverview>
编辑: 应该生成pdf的Java代码......
public void createPdfWithFop(File xmlString) throws IOException {
FacesContext context = FacesContext.getCurrentInstance();
ServletContext servletContext = (ServletContext) context.getExternalContext().getContext();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
pathToXsl = servletContext.getRealPath("/resources/reporting/PrintDealersOverview.xsl");
File xslFile = new File(pathToXsl);
String tempString = FileUtils.readFileToString(xmlString);
System.out.println("Das XML-File in der Fop: " + tempString + " ende");
StreamSource source = new StreamSource(xmlString);
StreamSource transformSource = new StreamSource(xslFile);
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent userAgent = fopFactory.newFOUserAgent();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer transformer = factory.newTransformer(transformSource);
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
Result res = new SAXResult(fop.getDefaultHandler());
transformer.transform(source, res);
byte[] pdfBytes = outStream.toByteArray();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=\"DealersOverview.pdf\"");
response.getOutputStream().write(pdfBytes);
// response.getOutputStream().flush();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FOPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
第二次编辑:
我发现,我的 outputStream 有问题。我想显示一个另存为对话框,以便从我的 Web 应用程序下载生成的文件。没看出来,我的输出有什么问题...