How to build a React.js PDF viewer with Nutrient

Table of contents

    How to build a React.js PDF viewer with Nutrient

    This post provides you with a step-by-step guide on how you can deploy Nutrient’s React.js PDF viewer. React.js is a JavaScript library for building user interfaces, and according to the Stack Overflow 2023 Developer Survey(opens in a new tab), React is the most popular web framework.

    The video tutorial above uses create-react-app. We now recommend using Vite, as detailed below.

    What is a React PDF viewer?

    A React PDF viewer lets you render and view PDF documents in a web browser without the need to download it to your hard drive or use an external application like a PDF reader.

    Nutrient React PDF viewer

    We offer a commercial React.js PDF viewer library that can easily be integrated into your web application. It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.

    • A prebuilt and polished UI for an improved user experience
    • 15+ prebuilt annotation tools to enable document collaboration
    • Support for more file types with client-side PDF, MS Office, and image viewing
    • Dedicated support from engineers to speed up integration

    Example of our React.js PDF viewer

    To demo our React.js PDF viewer, upload a PDF, JPG, PNG, or TIFF file by selecting Open Document. Once your document is displayed in the viewer, try drawing freehand, adding a note, or applying a crop or an eSignature.

    Requirements to get started

    To get started, you’ll need:

    Creating a new React project

    1. Create a new React app using the Vite(opens in a new tab) tool:
    yarn create vite pspdfkit-react-example -- --template react
    npm create vite@latest pspdfkit-react-example -- --template react
    1. Change to the created project directory:
    cd pspdfkit-react-example

    Adding Nutrient to your project

    1. Add the Nutrient dependency:
    yarn add pspdfkit
    npm install --save pspdfkit
    1. Copy the Nutrient Web SDK library assets to the public directory:
    Terminal window
    cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib

    The code above will copy the pspdfkit-lib directory from within node_modules/ into the public/ directory to make it available to the SDK at runtime.

    1. Make sure your public directory contains a pspdfkit-lib directory with the Nutrient library assets.

    Displaying a PDF

    1. Add the PDF document you want to display to the public directory. You can use our demo document as an example. Don’t forget to rename it to document.pdf.

    2. Add a component wrapper for the Nutrient library and save it as components/ViewerComponent.jsx:

    import { useEffect, useRef } from 'react';
    export default function ViewerComponent(props) {
    const containerRef = useRef(null);
    useEffect(() => {
    const container = containerRef.current;
    let PSPDFKit;
    (async function () {
    PSPDFKit = await import('pspdfkit');
    PSPDFKit.unload(container); // Ensure that there's only one PSPDFKit instance.
    await PSPDFKit.load({
    // Container where PSPDFKit should be mounted.
    container,
    // The document to open.
    document: props.document,
    // Use the public directory URL as a base URL. PSPDFKit will download its library assets from here.
    baseUrl: `${window.location.protocol}//${
    window.location.host
    }/${import.meta.env.BASE_URL}`,
    });
    })();
    return () => PSPDFKit && PSPDFKit.unload(container);
    }, []);
    return (
    <div
    ref={containerRef}
    style={{ width: '100%', height: '100vh' }}
    />
    );
    }
    1. Include the newly created component in App.jsx:
    src/App.jsx
    import ViewerComponent from './components/ViewerComponent';
    function App() {
    return (
    <div className="App" style={{ width: '100vw' }}>
    <div className="PDF-viewer">
    <ViewerComponent document={'document.pdf'} />
    </div>
    </div>
    );
    }
    export default App;
    1. Your project structure will now look like this:
    pspdfkit-react-example
    ├── public
    │   ├── pspdfkit-lib
    │   └── document.pdf
    ├── src
    │ ├── components
    │   | └── ViewerComponent.jsx
    | └── App.jsx
    ├── package.json
    └── yarn.lock
    1. Start the app and run it in your default browser:
    yarn run dev
    npm run dev

    Adding even more capabilities

    Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular React.js guides:

    Conclusion

    You should now have our React PDF viewer up and running in your web application. If you hit any snags, don’t hesitate to reach out to our Support team for help.

    You can also deploy our vanilla JavaScript PDF viewer or use one of our many web framework deployment options like Vue.js, Angular, and jQuery. To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.

    Hulya Masharipov

    Hulya Masharipov

    Technical Writer

    Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

    Explore related topics

    FREE TRIAL Ready to get started?