我使用fop生成pdf输出,我是新手。经过长时间的工作,我遇到了一个问题:
com.sun.org.apache.xalan.internal.xsltc.compiler.CompilerException:输入文档不是样式表(XSL命名空间未在根元素中声明)。
有我的xsl文件:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="root">
<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">
<!-- LOGO
<fo:block>
</fo:block>
-->
<!-- TEXT -->
<fo:block text-align="justify">
<xsl:value-of select="text" />
</fo:block>
<!-- TABLES -->
<xsl:for-each select="./table">
<fo:block>
<fo:block font-weight="bold"> <!-- Add some adjustment -->
<xsl:value-of select="@name" />
</fo:block>
<table> <!-- Add some border -->
<fo:table-body> <!-- Add some adjustment -->
<fo:table-row>
<xsl:for-each select="./header/cell">
<fo:table-cell>
<fo:block font-weight="bold"> <!-- Add some adjustment and extra border -->
<xsl:value-of select="." />
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
<xsl:for-each select="./row">
<fo:table-row>
<xsl:for-each select="./cell">
<fo:table-cell>
<fo:block> <!-- Add some adjustment -->
<xsl:value-of select="." />
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</table>
</fo:block>
</xsl:for-each>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
并且Java部分存在打印文件被触发
private void convertToPDF(File xsl, File pdf) throws IOException,
FOPException, TransformerException {
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
OutputStream out = new BufferedOutputStream(new FileOutputStream(pdf));
try {
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent,
out);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(
xsl));
Source src = prepareSource();
Result res = new SAXResult(fop.getDefaultHandler());
// Start XSLT transformation and FOP processing
transformer.transform(src, res);
} finally {
out.close();
}
}
给予此方法的参数xsl是从我的项目资源中挑选的文件,我确信这是一个好的文件(打印出来)。
我的xml源是在java中动态生成的。如果需要这些文件,那么我将编辑这篇文章并将它们放在那里。
我一直在尝试编辑我的xsl文件,简化它但没有任何效果。
对于编译,我使用Eclipse和Spring Tool Suite扩展
你能帮我吗?
另外,你能告诉我如何在我的pdf输出中放置图像吗?