FlyingSaucer renderer.setDocument 抛出“流已关闭”例外
我在使用找到的简单示例创建 PDF 时遇到问题 此处。这是我第一次尝试使用它,我尝试了一些方法并进行了大量搜索,但尚未找到生成错误的原因。该错误源自 renderer.setDocument(url);
行。如果有人有任何想法、建议或替代方案,我们将不胜感激。
package flyingsaucerpdf;
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class FirstDoc {
public static void main(String[] args)
throws IOException, DocumentException {
String inputFile = "samples/firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
控制台打印出以下错误。
ERROR: 'Stream closed'
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). java.io.IOException: Stream closed
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:211)
at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:134)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:138)
at com.asiaprice.service.email.template.CompletePdf.createpdf(CompletePdf.java:28)
I am having problems with creating a PDF using the simple example found here. It is my first time trying to use it and I have tried a few things and lots of searching but haven't found a reason why the error is generating. The error originates on the renderer.setDocument(url);
line. If anyone has any ideas, suggestions or alternatives it would be greatly appreciated.
package flyingsaucerpdf;
import java.io.*;
import com.lowagie.text.DocumentException;
import org.xhtmlrenderer.pdf.ITextRenderer;
public class FirstDoc {
public static void main(String[] args)
throws IOException, DocumentException {
String inputFile = "samples/firstdoc.xhtml";
String url = new File(inputFile).toURI().toURL().toString();
String outputFile = "firstdoc.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();
}
}
Console prints out the error below.
ERROR: 'Stream closed'
org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). java.io.IOException: Stream closed
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:211)
at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:134)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:138)
at com.asiaprice.service.email.template.CompletePdf.createpdf(CompletePdf.java:28)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果 xhtml 引用渲染器无法找到的文件(例如 css 文件),则 renderer.setDocument 可能会引发“流已关闭”异常。
这种情况的症状与原始海报并不完全匹配,因为错误消息中没有出现“无法加载 XML 资源”,我将其包含在内是为了那些通过 google 来到这里的人的利益。
renderer.setDocument can throw a “Stream closed” exception if the xhtml references a file that the renderer can't find, such as a css file.
The symptoms of this don't exactly match the original posters, as "Can't load the XML resource" doesn't appear in the error message, I am including this for the benefit of those who come here via google.
我解决了这个问题,只需替换
为
I solved this issue simply replacing
with
这是运行良好的代码。
大多数人在我的代码中遇到上述问题。
这里我们必须给出相对路径。
This is code which is working fine.
Most of the people getting the above problem @ my code.
Here we have to give the relative path.
“samples/firstdoc.xhtml”是教程中的文件吗?它是否位于正确的目录中并且可以访问? XHTMLRenderer 只接受干净的 XHTML 代码并且非常严格。如果出现问题,您将得到例外。
在我的一些项目中,我使用 JTidy 在渲染之前清理源代码。
Is "samples/firstdoc.xhtml" the file from the tutorial? Is it in the right directory and accessible? XHTMLRenderer only accepts clean XHTML code and is very strict. If something is wrong you will get an exception.
In some of my projects I'm using JTidy to clean up the source before rendering.
这里实际发生的是
setDocument(...)
调用执行,并且renderer
无法打开InputStream
(通常是因为文件不存在或没有足够的权限来访问它)。修复方法是将该引用替换为应用程序可以访问的文件或实时 URL。What actually is happening here is the
setDocument(...)
call executes andrenderer
can't open anInputStream
(usually because either the file doesn't exist or there are insufficient privileges to access it). The fix would be to replace that reference with aFile
or a live URL that the app can hit.