vaadin-component-factory / vcf-pdf-viewer-flow

Vaadin Addon for providing pdf viewing functionality
Apache License 2.0
9 stars 6 forks source link

pdf-viewer download problem #38

Open rabiakirim opened 1 year ago

rabiakirim commented 1 year ago

I want to display and save the byte as pdf. I am using version 1.0.2. They fixed this issue in version 2.7.2 version 2.7.2. What solution can I use version 1.0.2? I cannot use a dynamic streamResource that I created as new StreamResource(filename, () -> new ByteArrayInputStream(contents)) for the 2nd time. I will be grateful if you could help me. Thanks

paodb commented 1 year ago

I will try to see if a backport to 1.0.x of the fix in 2.7.2 is possible.

rabiakirim commented 1 year ago

ok, thank you for your support, keep up the good work

PeterWMundt commented 1 year ago

In the current solution the constructor of the StreamResource requires an InputStreamFactory as parameter :

public StreamResource(String name, InputStreamFactory factory) 

In the demo example, the InputStream is created via a resource loaded from the class path:

StreamResource resource = new StreamResource("example.pdf", () -> getClass().getResourceAsStream("/pdf/example.pdf"));

In plain HTML this is translated to something like this:

<a download="" tabindex="-1" href="VAADIN/dynamic/resource/0/489d5a7c-4cd3-4aeb-a500-0973553d98ee/pdf/example.pdf"><vaadin-button theme="download-button" ..

In the end this means that "only" static pdf resources (in src/main/resources) can be downloaded with the current solution!

A solution where the Pdf is created dynamically like this:

    byte[] byteArray = makePdfAsByteArray(text1, text2);
    InputStream inputStream = new ByteArrayInputStream(byteArray);

.. will never work!

This link Baeldung - Download PDF in Servlet shows how a HttpServlet can downlaod a text-file! This sample can be easily changed to download a dynamic generated pdf file!

Up to now I have not succeeded to link this servlet solution with the PdfViewer!

Any help is appreciated!

..some sample code

PeterWMundt commented 1 year ago

These comments https://github.com/vaadin-component-factory/vcf-pdf-viewer-flow/issues/24#issuecomment-1515091975 and https://github.com/vaadin-component-factory/vcf-pdf-viewer-flow/issues/24#issuecomment-1515596474 gave me the hints for a working solution:

NOT:

 InputStream in = new ByteArrayInputStream(byteArrayPdf));
 StreamResource resource = new StreamResource(billFileName, () -> in);

..but..

 StreamResource resource = new StreamResource(billFileName, () -> new ByteArrayInputStream(byteArrayPdf)));   
 resource.setContentType("application/pdf");

.. works for me! :-)

Thanks to ARCHERS!