Build a Next.js PDF viewer with Apryse WebViewer SDK
This guide shows how to build a Next.js PDF viewer using the Apryse WebViewer SDK. You'll learn how to integrate the SDK into a Next.js application to render, view, and interact with PDF documents using the WebViewer UI. This guide uses JavaScript examples. TypeScript projects will require minor adjustments.
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.
Prerequisites
This guide assumes basic familiarity with Next.js development. Before you start:
Install Node.js and npm. See the Next.js 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 Next.js project
In this section, you’ll create a new Next.js application using npm. This project provides the foundation for integrating Apryse WebViewer. If you already have a Next.js 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-nextjsproject using a minimal setup:
npx --yes create-next-app@latest webviewer-nextjs --js --no-eslint --no-react-compiler --no-tailwind --no-src-dir --app --yesNavigate to your new Next.js 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 Next.js application.
After navigating to your webviewer-nextjs 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 Next.js project, you must copy these assets into the public directory so they can be served correctly. For more, see Copying WebViewer static assets.
From your project root, create the
public/lib/webviewerdirectory if it doesn't already exist:
Copy all WebViewer static assets from
node_modules/@pdftron/webviewer/publicinto the newpublic/lib/webviewerdirectory:
Your project should now include a similar structure:
4. Create the PDF viewer
In this section, you'll add WebViewer to your Next.js app by creating a component and initializing the viewer. This mounts the WebViewer UI and loads a document in your application.
1. Create a components folder in your project's root:
Create a
webviewer.jsfile in thecomponentsfolder:
In Visual Studio Code, add this code to the
webviewer.jsfile and save:
Replace the contents of the
app/page.jsfile with the following and save:
5. Verify your output
You can now load and display a PDF document in the WebViewer UI. Run your Next.js 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 how to install and integrate the Apryse WebViewer SDK into a Next.js project using TypeScript. You’ll set up the PDF viewer, enable content editing, and explore the DOCX and Spreadsheet Editors.
Troubleshooting
This section helps you resolve specific build errors when integrating WebViewer into a Next.js application.
The viewer renders as an empty box
If the page loads but the viewer area stays empty with no visible error, WebViewer can't load its static assets. Check the browser console for an error such as:
The path option doesn't point to your local copy of the WebViewer assets. Set it to the directory you created in the Copy WebViewer assets section (for this guide, it is the /lib/webviewer) directory). Don't point it at an external CDN or the npm package path.
Build fails with a Server Component error
If the build fails with an error such as:
WebViewer must run in a Client Component because it relies on browser APIs. Add the 'use client' directive as the first line of the file that initializes WebViewer, as shown in the webviewer.js file example in the Create the PDF viewer section.
Build fails with an ssr: false error
If the build fails with an error such as:
The WebViewer import was moved into app/page.js using next/dynamic. Instead, load WebViewer with the native import('@pdftron/webviewer') call inside the useEffect hook in webviewer.js, as shown in the Create the PDF viewer section. The useEffect hook runs only in the browser, so no ssr: false workaround is needed.
Next steps
Last updated
Was this helpful?