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 UI.
Installation
You can load Nutrient Web SDK directly from Nutrient's content delivery network (CDN). Nutrient maintains the CDN for customers and it's a good way to get started. For more control and flexibility, you can use local installation.
Add the following
<script>
tag in yourindex.html
:index.html <!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>Vite + React + TS</title></head><body><div id="root"></div><script type="module" src="/src/main.tsx"></script></body></html>You're now ready to use the Nutrient Web SDK and reference
window.PSPDFKit
in the client side code.
Add the Nutrient Web SDK (
@nutrient-sdk/viewer
) dependency:Terminal window npm i @nutrient-sdk/viewerTerminal window pnpm add @nutrient-sdk/viewerTerminal window yarn add @nutrient-sdk/viewerIf you tried CDN installation first, make sure to remove the script tag:
index.html <!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>Vite + React + TS</title></head><body><div id="root"></div><script type="module" src="/src/main.tsx"></script></body></html>Add a config 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-copyUpdate your Vite config:
vite.config.ts import { defineConfig } from "vite";import react from "@vitejs/plugin-react";import copy from "rollup-plugin-copy";export default defineConfig({plugins: [copy({targets: [{// The Nutrient Web SDK requires its assets to be in the `public` directory so it can load them.src: "node_modules/@nutrient-sdk/viewer/dist/nutrient-viewer-lib",dest: "public/",},],hook: "buildStart",}),react(),],});Update
App.tsx
or any other component where you're usingNutrientViewer
:App.tsx import { useEffect, useRef } from "react";function App() {const containerRef = useRef(null);useEffect(() => {const container = containerRef.current;let cleanup = () => {};(async () => {const NutrientViewer = (await import("@nutrient-sdk/viewer")).default;// Ensure there's only one NutrientViewer instanceNutrientViewer.unload(container);if (container && NutrientViewer) {NutrientViewer.load({container,// You can also specify a file in public directory, for example /document.pdfdocument: "https://www.nutrient.io/downloads/pspdfkit-web-demo.pdf",// baseUrl tells the SDK where to load the assets frombaseUrl: `${window.location.protocol}//${window.location.host}/${import.meta.env.PUBLIC_URL ?? ""}`,});}cleanup = () => {NutrientViewer.unload(container);};})();return cleanup;}, []);// Set the container height and widthreturn <div ref={containerRef} style={{ height: "100vh", width: "100vw" }} />;}export default App;You're now ready to use the Nutrient Web SDK locally in your React + Vite app.
Render a PDF
Load the PDF file in
App.tsx
or any other component of your choice usingNutrientViewer
:App.tsx import { useEffect, useRef } from "react";function App() {const containerRef = useRef(null);useEffect(() => {const container = containerRef.current;const { NutrientViewer } = window;if (container && NutrientViewer) {NutrientViewer.load({container,// You can also specify a file in public directory, for example /document.pdfdocument: "https://www.nutrient.io/downloads/pspdfkit-web-demo.pdf",});}return () => {NutrientViewer?.unload(container);};}, []);// we also need to set the container height and widthreturn <div ref={containerRef} style={{ height: "100vh", width: "100vw" }} />;}export default App;Start the development server:
Terminal window npm run devTerminal window pnpm run devTerminal window yarn run devYou should see the PDF rendered in the Nutrient Web SDK UI.
Next steps
Further steps to set 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 these steps:
Add the Nutrient Web SDK dependency, if not done previously. You need the package installed locally in order to reference the types:
Terminal window npm i @nutrient-sdk/viewerTerminal window pnpm add @nutrient-sdk/viewerTerminal window yarn add @nutrient-sdk/viewerCreate 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 loadedNutrientViewer?: typeof NutrientViewer;}}Restart TS server or your editor if needed.
Optimize CDN installation
If you use the CDN installation approach in production, Nutrient recommends considering optimizations such as prefetching.