FlyingSaucer renderer.setDocument throws "Stream closed" exception
Asked Answered
R

5

9

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)
Ruderal answered 22/2, 2011 at 4:45 Comment(1)
In general, my advice is "get the source, set a breakpoint where the exception is thrown. That particular exception really doesn't make sense in that context. The only thing I can think of is that the FileOutputStream failed to open for some reason.Operational
A
11

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.

Antirachitic answered 21/2, 2013 at 11:13 Comment(1)
Yeah, definitely make sure that any references in the XHTML can be resolved. Might also wrap this in a try/catch to prevent any kind of weird stack tracesGascony
B
1

I solved this issue simply replacing

renderer.setDocument(url);

with

renderer.setDocument(new File(inputFile));
But answered 26/9, 2011 at 9:52 Comment(0)
G
1
 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.

Gavin answered 13/2, 2013 at 13:33 Comment(1)
Can we pass a spring mapping in the File_To_Convert string?String toConvert = "/dailyFlight.do";Regality
I
0

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.

Iodate answered 22/2, 2011 at 11:9 Comment(0)
F
0

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.

Fain answered 12/1, 2012 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.