How to build a Svelte PDF viewer

In this article, we’ll walk you through implementing a Svelte PDF viewer using Nutrient. Whether you’re building a document management system, a collaborative platform, or a simple PDF viewer, Nutrient’s powerful features and seamless Svelte integration make it an excellent choice. We’ll also explore the features and capabilities of Nutrient, discuss performance considerations, and provide tips for optimizing Svelte PDF rendering.
How to build a Svelte PDF viewer

Svelte(opens in a new tab) 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.

Nutrient: A commercial Svelte PDF viewer library

We offer a commercial PDF viewer SDK for Svelte 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 user interface (UI) that you can extend or simplify based on your unique use case.

  • A prebuilt and polished UI for an improved user experience
  • Support for more file types with client-side PDF, MS Office, and image viewing
  • Dedicated support from engineers to speed up integration

Why choose Nutrient for your Svelte PDF viewer?

Nutrient is a robust PDF solution that offers a wide range of features to enhance your Svelte application. The following section provides an overview of what you can achieve with Nutrient in a Svelte environment.

Key features of Nutrient in Svelte

These features not only enhance the user experience but also make Nutrient a versatile solution for legal, education, and enterprise industries.

Benefits of using Nutrient in Svelte

  • Enhanced user experience — Nutrient’s intuitive interface and powerful tools make it easy for users to interact with PDFs.

  • Versatility — From annotations to digital signatures, Nutrient supports a wide range of use cases.

  • Scalability — Whether you’re handling small documents or large files, Nutrient is designed to perform efficiently.

  • Customizability — Tailor the viewer to match your application’s branding and functionality requirements.

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 eSignature.

Requirements to get started

To get started, you’ll need:

Creating a new project

  1. Install the degit(opens in a new tab) package globally. It’s a project scaffolding tool from the creator of Svelte and Rollup(opens in a new tab):
Terminal window
npm install -g degit
  1. Create a new Svelte app using the degit(opens in a new tab) tool with the Svelte template:
Terminal window
npx degit sveltejs/template my-svelte-project

The default template uses Rollup(opens in a new tab) as its module bundler.

  1. Change to the created project directory:
Terminal window
cd my-svelte-project
  1. Install the dependencies:
Terminal window
npm install

Adding Nutrient to your project

  1. Add the Nutrient dependency:
Terminal window
npm install pspdfkit
  1. Copy the Nutrient Web SDK library assets to the public directory:
Terminal window
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 Nutrient 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 Nutrient 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:
Terminal window
npm run dev

You can see the application running on localhost:8080.

nutrient demo

You can find the example on GitHub(opens in a new tab).

Performance considerations for Svelte PDF rendering

When building a Svelte PDF viewer, performance is critical, especially when handling large documents. Here are some strategies to optimize your application:

  1. Lazy loading — Load PDF documents only when they’re needed to reduce initial load time.

  2. Efficient memory management — Destroy unused instances of the PDF viewer to free up memory.

  3. Handling large files — Use chunked loading or server-side rendering to handle large PDFs without compromising performance.

  4. Caching — Cache frequently accessed documents to improve load times for returning users.

Conclusion

Building a Svelte PDF viewer with Nutrient is a straightforward process that unlocks powerful document management capabilities. By following the steps outlined in this post, you can integrate Nutrient into your Svelte application, optimize performance, and deliver a seamless user experience. Whether you’re working on a small project or a large-scale application, Nutrient’s feature-rich SDK is the perfect solution for all your PDF needs.

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.

Hulya Masharipov

Hulya Masharipov

Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Explore related topics

FREE TRIAL Ready to get started?