问题描述
我正在使用的罐子来自Maven 回购
The jar i am using is from the maven repo
我正在测试的代码来自以前的堆栈解决方案
The code i am testing is from a previous stack solution
我担心它可能会过时,因为库已过时.在测试以下代码时:
I fear it might be outdated because the libraries are depreciated. When testing the following code:
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class test
{
public static void main(String[] args)
{
ITextRenderer renderer = new ITextRenderer();
// if you have html source in hand, use it to generate document object
renderer.setDocumentFromString( "C:/Users/Goran/Documents/Documents/Development/workspace/FlyingSaucer/data/input/report.xhtml" );
renderer.layout();
String fileNameWithPath = "C:/Users/Goran/Documents/Documents/Development/workspace/FlyingSaucer/data/output/" + "PDF-FromHtmlString.pdf";
FileOutputStream fos = new FileOutputStream( fileNameWithPath );
renderer.createPDF( fos );
fos.close();
System.out.println( "File 2: '" + fileNameWithPath + "' created." );
}
}
此行出现以下错误:renderer.createPDF( fos );
The type com.lowagie.text.DocumentException cannot be resolved. It is indirectly referenced from required .class files
任何人都可以对此有所了解,或提出用Java创建pdf文档的最佳方法吗?我同时有XML和XHTML文档.
Anyone able to shed some light on this, or suggest the best way to create a pdf document in java? I have both XML and XHTML documents available.
编辑当我从回购
Exception in thread "main" java.lang.NoClassDefFoundError: org/xhtmlrenderer/extend/UserAgentCallback
at test.main(test.java:16)
Caused by: java.lang.ClassNotFoundException: org.xhtmlrenderer.extend.UserAgentCallback
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
我甚至还在使用正确的库/最新的库吗?
Am i even using the right libraries / the most up to date ones?
拥有最新资料库或知道其存储库的人可以链接它们吗?
Can someone who has the most recent libraries or know a repo where they are stored link them?
编辑2 放弃了使用飞碟并按建议使用ApacheFop的想法.当前错误是:
EDIT 2 Abandoned idea of using flying saucer and using ApacheFop as suggested. Current error is:
Mar 10, 2016 9:58:23 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "Symbol,normal,700" not found. Substituting with "Symbol,normal,400".
Mar 10, 2016 9:58:23 PM org.apache.fop.events.LoggingEventListener processEvent
WARNING: Font "ZapfDingbats,normal,700" not found. Substituting with "ZapfDingbats,normal,400".
Mar 10, 2016 9:58:23 PM org.apache.fop.events.LoggingEventListener processEvent
INFO: Rendered page #1.
推荐答案
我创建了一个具有以下依赖关系的简单Maven项目:
I created a simple Maven project with the following depencency:
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>core-renderer</artifactId>
<version>R8</version>
</dependency>
我完全用您上面发布的代码创建了一个测试类,并且能够毫无问题地对其进行编译. Eclipse将其显示为Maven依赖项:
I created a test class with excatly the code that you posted above and was able to compile it without issue. This is what Eclipse shows as the Maven dependencies:
请注意,我必须更改此行才能使代码起作用:
Note that I had to change this line for the code to work:
renderer.setDocumentFromString("<html><body><strong>Hello</strong> <em>world</em>!</body></html>");
...,因为字符串本身应该是HTML内容,而不是文件的路径.
... since the string should be the HTML content itself any not the path to the file.
PDF看起来像这样:
The PDF looked like this:
我希望对您有帮助...
I hope that helps...
这篇关于Java用飞碟生成PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!