Add PDF functionality with TypeScript
Nutrient Web SDK is a PDF library for viewing, annotating, and editing PDFs directly in the browser. Use it to add PDF capabilities to any web application.
This guide walks you through the necessary steps to integrate Nutrient Web SDK into your TypeScript 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.
Prerequisites
A package manager compatible with npm(opens in a new tab). This guide contains usage examples for Yarn(opens in a new tab), pnpm(opens in a new tab), and the npm client(opens in a new tab). The npm client is installed with Node.js by default.
Node.js is required to complete this guide, but it’s not a general requirement for using Nutrient Web SDK.
Creating a new project
If you don’t yet have a project set up, you can create a new one using Vite’s TypeScript template.
Run the following command to create a new Vite project with TypeScript:
Navigate to the project directory:
Terminal window cd nutrient-typescript-exampleInstall the dependencies:
Adding Nutrient Web SDK
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 yourindex.html
file:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><link rel="icon" type="image/svg+xml" href="/vite.svg" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Nutrient Web SDK viewer — TypeScript example</title></head><body><script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.7.0/nutrient-viewer.js"></script><div class="container"></div><script type="module" src="/src/main.ts"></script></body></html>You’re now ready to use Nutrient Web SDK and reference
window.NutrientViewer
in 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:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8" /><link rel="icon" type="image/svg+xml" href="/vite.svg" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Nutrient Web SDK viewer — TypeScript example</title></head><body><script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.7.0/nutrient-viewer.js"></script><div class="container"></div><script type="module" src="/src/main.ts"></script></body></html>Add a configuration in Vite to copy the SDK assets to the
public
directory. Install therollup-plugin-copy
package:Terminal window npm i -D rollup-plugin-copyTerminal window pnpm add -D rollup-plugin-copyTerminal window yarn add -D rollup-plugin-copyThe SDK loads additional resources at runtime (web workers, fonts, icons) that must be served from your domain. The
rollup-plugin-copy
package automatically copies these assets fromnode_modules
to yourpublic
directory during the build process.Configure asset copying in your Vite configuration:
vite.config.ts import { defineConfig } from "vite";import copy from "rollup-plugin-copy";export default defineConfig({plugins: [copy({targets: [{// Nutrient Web SDK requires its assets to be in the `public` directory so it can load them at runtime.src: "node_modules/@nutrient-sdk/viewer/dist/nutrient-viewer-lib",dest: "public/",},],hook: "buildStart", // Copy assets when the build starts.}),],});You’re now ready to use Nutrient Web SDK locally in your TypeScript + Vite app.
CSS setup requirements
Nutrient Web SDK requires that the mounting container has an explicit width and height before calling
NutrientViewer.load()
. The container cannot be0Ă—0
pixels or the SDK will fail to initialize.
Update the
src/style.css
file with the following content. This declares the height of the viewer element:.container {height: 100vh;}Ensure your viewer container has explicit dimensions. The TypeScript examples in this guide use CSS classes, which is the recommended approach for most projects.
For existing projects: Check for any CSS framework styles that might interfere with container positioning or dimensions, and override them as needed.
Rendering a PDF
Load the PDF file in
src/main.ts
:main.ts import "./style.css";function load() {const container = document.querySelector(".container") as HTMLElement;const { NutrientViewer } = window;// 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",}).then((instance) => {console.log("Nutrient loaded", instance);}).catch(console.error);}}load();Start the development server:
You should see the PDF rendered in the Nutrient Web SDK UI.
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 local installation. For the CDN installation, follow the steps below.
Add the Nutrient Web SDK dependency, if you haven’t already previously. You need the package installed locally to reference the types:
Create a module for custom typings (say
global.d.ts
in thesrc
directory) to reference the built-in typings for the SDK:src/global.d.ts import NutrientViewer from "@nutrient-sdk/viewer";declare global {interface Window {// Nutrient Web SDK will be available on `window.NutrientViewer` once loaded.NutrientViewer?: typeof NutrientViewer;}}Restart the TypeScript server or your editor if needed.
Optimizing the CDN installation
If you use the CDN installation approach in production, Nutrient recommends considering optimizations such as prefetching(opens in a new tab).
Running the project
Start the development server:
Open your browser and navigate to the URL displayed in the terminal (typically http://localhost:5173(opens in a new tab)). You’ll see the PDF document rendered in the viewer.
To build for production, run:
To preview the production build locally, run:
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.