java FlyingSaucer renderer.setDocument 抛出“流关闭”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5074103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FlyingSaucer renderer.setDocument throws "Stream closed" exception
提问by paben
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.
我在使用此处找到的简单示例创建 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();
}
}
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)
回答by Terry Horner
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.
如果 xhtml 引用了渲染器无法找到的文件,例如 css 文件,renderer.setDocument 可能会引发“流关闭”异常。
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.
这种现象与原始海报并不完全匹配,因为“无法加载 XML 资源”没有出现在错误消息中,我将其包含在内是为了那些通过谷歌来到这里的人的利益。
回答by bluish
I solved this issue simply replacing
我解决了这个问题,只需更换
renderer.setDocument(url);
with
和
renderer.setDocument(new File(inputFile));
回答by Praveen
String File_To_Convert = "src/file.html";
String url = new File(File_To_Convert).toURI().toURL().toString();
//System.out.println("---"+url);
String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os) ;
os.close();
System.out.println("done.");
This is code which is working fine.
这是工作正常的代码。
Most of the people getting the above problem @ my code.
大多数人在@我的代码中遇到上述问题。
File_To_Convert = "src/file.html";
Here we have to give the relative path.
这里我们要给出相对路径。
回答by Chris Cashwell
What actually is happening here is the setDocument(...)
call executes and renderer
can't open an InputStream
(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 a File
or a live URL that the app can hit.
这里实际发生的是setDocument(...)
调用执行并且renderer
无法打开InputStream
(通常是因为文件不存在或没有足够的权限访问它)。解决方法是用File
应用程序可以访问的一个或一个实时 URL替换该引用。
回答by Chris
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.
“samples/firstdoc.xhtml”是教程中的文件吗?它是否在正确的目录中并且可以访问?XHTMLRenderer 只接受干净的 XHTML 代码并且非常严格。如果出现问题,您将得到一个例外。
In some of my projects I'm using JTidyto clean up the source before rendering.
在我的一些项目中,我使用JTidy在渲染前清理源代码。