Blog Post

How to Build a Svelte PDF Viewer with PSPDFKit

Illustration: 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
  • 15+ annotation tools
  • Support for multiple file types
  • Dedicated support from engineers

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

  1. Install the degit package globally. It’s a project scaffolding tool from the creator of Svelte and Rollup:

npm install -g degit
  1. 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.

  1. Change to the created project directory:

cd my-svelte-project
  1. Install the dependencies:

npm install

Adding PSPDFKit to Your Project

  1. Add the PSPDFKit dependency:

npm install pspdfkit
  1. 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.

  1. Make sure your public directory contains a pspdfkit-lib directory with the PSPDFKit library assets.

Displaying a PDF

  1. Add the PDF document you want to display to the public directory. You can use our demo document as an example.

  2. 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>
  1. 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>
  1. Start the app and run it in your default browser:

npm run dev

You can see the application running on localhost:8080.

pspdfkit demo

Information

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:

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.

Related Products
Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
WEB  |  Web • Vue.js • JavaScript • How to • PDF Generation

Generate a PDF from HTML with Vue.js

PRODUCTS  |  Web • Releases • Components

PSPDFKit for Web 2022.3 Introduces New PDF Form Creator UI

TUTORIALS  |  Web • SharePoint • How To • PDF Viewer

SharePoint PDF Viewer: Integrating On-Premises with PSPDFKit