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-beta1") @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");
 

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:
  • 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.
    static final class 
    Container for your own controls in the PdfViewer toolbar.
    static class 
    Event fired when the toolbar is collapsed or expanded.
    static class 
    Event fired when the zoom level 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.
    com.vaadin.flow.shared.Registration
    addToolbarCollapsedChangeListener(com.vaadin.flow.component.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).
    com.vaadin.flow.shared.Registration
    addZoomChangeListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.ZoomChangeEvent> listener)
    Adds a listener that is called when the zoom level changes.
    void
    Downloads the document in the browser, like the download button of the toolbar does.
    Gets the file name used by download().
    Gets the title set with setHeaderTitle(String).
    Gets the i18n object.
    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.
    Gets the container for your own controls, such as buttons, in the toolbar.
    Gets the zoom level of the pages.
    boolean
    Gets whether the toolbar shows the download button.
    boolean
    Gets whether the viewer shows the header above the toolbar.
    boolean
    Gets whether the toolbar is collapsed.
    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
    scrollToPage(int page)
    Scrolls to the start of the given page.
    void
    setDownloadButtonVisible(boolean downloadButtonVisible)
    Sets whether the toolbar shows the download button.
    void
    setFileName(String fileName)
    Sets the file name used by download() and shown in the header.
    void
    setHeaderTitle(String headerTitle)
    Sets the title shown in the header.
    void
    setHeaderVisible(boolean headerVisible)
    Sets whether the viewer shows the header above the toolbar.
    void
    Sets the i18n object that translates the texts of the viewer.
    void
    setSrc(com.vaadin.flow.server.streams.DownloadHandler downloadHandler)
    Sets a download handler that serves the document to show.
    void
    Sets the URL of the document to show.
    void
    setToolbarCollapsed(boolean toolbarCollapsed)
    Sets whether the toolbar is collapsed.
    void
    setZoom(Zoom zoom)
    Sets the zoom level of the pages: Zoom.PAGE_WIDTH fits the width of the current page to the viewer Zoom.PAGE_FIT fits the whole current page into the viewer Zoom.of(double) scales the pages relative to their actual size The fit modes update the scale when the viewer resizes.

    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. The browser fetches the URL, so it must be reachable from the client.

      To show a file stored on the server, use PdfViewer(DownloadHandler), which serves the file to the browser through a generated 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 reachable from the client: on the same origin as the application, or on a server that allows it with CORS headers.

      To show a file stored on the server, a classpath resource, or a document generated by the application, use setSrc(DownloadHandler), which serves it to the browser through a generated URL.

      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. Use this to show a file stored on the server, a classpath resource, or a document generated in memory. The framework generates a URL for the handler, which getSrc() returns, and the viewer loads it like a URL set with setSrc(String).

      The factory methods of DownloadHandler create handlers for the common cases, for example for a file stored on the server:

       viewer.setSrc(DownloadHandler.forFile(new File("/data/report.pdf")));
       
      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 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
    • 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 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
    • addZoomChangeListener

      public com.vaadin.flow.shared.Registration addZoomChangeListener(com.vaadin.flow.component.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 com.vaadin.flow.shared.Registration addToolbarCollapsedChangeListener(com.vaadin.flow.component.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 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