我知道之前已经问过这个问题,但是我还没有确定哪个PDF生成框架可用于我当前的项目.
我的要求
很多人似乎都在使用iText,但我对关注点分离有一些担忧(除了更改的许可证):在HTML上下文中有很好的MVC支持,我通常坚持使用Spring MVC和FreeMarker来分离逻辑和布局.我有点担心,使用iText,你最终会混合代码和布局.
我知道,Apache FOP在这里可能是一个解决方案,但是我再次发现XSLT很繁琐,而且我读到FOP在许多文档的大量组合中可能会很慢?
我也考虑过JasperReports,但根据我的理解,这更适合包含表格数据集的报告而不是单个文档,例如需要大量布局格式的发票?
有什么想法吗?
有人提出了这方面的问题,但最近并没有这方面的问
要求:
假设的选项是iText,PDFBox,FOP,还有其他什么?基于上述要求的建议是什么?
我正在使用FOP 1.0版来创建PDF.在其中一个页面中,我想显示一个图像(2552 x 4200像素),如果它不完全适合页面,则将其缩小.据我所知,在邮件列表中,建议的方法是:
<fo:external-graphic inline-progression-dimension.maximum="100%"
content-height="scale-down-to-fit"
content-width="scale-down-to-fit"
src="..."/>
Run Code Online (Sandbox Code Playgroud)
不幸的是,仍然没有显示整个图像.图像的下半部分被切掉.任何人都可以给我一个关于我可能做错的提示吗?
我见过使用DocBook XML文档的最佳工具,但我的问题略有不同.对于XML DocBook,目前推荐的格式化工具链 - 而不是编辑工具?
Eric Raymond的"Unix编程艺术"(一本优秀的书!)中,建议是XML-FO(XML格式化对象),但我在此看到的建议表明XML-FO不再处于开发阶段(虽然我无法再在StackOverflow上找到这个问题,所以可能是错误的).
假设我主要对Unix/Linux(包括MacOS X)感兴趣,但我不会自动忽略仅限Windows的解决方案.
是Apache的FOP去的最佳途径?还有其他选择吗?
我在Tomcat 7上开发webapp.在我的本地版本的Tomcat 上一切正常,但是当我在生产服务器上部署它时,它会抛出这个异常.
java.lang.VerifyError: (class: org/apache/commons/logging/impl/Log4JLogger, method: fatal signature: (Ljava/lang/Object;Ljava/lang/Throwable;)V) Incompatible object argument for function call
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2595)
at java.lang.Class.getConstructor0(Class.java:2895)
at java.lang.Class.getConstructor(Class.java:1731)
at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:410)
at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
at org.apache.fop.apps.FopFactory.<clinit>(FopFactory.java:68)
at cz.soma.tomcat.generator.DokumentaceUtils.createPdfDocument(DokumentaceUtils.java:818)
at cz.soma.tomcat.generator.FileFactory.createPdfDocument(FileFactory.java:58)
at cz.soma.tomcat.generator.Generator.doPost(Generator.java:111)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
Run Code Online (Sandbox Code Playgroud)
当我尝试FopFactory.newInstance();(来自Apache FOP 1.0)时抛出错误.之后,它试图LogFactory.getLog(FopFactory.class);.它logClass.getConstructor(logConstructorSignature);被称为,其中logConstructorSignature包含一个String.class.(至少在我的本地机器上)
try …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用FOP和Java生成PDF文档.
我将XML作为字符串而不是文件接收.
如何将此XML字符串转换为XML输入流,以便我可以调用xslfoTransformer.transform(source,res); 其中source是我的XML字符串作为输入流.
请提供您的建议.
我试图用fop 0.95生成的pdf中修复图像的高度和宽度.这是用于它的代码
<fo:external-graphic src="s\image.png" height="2.00in" width="2.00in"/>
Run Code Online (Sandbox Code Playgroud)
以下也行不通
<fo:external-graphic src="s\image.png" content-height="scale-to-fit" height="2.00in" content-width="2.00in"/>
Run Code Online (Sandbox Code Playgroud)
这些给了我们由宽度控制的图像.总之,我试图拉伸图像,而不是使用fop 0.95保持纵横比.任何人都有想法吗?
我需要使用Apache FOP进行XSL转换,我有这样的代码:
//Setup FOP
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
//Setup Transformer
Source xsltSrc = new StreamSource(new File(xslPath));
Transformer transformer = tFactory.newTransformer(xsltSrc);
//Make sure the XSL transformation's result is piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
//Setup input
Source src = new StreamSource(new File(xmlPath));
//Start the transformation and rendering process
transformer.transform(src, res);
Run Code Online (Sandbox Code Playgroud)
这里xslPath是我的XSLT文件的存储路径.
我已经确认它在我只有一个XSLT文件时有效,但在我的项目中,我将事物分成了几个XSLT文件,并用<xsl:import />标签加入它们.使用此配置,我得到一个NullPointerException,因为它不了解存储在XSLT中的所有信息,因为它分布在不同的文件上.
我想知道是否有任何方法可以在Source xsltSrc变量中加载所有这些文件,以便所有XSL信息都可用.
UPDATE
我根据Mads Hansen给出的答案更改了代码,但它仍然无效.我必须在类路径中包含XSLT slt文件,因此我使用ClassLoader加载XSLT文件.我已经检查过URL在执行时有正确的路径url.toExternalForm().这是我的新代码:
ClassLoader cl = this.getClass().getClassLoader();
String systemID = "resources/xslt/myfile.xslt";
InputStream in …Run Code Online (Sandbox Code Playgroud) 我一直在尝试插入Unicode字符∇或▽,因此它显示在Apache FOP生成的PDF中.
这是我到目前为止所做的:
首先,您必须知道正确的Unicode代码点,以根据此基本帮助Apache XSL-FO输入来表示字符,Unicode代码点可以在unicode.org中的数学运算符列表中找到.代码是∇∇NABLA,我也可以使用▽▽一个向下的三角形.
找到正确的代码后,我必须选择包含必要字形的字体,并且Adobe PostScript和PDF规范指定必须可用于每个PostScript解释器和PDF阅读器的Base-14字体字符映射,因此我在列表和字体中搜索符号包含∇ ∇,(没有字体包含▽ ▽丢弃)
之后搜索结果代码,我认为我应该在xsl中使用:fo是:
Run Code Online (Sandbox Code Playgroud)<fo:block font-family="Symbol"> ∇ </fo:block>
问题是当它生成PDF时,结果不是我所期望的"∇",而是它显示的是"?",我读到当它无法显示时通常会显示"#",但我的代码显示"?".
我正在使用Apache fop 0.95,我怀疑是导致问题的那个版本,我应该更新到1.0,但目前这是生产中使用的版本,团队负责人表示现在很难升级它.
那么这就是问题,除了版本之外可能还有其他什么?可能是我忘记做的dummie吗? 如何使用Apache fop显示Unicode字符?
我将波兰字体嵌入到从HTML转换的PDF中时遇到问题.
我的HTML代码在正文中有样式:
<BODY style="font-family: Tahoma, Arial, sans-serif;font-size : 8pt;">
Run Code Online (Sandbox Code Playgroud)
我尝试了两种将这种HTML转换为PDF的方法:
对于FOP,我可以将所有使用过的字体添加到其配置文件中,然后创建PDF,嵌入这些字体(如果字体用于HTML).在生成的PDF中,我在Identity-H编码中使用了Tahoma字体.它看起来不错 - 所有波兰语字母都按预期显示.
然后我尝试用iText进行这样的转换:看起来比较简单,因为我不需要为每个HTML创建转换.不幸的是,我不知道如何将使用过的字体嵌入到生成的PDF中.我发现的大多数示例都是从头开始创建PDF,我不知道如何将这些方法应用于Flying Saucer ITextRenderer或转换中使用的其他对象.
我当前的代码尝试PDFCreationListener.preOpen()通过获取ITextFontResolver和添加字体来添加字体fs.addFont(path, true);.但我创建的所有.pdf都没有我想要的字体.
第二个问题是结果PDF没有波兰语字母.飞碟或iText有问题吗?Acrobat显示创建的PDF文档使用带Ansi编码的Helvetica和ArialMT作为字体.我认为这个Ansi编码不好.如何设置波兰语编码(Identity-H)?