Class 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
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 ClassesModifier and TypeClassDescriptionstatic classEvent fired when a document could not be loaded.static classEvent fired when a document has been loaded, before its pages are shown.static classEvent fired when the current page changes. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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 TypeMethodDescriptioncom.vaadin.flow.shared.RegistrationaddDocumentErrorListener(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.RegistrationaddDocumentLoadListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.DocumentLoadEvent> listener) Adds a listener that is called when a document has been loaded.com.vaadin.flow.shared.RegistrationaddPageChangeListener(com.vaadin.flow.component.ComponentEventListener<PdfViewer.PageChangeEvent> listener) Adds a listener that is called when the current page changes.intgetPage()Gets the current page, which is the page that takes up most of the visible area.intGets the number of pages in the document.getSrc()Gets the URL of the document.protected voidonAttach(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, or0if 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.voidsetSrc(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 ofDownloadHandler.voidSets 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, whenAttachedMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.vaadin.flow.component.AttachNotifier
addAttachListenerMethods inherited from interface com.vaadin.flow.component.DetachNotifier
addDetachListenerMethods inherited from interface com.vaadin.flow.component.HasElement
getElementMethods 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, setWidthFullMethods 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
Creates a PDF viewer that shows the document at the given URL.- Parameters:
src- the URL of the document, ornullto 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, notnull- See Also:
-
-
Method Details
-
getSrc
Gets the URL of the document.- Returns:
- the URL of the document, or
nullif no document is set
-
setSrc
Sets the URL of the document to show. The current document is replaced, and the new one opens on the first page.PdfViewer.DocumentLoadEventfires when the document has loaded,PdfViewer.DocumentErrorEventif 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, ornullto 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 ofDownloadHandler. The framework generates a URL for the handler, whichgetSrc()returns, and the viewer loads it like a URL set withsetSrc(String).- Parameters:
downloadHandler- the download handler that serves the document, notnull
-
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 is1.The value comes from the browser and must not be trusted for security decisions.
- Returns:
- the current page, starting from
1
-
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
0if no document is loaded
-
pageCountSignal
Gets a read-only signal with the number of pages in the document, or0if 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, notnull- 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, notnull- 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, notnull- Returns:
- a registration for removing the listener
-
onAttach
protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent) - Overrides:
onAttachin classcom.vaadin.flow.component.Component
-