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

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

Unhandled Promise Rejection: Error: DocException - expected a valid Error. #12

Closed florianrhomberg closed 2 years ago

florianrhomberg commented 2 years ago

Hello, with Vaadin 22.0.4 I get the above mentioned error when trying to open a PDF with the pdf Viewer, resulting that the PDF is not displayed, however downloading is working fine:

Bildschirmfoto 2022-06-08 um 14 53 11

Has someone the same problem?

Florian

florianrhomberg commented 2 years ago

I could solve the problem, it seems to be the same "issue" as already mentioned that the Plugin is not working directly with stream resources (I think the problem ist that the Stream resource returns only the dynamic part of the URL, but not the complete one). The following code is working and the PDFViewer displays sucessfully the PDF:

FileRecord file;
//
        // This is a workaround to use the PDFViewer, which only works with complete URLs.
        //
        String requestUrl = ((VaadinServletRequest) VaadinService.getCurrentRequest()).getRequestURL().toString();
        if(!requestUrl.endsWith("/"))
            requestUrl = requestUrl+"/";
        StreamResource resource = StreamResourceHandler.getStream(record);
        final StreamRegistration registration = VaadinSession.getCurrent().getResourceRegistry().registerResource(resource);
        this.url = requestUrl+registration.getResourceUri().toString();

PdfViewer viewer = new PdfViewer();
        viewer.setSrc(url);
        viewer.setSizeFull();
        this.add(viewer);

I hope that it helps some people who have the same problem. Florian