我尝试使用Apache Fop和Java生成PDF,但生成的Pdf始终是一个空白页面。 它全部嵌套在Web应用程序中,Sever是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>
Example-XML-File看起来像这样:
<?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应用程序下载生成的文件。 我不知道,我的输出有什么不对......
答案 0 :(得分:0)
当你说你得到一个空白页面时,我不确定你是什么意思,因为运行上面的转换会为我产生一些输出。
但是,我确实注意到你的for-each不正确。您的选择正在查找DealerOverview/Brands/Brand
但是因为您已经在DealerOverview
模板中,所以选择正在此位置DealerOverview/DealerOverview/Brands/Brand
查找不存在的元素。因此,如果您从选择中删除DealerOverview
,则会在输出中获得其他信息。