Create React App was once the standard for scaffolding React applications, but it has since been deprecated in favor of tools like Vite. This guide shows how to build a React PDF viewer using the Apryse WebViewer SDK with React and Vite. You’ll learn how to integrate the SDK into a React application to render, view, and interact with PDF documents using the WebViewer UI.
You can also download a ready-to-use GitHub sample to get started quickly, or explore the interactive Showcase demo to see WebViewer's full capabilities in action.
This guide assumes basic familiarity with React development. Before you start:
Apryse collects some data regarding your usage of the SDK for product improvement.
If you wish to continue without data collection, contact us and we will email you a no-tracking trial key for you to get started.
In this section, you’ll create a new React with Vite application using npm. This project provides the foundation for integrating Apryse WebViewer. If you already have a React app, skip this and continue to Install WebViewer.
webviewer-react project using a minimal setup:Flags are used to skip prompts during project configuration. The Vite setup selects React as the framework and uses Javascript. Select different options if preferred.
3. If prompted to install with npm and start now, select No.
4. Navigate to your new React project directory and install dependencies:
Next, install the Apryse WebViewer SDK using npm. This command adds the WebViewer package to your project, allowing you to integrate the PDF viewer and editor into your React application.
After navigating to your webviewer-react project directory, run the following command to install WebViewer:
WebViewer needs access to its static assets at runtime, including WebAssembly modules, HTML, and CSS files. In a React project, you must copy these assets into the public directory so they can be served correctly. For more, see Copying WebViewer static assets.
1. In your terminal, create the public/lib/webviewer directory if it doesn't already exist:
2. Copy all WebViewer static assets from node_modules/@pdftron/webviewer/public into the new public/lib/webviewer directory:
Your project should now include a similar structure:
The path option used when initializing WebViewer must point to this directory (for example, /lib/webviewer). If the path is incorrect, WebViewer will fail to load.
Next, instantiate the Apryse WebViewer SDK and mount it in your React project. This involves creating a container for the viewer and initializing WebViewer so it loads correctly at runtime.
src/App.jsx file with the following and save:src/App.jsx
2. Replace the contents of the src/main.jsx file with the following and save:
src/main.jsx
In React 18 and later, useEffect may run twice in development when Strict Mode is enabled (which Vite uses by default). This can cause WebViewer to initialize twice and to display a duplicate instance. This behavior is expected and doesn't affect production builds.
For this example, you can avoid duplicate initialization by removing the StrictMode wrapper in src/main.jsx. However, note that Strict Mode helps identify issues like memory leaks, so removing it isn't recommended for production or more complex applications.
You can now load and display a PDF document in the WebViewer UI. Run your React application to launch WebViewer and see the PDF in your browser.
A successful output looks similar to:
2. Open the localhost URL from your terminal to view the WebViewer UI and PDF document.
In this 7-minute video, learn how to install and integrate the Apryse WebViewer SDK into a React project built with Vite.
Integrate the WebViewer SDK in a React with Vite application.
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales