How to Build a Svelte PDF Viewer with PSPDFKit
In this post, we provide you with a step-by-step guide outlining how to deploy PSPDFKit’s Svelte PDF viewer.
Svelte is a component framework similar to React.js and Vue.js. What makes Svelte different from other frameworks is that it’s a compiler that takes declarative code and turns it into imperative JavaScript.
With Svelte, you’ll write less code, and your code will be more performant. It doesn’t use a virtual DOM on every state change like React does, and it’s about 30 percent faster than other frameworks.
What Is a Svelte PDF Viewer?
A Svelte PDF viewer lets you render and view PDF documents in a web browser without the need to download it to your hard drive or use an external application like a PDF reader.
PSPDFKit Svelte PDF Viewer
We offer a commercial Svelte PDF viewer library that can easily be integrated into your web application. It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.
- A prebuilt and polished UI for an improved user experience
- 15+ prebuilt annotation tools to enable document collaboration
- Support for more file types with client-side PDF, MS Office, and image viewing
- Dedicated support from engineers to speed up integration
Example of Our Svelte PDF Viewer
To demo our Svelte PDF viewer, upload a PDF, JPG, PNG, or TIFF file by clicking Open Document under the Standalone option (if you don’t see this option, select Choose Example from the dropdown). Once your document is displayed in the viewer, try drawing freehand, adding a note, or applying a crop or an e-signature.
Requirements to Get Started
To get started, you’ll need:
- Node.js (in this article, we’re using version 16.13.0)
- A package manager for installing and importing packages — you can use npm or Yarn
Creating a New Project
-
Install the degit package globally. It’s a project scaffolding tool from the creator of Svelte and Rollup:
npm install -g degit
-
Create a new Svelte app using the degit tool with the Svelte template:
npx degit sveltejs/template my-svelte-project
The default template uses Rollup as its module bundler.
-
Change to the created project directory:
cd my-svelte-project
-
Install the dependencies:
npm install
Adding PSPDFKit to Your Project
-
Add the PSPDFKit dependency:
npm install pspdfkit
-
Copy the PSPDFKit for Web library assets to the
public
directory:
cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib
The code above will copy the pspdfkit-lib
directory from within node_modules/
into the public/
directory to make it available to the SDK at runtime.
-
Make sure your
public
directory contains apspdfkit-lib
directory with the PSPDFKit library assets.
Displaying a PDF
-
Add the PDF document you want to display to the
public
directory. You can use our demo document as an example. -
Add a component wrapper for the PSPDFKit library and save it as
src/PdfViewerComponent.svelte
:
// src/PdfViewerComponent.svelte <script> import { afterUpdate, onMount, onDestroy } from "svelte"; import PSPDFKit from "pspdfkit"; let currentDocument; let container; let instance; export let document; async function load() { currentDocument = document; instance = await PSPDFKit.load({ baseUrl: `${window.location.protocol}//${window.location.host}/`, container: container, document: document, }); } function unload() { PSPDFKit.unload(instance); instance = null; } onMount(() => { load(); }); afterUpdate(() => { if (document !== currentDocument) { unload(); load(); } }); onDestroy(() => { unload(); }); </script> <body> <div bind:this={container} style="height: 100vh; width: 100vw;" /> </body>
-
Replace the contents of the
App.svelte
file with the newly created component:
// src/App.svelte <script> import Pspdfkit from "./PdfViewerComponent.svelte"; </script> <main> <Pspdfkit document="document.pdf" /> </main>
-
Start the app and run it in your default browser:
npm run dev
You can see the application running on localhost:8080
.
You can find the example on GitHub.
Adding Even More Capabilities
Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular Svelte guides:
- Adding annotations
- Editing documents
- Filling PDF forms
- Adding signatures to documents
- Real-time collaboration
- Redaction
- UI customization
Conclusion
You should now have our Svelte PDF viewer up and running in your web application. If you hit any snags, don’t hesitate to reach out to our Support team for help.
You can also integrate our PDF viewer using web frameworks like Angular, Vue.js, and React.js. To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.