0

On my app I want to open a generated PDF with PdfBox on a new tab, I have seen a couple of documentations but they are for old version of Vaadin or won't work in my case. I don't want to save it on the server or attach the file to any link or button, I want to write a method to automatically open it on a new tab.

PdfBox gives an option to save the file on an OutputStream, is there any way to open that OutputStream file on a new tab? Or at least download it without attaching it to an Anchor widget like Flow Viritin does?

MrRene
  • 174
  • 11
  • 1
    Have you tried using the OutputStream for an `Image` component? – kscherrer Jun 18 '20 at 06:31
  • No, I haven't... why @kscherrer? – MrRene Jun 18 '20 at 09:17
  • 1
    is it not your goal to display the pdf in a view? – kscherrer Jun 18 '20 at 09:31
  • 1
    Oh, now I guess I understand your question. If you don't save the file on the server, where exactly do you imagine to point to in the new tab? Either you let the user *download* it so the user can open the file themself, or you display an `Image` in the same View where you have your OutputStream (could also be in a `Dialog` to simulate a new Window), or you save the file on the server so the new Tab that you open for the user can read and display the file (again, using `Image`). So *no* there isn't a way to exactly do what you're asking. – kscherrer Jun 18 '20 at 09:44
  • 1
    It's basically not possible because you can't pass the outputstream as parameter to your new tab. Maybe a workaround where you put the outputstream in a VaadinSession attribute (see [this thread](https://stackoverflow.com/a/57603924/3441504) for more), Open a new Tab to a new `PdfPreviewView` where you display an `Image` using the stored Outputstream of the Session. – kscherrer Jun 18 '20 at 10:02
  • I think the last workaround sounds perfect @kscherrer, thanks! But I can't get the Image widget to preview the pdf, I'm using ByteArrayOutputStream and StreamResource as in the vaadin documentation but it says Could not load the image in the browser console – MrRene Jun 18 '20 at 10:30
  • I'll need your code to further help you there. Try to do it in the same view first - if it doesn't work there, you know the issue is not with the Storing/Reading in the VaadinSession. I think from the `ByteArrayOutputStream myOutputStream` you should be able to make an `Image` with this: `Image i = new Image(new StreamResource("test", () -> new ByteArrayInputStream(myOutputStream.toByteArray())), "alt text");` – kscherrer Jun 18 '20 at 11:21
  • It seems the Image widget won't show PDFs, works with jps and pngs but not pdf or at least I couldn't do it. This for example won't work: Image image = new Image("https://vaadin.com/download/book-of-vaadin/vaadin-7/pdf/book-of-vaadin.pdf", "DummyImage"); Dialog dialog = new Dialog(); dialog.add(image); – MrRene Jun 18 '20 at 11:28
  • However I was able to do display the pdf with this: https://vaadin.com/forum/thread/17577990/pdf-viewer-for-vaadin-13 I was just wondering how to do it with the Image component to save a little bit of code – MrRene Jun 18 '20 at 11:30
  • 1
    Ah, I wasn't aware of this. Good to know, and congrats on making it work. – kscherrer Jun 18 '20 at 12:01

1 Answers1

2

The problem lies in the fact that because you don't want to save the file on the server, you cannot then have a direct url of said file to link to. You also cannot pass the pdf file as parameter to another View.

The solution is to store the pdf in the VaadinSession (as byte[] is probably easiest), and when the user then opens another tab by clicking a certain anchor/routerlink, that view will read the pdf from the VaadinSession and add the it to the view. (How to display pdf with Vaadin 14). Please remember to remove the pdf again from the VaadinSession :)

kscherrer
  • 5,486
  • 2
  • 19
  • 59