The PDFViewerCanvas component is used for rendering a PDF document onto a canvas and includes navigation through the PDF pages. It provides loading, error handling, and navigation functionalities, making it flexible for displaying PDF files with page navigation controls.
- PDF Loading and Rendering: Uses
pdfjsLibto fetch and render a PDF onto an HTML5 canvas. - Navigation: Includes navigation between pages using the
DifferenceNavigationcomponent. - Error Handling: Displays an error message if the PDF fails to load.
- Loading Indicator: A loading spinner is displayed while the PDF is being fetched and rendered.
- Customizable Canvas Attributes: Accepts additional attributes for customization, like CSS classes and style properties.
loading: A boolean indicating if the PDF is still loading.numberOfPages: The total number of pages in the PDF.currentPage: Tracks the current page being rendered.errorMessage: Holds any error message in case the PDF fails to load.
src: The source URL or file for the PDF document.className: An optional class name for styling the canvas.documentName: The name of the document, used for slugifying theidof the canvas.pageNumber: The page number to display initially (default: 1).showNavigation: A boolean to control whether navigation is displayed (default: true)....attributes: Additional attributes passed to the canvas element.
- PDF Fetching:
- The
loadPDFfunction fetches the PDF using thepdfjsLib.getDocument()method. - The PDF is rendered using
renderPage()which takes the page number, fetches the respective page, and draws it on the canvas.
- The
- Page Rendering Queue:
- The
queueRenderPagemethod ensures that pages are rendered sequentially, preventing multiple render operations from overlapping.
- The
- The component shows a loading indicator while the PDF is fetched, and an error message if the PDF fails to load.
<PDFViewerCanvas
src="/path/to/pdf/document.pdf"
className="pdf-viewer"
documentName="example-document"
pageNumber={1}
showNavigation={true}
/>This component is well-suited for use cases where PDFs need to be viewed on a webpage with navigation between pages. It includes a flexible interface and handles common scenarios such as loading, error states, and navigating through multiple pages.