Class PdfViewer

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

@Tag("vaadin-pdf-viewer") @NpmPackage(value="@vaadin/pdf-viewer", version="25.4.0-beta1") @JsModule("@vaadin/pdf-viewer/src/vaadin-pdf-viewer.js") public class PdfViewer extends Component implements 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");
 

You can add your own controls to the toolbar with getToolbarActions().

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:
  • 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(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(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
    • getFileName

      public String getFileName()
      Gets the file name used by download().
      Returns:
      the file name, or null if the name of the document is used
    • setFileName

      public void setFileName(String fileName)
      Sets the file name used by download() and shown in the header. When not set, the name of the document is used.
      Parameters:
      fileName - the file name, or null to use the name of the document
    • isDownloadButtonVisible

      public boolean isDownloadButtonVisible()
      Gets whether the toolbar shows the download button. The default is true.
      Returns:
      true if the download button is visible, false otherwise
    • setDownloadButtonVisible

      public void setDownloadButtonVisible(boolean downloadButtonVisible)
      Sets whether the toolbar shows the download button. download() works also while the button is hidden.
      Parameters:
      downloadButtonVisible - true to show the download button, false to hide it
    • download

      public void download()
      Downloads the document in the browser, like the download button of the toolbar does. If the document is still loading, it is downloaded once loaded. Does nothing if no document is set, it could not be loaded, or the viewer is not attached.
    • getPage

      @Synchronize(property="page", value="page-changed") 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
    • scrollToPage

      public void scrollToPage(int page)
      Scrolls to the start of the given page. A page below 1 scrolls to the first page, and a page above the page count scrolls to the last page.

      While a document is loading, for example right after setSrc, it scrolls once the document has loaded, and in a hidden viewer, once the viewer is shown. Does nothing without a document.

      Parameters:
      page - the page to scroll to, starting from 1
    • pageSignal

      public 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 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
    • isHeaderVisible

      public boolean isHeaderVisible()
      Gets whether the viewer shows the header above the toolbar. The default value is true.
      Returns:
      true if the header is visible, false otherwise
    • setHeaderVisible

      public void setHeaderVisible(boolean headerVisible)
      Sets whether the viewer shows the header above the toolbar. The header shows the title, see setHeaderTitle(String), and collapses and expands the toolbar when the user clicks it.

      While the header is hidden, the toolbar is always shown, also when it is collapsed with setToolbarCollapsed(boolean).

      Parameters:
      headerVisible - true to show the header, false to hide it
    • getHeaderTitle

      public String getHeaderTitle()
      Gets the title set with setHeaderTitle(String).
      Returns:
      the title, or null if no title is set
    • setHeaderTitle

      public void setHeaderTitle(String headerTitle)
      Sets the title shown in the header. By default, no title is set, and the header shows the file name of the document, or the text from PdfViewerI18n.setDocument(String) if the document has no file name.
      Parameters:
      headerTitle - the title, or null or an empty string to show the file name of the document
      See Also:
    • isToolbarCollapsed

      @Synchronize(property="toolbarCollapsed", value="toolbar-collapsed-changed") public boolean isToolbarCollapsed()
      Gets whether the toolbar is collapsed. The default value is false.

      While the header is hidden, the toolbar is shown also when this is true, see setHeaderVisible(boolean).

      Returns:
      true if the toolbar is collapsed, false otherwise
    • setToolbarCollapsed

      public void setToolbarCollapsed(boolean toolbarCollapsed)
      Sets whether the toolbar is collapsed. A collapsed toolbar is hidden, and the user can expand it again by clicking the header.

      The toolbar collapses only while the header is visible, see setHeaderVisible(boolean).

      Parameters:
      toolbarCollapsed - true to collapse the toolbar, false to expand it
    • getToolbarActions

      public PdfViewer.PdfViewerToolbarActions getToolbarActions()
      Gets the container for your own controls, such as buttons, in the toolbar. The controls are placed after the built-in page and zoom controls, before the download button. Empty by default.
      Returns:
      the toolbar actions container
    • getI18n

      public PdfViewerI18n getI18n()
      Gets the i18n object.
      Returns:
      the i18n object, or null if not set
    • setI18n

      public void setI18n(PdfViewerI18n i18n)
      Sets the i18n object that translates the texts of the viewer. Changes to the object take effect only when you set it again.
      Parameters:
      i18n - the i18n object, not null
    • getZoom

      @Synchronize(property="zoom", value="zoom-changed") public Zoom getZoom()
      Gets the zoom level of the pages. The default value is Zoom.PAGE_WIDTH.
      Returns:
      the zoom level, not null
    • setZoom

      public void setZoom(Zoom zoom)
      Sets the zoom level of the pages: The fit modes update the scale when the viewer resizes. Scrolling to a page of another size does not change the scale.

      The zoom stays when another document is set.

      Parameters:
      zoom - the zoom level, not null
    • addPageChangeListener

      public Registration addPageChangeListener(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
    • addZoomChangeListener

      public Registration addZoomChangeListener(ComponentEventListener<PdfViewer.ZoomChangeEvent> listener)
      Adds a listener that is called when the zoom level changes.
      Parameters:
      listener - the listener to add, not null
      Returns:
      a registration for removing the listener
    • addToolbarCollapsedChangeListener

      public Registration addToolbarCollapsedChangeListener(ComponentEventListener<PdfViewer.ToolbarCollapsedChangeEvent> listener)
      Adds a listener that is called when the toolbar is collapsed or expanded, either by the user clicking the header or with setToolbarCollapsed(boolean).
      Parameters:
      listener - the listener to add, not null
      Returns:
      a registration for removing the listener
    • addDocumentLoadListener

      public Registration addDocumentLoadListener(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 Registration addDocumentErrorListener(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(AttachEvent attachEvent)
      Description copied from class: Component
      Called when the component is attached to a UI.

      This method is invoked before the AttachEvent is fired for the component. Make sure to call super.onAttach when overriding this method.

      Overrides:
      onAttach in class Component
      Parameters:
      attachEvent - the attach event