I'm trying to take a PDDocument object and pass it to other module as InputStream without saving the document to the file system.
Now, I read about PDStream and kind of understood the purpose of this. Hence, I tried to do something like this:
PDStream stream = new PDStream(document);
InputStream is = stream.createInputStream();
But when I try to load that input stream into a PDDocument, I get this error:
Exception in thread "main" java.io.IOException: Error: End-of-File, expected line
at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1111)
at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:1885)
at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:1868)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:245)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1098)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:995)
at app.DGDCreator.main(DGDCreator.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:143)
Later I discovered that the result file is 0kb in size...
java.io.Input/OutputStreamandsave(OutputStream out)andload(InputStream in)?new PDStream(document)does not create a new stream containing the document but instead a new stream to use inside the document. If you really want to stream a pdf from one piece of code to the next without buffering it as a whole, consider using aPipedInputream/PipedOutputStreamconstruct.