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 in your NextJS layout component (for example,
layout.tsx
):layout.tsx import Script from "next/script";export default function RootLayout({children,}: Readonly<{children: React.ReactNode;}>) {return (<html lang="en"><head><Script// Load before the page becomes interactive to reference window.NutrientViewer in the clientstrategy="beforeInteractive"/></head><body>{children}</body></html>);}You're now ready to use the Nutrient Web SDK and reference
window.NutrientViewer
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/viewerCopy the SDK assets to the
public
directory:Terminal window cp -R ./node_modules/@nutrient-sdk/viewer/dist/ public/nutrient-viewerMake sure your public directory contains a
nutrient-viewer
directory with the Nutrient library assets:Directorypublic
Directorynutrient-viewer
- ...
Directorysrc
- ...
- package.json
- ...
Set
src
for the Nutrient Web SDK script in your layout component:layout.tsx export default function RootLayout({children,}: Readonly<{children: React.ReactNode;}>) {return (<html lang="en"><head><Scriptsrc="/nutrient-viewer/nutrient-viewer.js"strategy="beforeInteractive"/></head><body>{children}</body></html>);}Finally, update the
next.config.ts
to exclude bundling@nutrient-sdk/viewer
in the client side bundle:next.config.ts import type { NextConfig } from "next";const nextConfig: NextConfig = {/* config options here */webpack: (config, { isServer }) => {if (!isServer) {config.externals = config.externals || [];config.externals.push({"@nutrient-sdk/viewer": "@nutrient-sdk/viewer",});}return config;},experimental: {turbo: {resolveAlias: {"@nutrient-sdk/viewer": "@nutrient-sdk/viewer",},},},};export default nextConfig;You're now ready to use the Nutrient Web SDK locally in your NextJS app.
Render a PDF
This guide covers integration using app router. You can use a similar approach for pages router.
Load the PDF file into a component (for example,
page.tsx
) usingNutrientViewer
:page.tsx // Only render the SDK on the client-side"use client";import React, { useEffect, useRef } from "react";export default function Home() {const containerRef = useRef(null);useEffect(() => {const container = containerRef.current;const { NutrientViewer } = window;if (container && NutrientViewer) {NutrientViewer.load({container,// You can specify a file in public directory, for example /document.pdfdocument: "https://www.nutrient.io/downloads/pspdfkit-web-demo.pdf",});}return () => {NutrientViewer?.unload(container);};}, []);// You must set the container height and widthreturn <div ref={containerRef} style={{ height: "100vh", width: "100%" }} />;}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.
Automatically copy assets
Nutrient Web SDK requires its assets to be in the public
directory so it can load them as and when needed.
This is required during the initial setup and whenever you update the SDK version. You can add a script in package.json
to automate this. Set the script to run before starting the development server or building the app:
{ "scripts": { "copy-assets": "cp -R ./node_modules/@nutrient-sdk/viewer/dist/ public/nutrient-viewer", "dev": "npm run copy-assets && next dev --turbo", "build": "npm run copy-assets && next build" }}
You can include the SDK assets public/nutrient-viewer
in your .gitignore
file to avoid committing them to your repository.
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:
Terminal window npm i @nutrient-sdk/viewerTerminal window pnpm add @nutrient-sdk/viewerTerminal window yarn add @nutrient-sdk/viewerCreate a module for custom typings (for example,
global.d.ts
in the root directory) to reference the built-in typings for the SDK: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 the TS server or your editor if needed.
Optimize CDN installation
If you use the CDN installation approach in production, Nutrient recommends optimizations such as prefetching.