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

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

Fix for download anchor href value not updating when pdf src is changed #29

Closed drewharvey closed 1 year ago

drewharvey commented 1 year ago

Issue: In the current version, the download Anchor's href value is only set when created, but does not get updated when the PdfViewer::setSrc methods are called.

Fix: Update anchor's href when setSrc methods are called.

Code to Test Issue/Fix:

@Route(value = "", layout = MainLayout.class)
public class ExampleView extends FlexLayout {

  public BasicPdfViewerExample() {
    setSizeFull();

    PdfViewer pdfViewer = new PdfViewer();
    pdfViewer.setSizeFull();
    pdfViewer.setSrc("");

    ComboBox<String> files = new ComboBox<>("Select PDF");
    files.setItems("bitcoin.pdf", "example.pdf", "example-invoice.pdf");
    files.addValueChangeListener(e -> {
      String filename = e.getValue();
      if (StringUtils.isEmpty(filename)) {
        pdfViewer.setSrc("");
        return;
      }
      try
      {
        byte[] contents = getClass().getResourceAsStream("/pdf/" + filename).readAllBytes();
        pdfViewer.setSrc(new StreamResource(filename, () -> new ByteArrayInputStream(contents)));
      } catch (Exception ex)
      {
        Notification.show("Failed to load file");
        ex.printStackTrace();
      }
    });

    add(files, pdfViewer);
    setFlexGrow(1, pdfViewer);
  }

}