Add PDF functionality with Svelte
Nutrient Web SDK is a JavaScript PDF library for viewing, annotating, and editing PDFs directly in the browser. Use it to add PDF capabilities to any web app.
This guide walks you through the steps to integrate Nutrient Web SDK into your project. By the end, you’ll be able to render a PDF document in the user interface (UI).
You can test the SDK capabilities in our playground.
Prefer to jump straight to code? View the example repo on GitHub.
Installation
You can load Nutrient Web SDK directly from Nutrient’s content delivery network (CDN). Nutrient maintains the CDN for customers, and it’s our recommended way to get started. For more control and flexibility, use the local installation option.
Add the following
<script>tag in yourapp.htmlfile:app.html <!doctype html><html lang="en"><head><meta charset="utf-8" /><link rel="icon" href="%sveltekit.assets%/favicon.png" /><meta name="viewport" content="width=device-width, initial-scale=1" />%sveltekit.head%</head><body data-sveltekit-preload-data="hover"><script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.9.1/nutrient-viewer.js"></script><div style="display: contents">%sveltekit.body%</div></body></html>You’re now ready to use Nutrient Web SDK and reference
window.NutrientViewerin the client-side code.
Add the Nutrient Web SDK (
@nutrient-sdk/viewer) dependency:If you tried CDN installation first, make sure to remove the script tag:
app.html <script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.9.1/nutrient-viewer.js"></script><div style="display: contents">%sveltekit.body%</div>You’re now ready to use Nutrient Web SDK locally in your Svelte app.
Rendering a PDF
Load the PDF file into a component — for example,
+page.svelte— usingNutrientViewer:+page.svelte <script lang="ts">import { onMount } from "svelte";let container: HTMLDivElement | null = null;onMount(() => {const { NutrientViewer } = window;if (container && NutrientViewer) {NutrientViewer.load({container,document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",});}return () => {NutrientViewer?.unload(container);};});</script><div class="container" bind:this={container}></div><style>/* we also need to set the container height and width */.container {height: 100vh;width: 100%;}</style>Start the development server:
You’ll see the PDF rendered in the Nutrient Web SDK user interface (UI).
With package manager installation, access the Nutrient Web SDK module directly. Update your
+page.sveltefile to load the PDF file, as shown below:+page.svelte <script lang="ts">import { onDestroy, onMount } from "svelte";let container: HTMLDivElement | null = null;let NutrientViewer: typeof import("@nutrient-sdk/viewer").default | undefined;onMount(async () => {NutrientViewer = (await import("@nutrient-sdk/viewer")).default;// Ensure there's only one `NutrientViewer` instance.NutrientViewer.unload(container);if (container && NutrientViewer) {NutrientViewer.load({container,document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",});}});onDestroy(() => {NutrientViewer?.unload(container);});</script><div class="container" bind:this={container}></div><style>/* we also need to set the container height and width */.container {height: 100vh;width: 100%;}</style>Optional: If you decide to self-host the Nutrient Web SDK assets as described in this guide, make sure to provide a
baseUrlin your configuration, as shown below:NutrientViewer.load({container,document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",baseUrl: `${window.location.protocol}//${window.location.host}/${import.meta.env.PUBLIC_URL ?? "" // Usually empty for Vite, but supports custom deployments.}`,})If you don’t set a
baseUrl, the SDK will load assets from the CDN by default.Start the development server:
Next steps
This section outlines additional steps for setting up your project.
TypeScript with CDN installation
Nutrient Web SDK comes with built-in support for TypeScript. This should work out of the box for the package manager installation. For the CDN installation, follow the steps below.
Add the Nutrient Web SDK dependency, if not done previously. You need the package installed locally to reference the types:
Add a reference for built-in types for the SDK in
app.d.ts:app.d.ts import NutrientViewer from "@nutrient-sdk/viewer";// See https://svelte.dev/docs/kit/types#app.d.ts// for information about these interfacesdeclare global {namespace App {// interface Error {}// interface Locals {}// interface PageData {}// interface PageState {}// interface Platform {}}interface Window {// Nutrient Web SDK will be available on window.NutrientViewer once loadedNutrientViewer?: typeof NutrientViewer;}}export {};Restart the TypeScript server or your editor if needed.
Optimizing the CDN installation
If you use the CDN installation approach in production, we recommend considering optimizations such as prefetching(opens in a new tab).
Troubleshooting
If you encounter issues, refer to the common issues guide.
Note for developers using AI coding assistants: To get accurate troubleshooting help, copy the content from the troubleshooting guide and include it in your prompt, along with your specific error.
You can test the SDK capabilities in our playground.
Prefer to jump straight into code? View the example repo on GitHub.