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 into your project. By the end, you'll be able to render a PDF document in the UI.
Installation
Add the Nutrient dependency:
Terminal window npm i @nutrient-sdk/viewerTerminal window pnpm add @nutrient-sdk/viewerTerminal window yarn add @nutrient-sdk/viewerRun the following command to install the necessary dependencies for
webpack
, create aconfig
directory, and place yourwebpack
configuration file inside it:Terminal window npm i -D webpack webpack-cli webpack-dev-server ts-loader typescript html-webpack-plugin cross-env copy-webpack-plugin clean-webpack-pluginmkdir config && touch config/webpack.jsIn the
config
folder, add the following code to thewebpack.js
file:const path = require('path');const HtmlWebpackPlugin = require('html-webpack-plugin');const CopyWebpackPlugin = require('copy-webpack-plugin');const filesToCopy = [// Nutrient files.{from: './node_modules/nutrient-sdk/viewer/dist',to: './nutrient-lib',},// Application CSS.{from: './src/index.css',to: './index.css',},// Example PDF.{from: './assets/document.pdf',to: './document.pdf',},];/*** webpack main configuration object.*/const config = {entry: path.resolve(__dirname, '../src/index.ts'),mode: 'development',devtool: 'inline-source-map',output: {path: path.resolve(__dirname, '../dist'),filename: '[name].js',},resolve: {extensions: ['.ts', '.tsx', '.js'],},module: {rules: [// All files with a `.ts` or `.tsx` extension will be handled by `ts-loader`.{test: /\.tsx?$/,loader: 'ts-loader',exclude: /node_modules/,},],},plugins: [new HtmlWebpackPlugin({template: './src/index.html',}),// Copy the WASM/ASM and CSS files to the `output.path`.new CopyWebpackPlugin({ patterns: filesToCopy }),],optimization: {splitChunks: {cacheGroups: {// Creates a `vendor.js` bundle that contains external libraries (including `pspdfkit.js`).vendor: {test: /node_modules/,chunks: 'initial',name: 'vendor',priority: 10,enforce: true,},},},},};module.exports = config;
Render a PDF
Rename the PDF document you want to display in your application to
document.pdf
, and place it in theassets
directory. You can use this demo document as an example.In the
src
folder, create anindex.html
file with the following content. This adds an empty<div>
element where PSPDFKit is loaded:<!DOCTYPE html><html><head><title>Nutrient for Web — TypeScript example</title><link rel="stylesheet" href="index.css" /></head><body><div class="container"></div></body></html>In the
src
folder, create anindex.css
file with the following content. This declares the height of the PSPDFKit element:.container {height: 100vh;}In the
src
folder, create anindex.ts
file with the following content:import NutrientViewer from '@nutrient-sdk/viewer';function load(document: string) {console.log(`Loading ${document}...`);NutrientViewer.load({document,container: '.container',}).then((instance) => {console.log('Nutrient loaded', instance);}).catch(console.error);}load('document.pdf'); // Pass your PDF file.You should see the PDF rendered in the UI when you serve the project locally.