openhtmltopdf icon indicating copy to clipboard operation
openhtmltopdf copied to clipboard

PdfRendererBuilder's run method closes the OutputStream passed using the toStream method

Open nilpotence opened this issue 2 years ago • 0 comments

The documentation of the PdfRendererBuilder's toStream method states that when calling the run method, the passed OutputStream will not be closed. That's not true in practice : the createPdf / createPdfFast methods of the PdfBoxRenderer class both call _pdfDoc.save(os);, which itself closes the OutputStream.

My workaround is to wrap my outputstream in a FilterOutputStream on which I override the close method :

OutputStream pdfOutput = ... //my original outputstream
OutputStream nonCloseablePdfOutput = new FilterOutputStream(pdfOutput) {
        @Override
        public void close() throws IOException {
	        //openhtmltopdf has a bug where it closes the stream at the end of the "run" method
	        //even though the documentation states otherwise.
        }
};

builder.toStream(nonCloseablePdfOutput);

nilpotence avatar May 08 '22 20:05 nilpotence