Build a Blazor PDF viewer with Apryse WebViewer SDK
This guide shows how to build a Blazor PDF viewer using the Apryse WebViewer SDK. You'll learn how to integrate the SDK into a Blazor application to render, view, and interact with PDF documents using the WebViewer UI.
You can also download ready-to-go GitHub samples for Blazor server and Blazor wasm to get started quickly. Additionally, explore the interactive Showcase demo to see WebViewer's full capabilities in action.
Prerequisites
This guide assumes basic familiarity with Blazor development. Before you start:
Install Node.js and npm. We recommend using the latest active LTS release.
Install the .NET 9+ SDK for your platform to build and run your application using C#.
Install Visual Studio Code or another code editor to develop and debug your code.
Get your Apryse trial key.
1. Create a Blazor project
In this section, you’ll create a new Blazor application using the .NET CLI. This project provides the foundation for integrating Apryse WebViewer. If you already have a Blazor app, skip this and continue to Install WebViewer.
Before creating a project, open your terminal and confirm the .NET CLI is installed:
dotnet --versionIf this prints a version number, you're ready to proceed. If not, install the .NET SDK and rerun the command.
In your terminal, go to the directory where you want to create the project.
Create a new
webviewer-blazorproject:
Navigate to your new Blazor project directory, initialize your project, and generate a
package.jsonfile:
The final package.json should look like this:
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 Blazor application.
After navigating to your webviewer-blazor 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 Blazor 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
wwwroot/lib/webviewer/publicdirectory if it doesn't already exist:
Copy all WebViewer static assets from
node_modules/@pdftron/webviewer/publicinto the newwwwroot/lib/webviewer/publicdirectory:
Copy the
webviewer.min.jslibrary file fromnode_modulesto thepublicdirectory:
Your project should now include a similar structure:
4. Create the PDF viewer
In this section, you’ll add WebViewer to your Blazor app by loading the WebViewer script and creating a Razor component that initializes the viewer and renders it in the UI, where it loads and displays a document.
In Visual Studio Code, replace the contents of the
Components/App.razorfile with the following and save:
Create a new
Components/WebViewer.razorfile, add the following code, and save:
Replace the
Components/Pages/Home.razorfile with the following and save:
5. Verify your output
You can now load and display a PDF document in the WebViewer UI. Run your Blazor 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.
Troubleshooting
WebViewer fails to load due to MIME type issues
If WebViewer doesn't load or you see a blank or partially rendered UI, the server may not be serving certain file types correctly. WebViewer includes files with .wasm, .gz, and .br extensions, which require proper MIME types that aren't always configured by default in Blazor.
To fix this, configure the application to serve these file types by updating Program.cs:
Restart the application after making these changes.
Get started video
In this 5-minute video, learn how to install and integrate the Apryse WebViewer SDK into a Blazor project.
Next steps
Last updated
Was this helpful?