Build a Vite PDF viewer with Apryse WebViewer SDK
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.
Prerequisites
This guide assumes basic familiarity with Vite development. Before you start:
Install Node.js and npm. See the Vite documentation for version details.
Install Visual Studio Code or another code editor to develop and debug your code.
Get your Apryse trial key.
1. Create a Vite project
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.
In your terminal, go to the directory where you want to create the project.
Create a new
webviewer-viteproject using a minimal setup:
npm create vite@latest webviewer-vite -- --template reactnpm create vite@latest webviewer-vite -- --template vanillanpm create vite@latest webviewer-vite -- --template vuenpm create vite@latest webviewer-vite -- --template svelteIf prompted to install with npm and start now, select No.
Navigate to your new Vite project directory and install dependencies:
2. Install WebViewer
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:
3. Copy WebViewer assets
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.
From your project directory, run the following to install the vite-plugin-static-copy package:
In Visual Studio Code, update the
vite.config.jsfile to import theviteStaticCopyplugin and save. If you don't see avite.config.jsfile in your project, create one at the project root and add the necessary configuration:
4. Create the PDF viewer
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.
In Visual Studio Code, replace the contents of the
src/App.jsxfile with the following and save:
Replace the contents of the
src/main.jsxfile with the following and save:
In Visual Studio Code, replace the contents of the
src/main.jsfile with the following and save:
Replace the
src/style.csswith the following and save:
In Visual Studio Code, replace the contents of the
src/components/HelloWorld.vuefile with the following and save:
Replace the contents of the
src/App.vuefile with the following and save:
In Visual Studio Code, create a
src/lib/WebViewer.sveltefile, add the following code to it, and save:
Replace the contents of the
src/App.sveltefile with the following and save:
5. Verify your output
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.
From your project directory, run the following command to start the application:
A successful output looks similar to:
Open the localhost URL from your terminal to view the WebViewer UI and PDF document.
Get started video
In this 4-minute video, learn the fundamentals of Apryse WebViewer, including key concepts for installation and initialization in any web application.
Next steps
Last updated
Was this helpful?