FlyingSaucer renderer.setDocument 抛出“Stream closed"例外 [英] FlyingSaucer renderer.setDocument throws "Stream closed" exception

查看:89
本文介绍了FlyingSaucer renderer.setDocument 抛出“Stream closed"例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用找到的简单示例创建 PDF 时遇到问题 此处.这是我第一次尝试使用它,我尝试了一些东西并进行了大量搜索,但没有找到产生错误的原因.错误源自 renderer.setDocument(url); 行.如果有人有任何想法、建议或替代方案,我们将不胜感激.

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)

推荐答案

renderer.setDocument 可以在 xhtml 引用渲染器找不到的文件(例如 css 文件)时抛出流关闭"异常.

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.

这种现象与原始海报不完全匹配,因为无法加载 XML 资源"没有出现在错误消息中,我将其包含在内是为了那些通过谷歌来到这里的人的利益.

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.

这篇关于FlyingSaucer renderer.setDocument 抛出“Stream closed"例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆