StreamResource + pdf

Hello all !

What’s wrong with that code ?

String path = app.getApplication().getContext().getBaseDirectory().getPath();

StreamResource stream = new StreamResource(
            new StreamResource.StreamSource() {
                public InputStream getStream() {
						return new ByteArrayInputStream((path + "relat.pdf").getBytes());
            }, "relat.pdf", app.getApplication());
app.getApplication().getMainWindow().open(stream, "_blank");

Show this :

When i open in Adobe Reader, works fine. But in application :frowning:

Google translates the error message as “Failed to load the pdf document”. Since it seems to be streaming the document to adobe reader properly and since chrome is just failing to load the document but seems to be finding it, have you made sure the pdf viewer in chrome is working properly? have you tested in other browsers?

Thank you Jonanthan, but did like this and works!

File f = new File(app.getApplication().getContext().getBaseDirectory().getPath() + "/relat.pdf");
FileResource fr = new FileResource(f, app.getApplication());

Problem is in this code:

return new ByteArrayInputStream((path + "relat.pdf").getBytes());

You’re trying to serve bytes of the string, containing the path to pdf file but not the pdf file itself. :slight_smile:


Are you printing to PDF an specific Layout??? how do you do it??