Class PdfViewer

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.pdfviewer.PdfViewer
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, Serializable

@Tag("vaadin-pdf-viewer") @NpmPackage(value="@vaadin/pdf-viewer", version="25.4.0-alpha5") @JsModule("@vaadin/pdf-viewer/src/vaadin-pdf-viewer.js") public class PdfViewer extends com.vaadin.flow.component.Component implements com.vaadin.flow.component.HasSize
PDF Viewer is a component for showing PDF documents. Give the document as a URL with setSrc(String), or serve it from the application with setSrc(DownloadHandler):
 PdfViewer viewer = new PdfViewer(
         DownloadHandler.forClassResource(getClass(), "report.pdf"));
 viewer.setHeight("600px");
 

This component is experimental and needs to be enabled with the com.vaadin.experimental.pdfViewerComponent feature flag, either in the experimental features tab of Copilot or in src/main/resources/vaadin-featureflags.properties:

 com.vaadin.experimental.pdfViewerComponent = true
 
Since:
25.4
Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Event fired when a document could not be loaded.
    static class 
    Event fired when a document has been loaded, before its pages are shown.
    static class 
    Event fired when the current page changes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a PDF viewer without a document.
    PdfViewer(com.vaadin.flow.server.streams.DownloadHandler downloadHandler)
    Creates a PDF viewer that shows the document served by the given download handler.
    Creates a PDF viewer that shows the document at the given URL.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.vaadin.flow.shared.Registration
    addDocumentErrorListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.DocumentErrorEvent> listener)
    Adds a listener that is called when a document could not be loaded.
    com.vaadin.flow.shared.Registration
    addDocumentLoadListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.DocumentLoadEvent> listener)
    Adds a listener that is called when a document has been loaded.
    com.vaadin.flow.shared.Registration
    addPageChangeListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.PageChangeEvent> listener)
    Adds a listener that is called when the current page changes.
    int
    Gets the current page, which is the page that takes up most of the visible area.
    int
    Gets the number of pages in the document.
    Gets the URL of the document.
    protected void
    onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
     
    com.vaadin.flow.signals.Signal<Integer>
    Gets a read-only signal with the number of pages in the document, or 0 if no document is loaded.
    com.vaadin.flow.signals.Signal<Integer>
    Gets a read-only signal with the current page, which is the page that takes up most of the visible area.
    void
    setSrc(com.vaadin.flow.server.streams.DownloadHandler downloadHandler)
    Sets a download handler that serves the document to show, for example one created with the factory methods of DownloadHandler.
    void
    Sets the URL of the document to show.

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, bindVisible, findAncestor, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTestId, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, getUIOrThrow, hasListener, isAttached, isTemplateMapped, isVisible, onDetach, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setTestId, setVisible, whenAttached

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.AttachNotifier

    addAttachListener

    Methods inherited from interface com.vaadin.flow.component.DetachNotifier

    addDetachListener

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasSize

    bindHeight, bindWidth, getHeight, getHeightUnit, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getWidth, getWidthUnit, setHeight, setHeight, setHeightFull, setMaxHeight, setMaxHeight, setMaxWidth, setMaxWidth, setMinHeight, setMinHeight, setMinWidth, setMinWidth, setSizeFull, setSizeUndefined, setWidth, setWidth, setWidthFull

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, bindClassName, bindClassNames, getClassName, getClassNames, getStyle, hasClassName, removeClassName, removeClassNames, setClassName, setClassName
  • Constructor Details

    • PdfViewer

      public PdfViewer()
      Creates a PDF viewer without a document.
    • PdfViewer

      public PdfViewer(String src)
      Creates a PDF viewer that shows the document at the given URL.
      Parameters:
      src - the URL of the document, or null to show no document
      See Also:
    • PdfViewer

      public PdfViewer(com.vaadin.flow.server.streams.DownloadHandler downloadHandler)
      Creates a PDF viewer that shows the document served by the given download handler.
      Parameters:
      downloadHandler - the download handler that serves the document, not null
      See Also:
  • Method Details

    • getSrc

      public String getSrc()
      Gets the URL of the document.
      Returns:
      the URL of the document, or null if no document is set
    • setSrc

      public void setSrc(String src)
      Sets the URL of the document to show. The current document is replaced, and the new one opens on the first page. PdfViewer.DocumentLoadEvent fires when the document has loaded, PdfViewer.DocumentErrorEvent if it could not be loaded.

      The browser fetches the document, so the URL must be on the same origin as the application, or the server must allow it with CORS headers. To serve a document from the application, use setSrc(DownloadHandler).

      Parameters:
      src - the URL of the document, or null to show no document
    • setSrc

      public void setSrc(com.vaadin.flow.server.streams.DownloadHandler downloadHandler)
      Sets a download handler that serves the document to show, for example one created with the factory methods of DownloadHandler. The framework generates a URL for the handler, which getSrc() returns, and the viewer loads it like a URL set with setSrc(String).
      Parameters:
      downloadHandler - the download handler that serves the document, not null
    • getPage

      @Synchronize(property="page", value="page-changed", allowUpdates=ALWAYS) public int getPage()
      Gets the current page, which is the page that takes up most of the visible area. The default value is 1.

      The value comes from the browser and must not be trusted for security decisions.

      Returns:
      the current page, starting from 1
    • pageSignal

      public com.vaadin.flow.signals.Signal<Integer> pageSignal()
      Gets a read-only signal with the current page, which is the page that takes up most of the visible area.

      The value comes from the browser and must not be trusted for security decisions.

      Returns:
      a read-only signal with the current page, starting from 1
    • getPageCount

      public int getPageCount()
      Gets the number of pages in the document.

      The value comes from the browser and must not be trusted for security decisions.

      Returns:
      the number of pages, or 0 if no document is loaded
    • pageCountSignal

      public com.vaadin.flow.signals.Signal<Integer> pageCountSignal()
      Gets a read-only signal with the number of pages in the document, or 0 if no document is loaded.

      The value comes from the browser and must not be trusted for security decisions.

      Returns:
      a read-only signal with the number of pages
    • addPageChangeListener

      public com.vaadin.flow.shared.Registration addPageChangeListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.PageChangeEvent> listener)
      Adds a listener that is called when the current page changes.
      Parameters:
      listener - the listener to add, not null
      Returns:
      a registration for removing the listener
    • addDocumentLoadListener

      public com.vaadin.flow.shared.Registration addDocumentLoadListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.DocumentLoadEvent> listener)
      Adds a listener that is called when a document has been loaded.
      Parameters:
      listener - the listener to add, not null
      Returns:
      a registration for removing the listener
    • addDocumentErrorListener

      public com.vaadin.flow.shared.Registration addDocumentErrorListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.DocumentErrorEvent> listener)
      Adds a listener that is called when a document could not be loaded.
      Parameters:
      listener - the listener to add, not null
      Returns:
      a registration for removing the listener
    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Overrides:
      onAttach in class com.vaadin.flow.component.Component