hi there,
In Save output to a stream | Documentation
you say “Let’s say that instead of saving rendering results to the local disk we want to have all the output file or output files in form of stream or list of streams.”.
I am trying to render to .pdf files.
To optimize the performance, I’m wondering if I should use FileStreamFactory where the OutputStream is buffered in Memory. However, the question is how to get hold of the converted .pdf files ?
After instantiating a class that implements FileStreamFactory and then passed to PdfViewOptions, how do I access the pdf ? Can you show some usage ?
Also, how to use the constructor
public PdfViewOptions(CreateFileStream createFileStream, ReleaseFileStream releaseFileStream) ?
Thanks very much
@anjanbacchu
I’m sorry for the delayed response. We’ll prepare a sample application to demonstrate how to use stream factories and share it with you.
@anjanbacchu
Please check this sample-app.zip (90.7 KB)
that demonstrates how you can save PDF to the output stream and access it.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MemoryFileStreamFactory streamFactory =
new MemoryFileStreamFactory(outputStream);
try (Viewer viewer = new Viewer("sample.docx"))
{
PdfViewOptions viewOptions = new PdfViewOptions(streamFactory);
viewer.view(viewOptions);
}
System.out.println(outputStream.size());
//TODO: use outputStream to save or further process PDF file
//....
public class MemoryFileStreamFactory implements FileStreamFactory {
private final ByteArrayOutputStream outputStream;
public MemoryFileStreamFactory(ByteArrayOutputStream outputStream) {
this.outputStream = outputStream;
}
public ByteArrayOutputStream createFileStream() {
return this.outputStream;
}
public void closeFileStream(OutputStream fileStream) {
//Do not close stream here
}
}
@anjanbacchu
You’re welcome, please let us know if you have any questions!
Hi Vladimir,
public void closeFileStream(OutputStream pageStream) {*
//Do not close stream here*
}
So, when do I close the OutputStream ? OR asking a different way, what should I do in closeFileStream() method above ?
Thanks again,
BR,
~A
@anjanbacchu
The method closeFileStream
is called by Viewer when the data is already written to the output stream so you can decide here on what to do next, for example, you can save the data to storage or post-process the output stream in this method and then close it.
In the code snippet above I’m instantiating the output stream ByteArrayOutputStream
and passing it to the MemoryFileStreamFactory
then Viewer converts the source file and writes the data (PDF) to the outputStream
, then it is reasonable to close the stream.