Add PDF functionality with React + Vite

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).

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.

  1. Add the following <script> tag in your index.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 src="http://cdn.cloud.pspdfkit.com/pspdfkit-web@1.7.0/nutrient-viewer.js"></script>
    <script type="module" src="/src/main.tsx"></script>
    </body>
    </html>
  2. You’re now ready to use Nutrient Web SDK and reference window.NutrientViewer in the client-side code.

CSS setup requirements

Nutrient Web SDK requires that the mounting container has an explicit width and height before calling NutrientViewer.load(). The container cannot be 0×0 pixels or the SDK will fail to initialize.

  1. For new Vite projects — Remove conflicting CSS from your src/index.css file. The default Vite React template includes CSS that interferes with container dimensions:

    /* src/index.css - Remove these conflicting styles */
    body {
    display: flex;
    place-items: center;
    }
  2. Ensure your viewer container has explicit dimensions. The React examples in this guide use inline styles (style={{ height: "100vh", width: "100vw" }}), which is the recommended approach for most projects.

  3. 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

  1. Load the PDF file in App.tsx:

    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 `/nutrient-web-demo.pdf`.
    document: "http://www.nutrient.io/downloads/nutrient-web-demo.pdf",
    });
    }
    return () => {
    NutrientViewer?.unload(container);
    };
    }, []);
    // Set the container height and width.
    return (
    <div ref={containerRef} style={{ height: "100vh", width: "100vw" }} />
    );
    }
    export default App;
  2. Start the development server:

    Terminal window
    npm run dev
  3. 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 these steps:

  1. Add the Nutrient Web SDK dependency, if not done previously. You need the package installed locally to reference the types:

    Terminal window
    npm i @nutrient-sdk/viewer
  2. Create a module for custom typings (say global.d.ts in the src 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;
    }
    }
  3. 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.