This guide shows how to build a Vite PDF viewer using the Apryse WebViewer SDK. You’ll learn how to integrate the SDK into React, Vanilla, Vue, or Svelte applications to render, view, and interact with PDF documents in the WebViewer UI.
For more, explore the interactive Showcase demo to see WebViewer's full capabilities in action.
This guide assumes basic familiarity with Vite 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 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-vite project using a minimal setup:Flags are used to skip prompts during project configuration. These flags determine which framework is used and select JavaScript as the variant. Select different options if preferred.
3. If prompted to install with npm and start now, select No.
4. Navigate to your new Vite 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 Vite application.
After navigating to your webviewer-vite 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 Vite project, you must copy these assets into the public directory so they can be served correctly. We'll use a Vite plugin to copy WebViewer's static assets into the public folder. For more, see Copying WebViewer static assets.
2. In Visual Studio Code, update the vite.config.js file to import the viteStaticCopy plugin and save. If you don't see a vite.config.js file in your project, create one at the project root and add the necessary configuration:
vite.config.js
vite.config.js
vite.config.js
vite.config.js
Your vite.config.js file may look slightly different depending on the framework you’re using. The key requirement is to add the viteStaticCopy plugin to the plugins array. Make sure to import the plugin correctly:
Next, instantiate the Apryse WebViewer SDK and mount it in your Vite 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
If you're signed in with an Apryse account, your license key is automatically prepopulated in all code snippets.
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.
src/main.js file with the following and save:src/main.js
If you're signed in with an Apryse account, your license key is automatically prepopulated in all code snippets.
2. Replace the src/style.css with the following and save:
src/style.css
src/components/HelloWorld.vue file with the following and save:src/components/HelloWorld.vue
If you're signed in with an Apryse account, your license key is automatically prepopulated in all code snippets.
2. Replace the contents of the src/App.vue file with the following and save:
src/App.vue
src/lib/WebViewer.svelte file, add the following code to it, and save:src/lib/WebViewer.svelte
If you're signed in with an Apryse account, your license key is automatically prepopulated in all code snippets.
2. Replace the contents of the src/App.svelte file with the following and save:
src/App.svelte
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.
If you created your project using Vite’s scaffolding tool, the default CSS files included with the template can interfere with WebViewer’s layout. If the viewer doesn't appear or renders incorrectly, remove or clear the styles from any default CSS files (such as style.css or index.css).
In this 4-minute video, learn the fundamentals of Apryse WebViewer, including key concepts for installation and initialization in any web application.
Overview of Apryse WebViewer fundamentals, including installation and initialization.
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales