PDFSlick

View and Interact with PDFs in React SolidJS, Svelte and JavaScript apps

README

readme-header

View and Interact with PDF documents in React, SolidJS, Svelte and JavaScript apps

PDFSlick is a library that enables viewing of and interaction with PDF documents in React, SolidJS, Svelte and JavaScript apps.
It's build on top of Mozilla's PDF.js, and utilises Zustand to provide a reactive store for the loaded documents.

PDFSlick for React


To get started with React run:

  1. ```shell
  2. npm install @pdfslick/react
  3. # yarn add @pdfslick/react
  4. # pnpm add @pdfslick/react
  5. ```

You can start using PDFSlick with the usePDFSlick() hook, like with the following basic example:

  1. ```jsx
  2. import { usePDFSlick } from "@pdfslick/react";
  3. import PDFNavigation from "./yourcomponents/PDFNavigation";
  4. import "@pdfslick/react/dist/pdf_viewer.css";

  5. type PDFViewerComponentProps = {
  6.   pdfFilePath: string,
  7. };

  8. const PDFViewerComponent = ({ pdfFilePath }: PDFViewerComponent) => {
  9.   const { viewerRef, usePDFSlickStore, PDFSlickViewer } = usePDFSlick(
  10.     pdfFilePath,
  11.     {
  12.       scaleValue: "page-fit",
  13.     }
  14.   );

  15.   const scale = usePDFSlickStore((s) => s.scale);
  16.   const numPages = usePDFSlickStore((s) => s.numPages);
  17.   const pageNumber = usePDFSlickStore((s) => s.pageNumber);

  18.   return (
  19.     <div className="absolute inset-0 pdfSlick">
  20.       <div className="relative h-full">
  21.         <PDFSlickViewer {...{ viewerRef, usePDFSlickStore }} />

  22.         {/*
  23.           Pass PDFSlick's store to your custom components
  24.         */}
  25.         <PDFNavigation {...{ usePDFSlickStore }} />

  26.         {/*
  27.           PDFSlick's store values automatically update
  28.         */}
  29.         <div className="absolute w-full top-0 left-0">
  30.           <p>Current scale: {scale}</p>
  31.           <p>Current page number: {pageNumber}</p>
  32.           <p>Total number of pages: {numPages}</p>
  33.         </div>
  34.       </div>
  35.     </div>
  36.   );
  37. };

  38. export default PDFViewerComponent;
  39. ```

Provided with the PDF Document path and PDFSlick options object, the usePDFSlick() hook returns an object consisting (among the other things) of:

- PDFSlickViewer is the PDF Viewer component used for viewing the PDF document
- `viewerRef` is the `ref` callback that is provided as a prop to the `` component
- usePDFSlickStore enables using PDFSlick store within your React components