Add PDF functionality with Blazor
Nutrient Web SDK is a JavaScript PDF library for viewing, annotating, and editing PDFs directly in the browser. Use it to add PDF capabilities to any web app.
This guide walks you through the steps to integrate Nutrient Web SDK into your project. By the end, you’ll be able to render a PDF document in the user interface (UI).
You can test the SDK capabilities in our playground.
Prefer to jump straight to code? View the example repo on GitHub.
Installation
You can load Nutrient Web SDK directly from Nutrient’s content delivery network (CDN). Nutrient maintains the CDN for customers, and it’s our recommended way to get started. For more control and flexibility, use the local installation option.
Add the following script tag in your
Components/App.razorfile before the</body>tag:<script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.9.1/nutrient-viewer.js"></script>You’re now ready to use Nutrient Web SDK and reference
NutrientViewerin your Blazor components.
Nutrient Viewer library files are distributed as an archive that you extract manually:
- Download the framework here(opens in a new tab). The download will start immediately and will save a
.tar.gzarchive likePSPDFKit-Web-binary-1.0.0.tar.gzto your computer. - Once the download is complete, extract the archive and copy the entire contents of its
distfolder to thewwwrootdirectory in your project. - Make sure your
wwwrootfolder contains thenutrient-viewer.jsfile and anutrient-viewer-libdirectory with library assets.
Rendering a PDF
These steps cover both server and WebAssembly.
To clean out the UI, go to the
Components/Layout/MainLayout.razorcomponent and keep only the@Bodyproperty:@inherits LayoutComponentBase@BodyGo to the
Homeroute located under theComponents/Pagesfolder, and replace the contents of the file with the following:@page "/"@rendermode InteractiveServer@inject IJSRuntime JS<div id='nutrient-container' style='background: gray; width: 100vw; height: 100vh; margin: 0 auto;'></div>@code {protected override async void OnAfterRender(bool firstRender){if (firstRender) {await JS.InvokeVoidAsync("loadPDF", "#nutrient-container", "https://www.nutrient.io/downloads/nutrient-web-demo.pdf");}}}Initialize Nutrient Viewer in
Components/App.razorbefore the</body>tag:@* Initialize Nutrient Viewer in Blazor by calling NutrientViewer.load(): *@<script>function loadPDF(container, document) {NutrientViewer.load({container: container,document: document})}</script>Server only — Make sure to add the MIME type mappings to allow conversion of Office files.
Go to
Program.cs.Import
using Microsoft.AspNetCore.StaticFiles;.Add the following code to your app builder:
FileExtensionContentTypeProvider extensionProvider = new();extensionProvider.Mappings.Add(".dll", "application/octet-stream");extensionProvider.Mappings.Add(".dat", "application/octet-stream");extensionProvider.Mappings.Add(".blat", "application/octet-stream");app.UseStaticFiles(new StaticFileOptions{ContentTypeProvider = extensionProvider});
To run the app, select Run > Start Debugging. This will start your app and open it in your default browser.
To clean out the UI, go to the
Components/Layout/MainLayout.razorcomponent and keep only the@Bodyproperty:@inherits LayoutComponentBase@BodyGo to the
Homeroute located under theComponents/Pagesfolder, and replace the contents of the file with the following:@page "/"@rendermode InteractiveServer@inject IJSRuntime JS<div id='nutrient-container' style='background: gray; width: 100vw; height: 100vh; margin: 0 auto;'></div>@code {protected override async void OnAfterRender(bool firstRender){if (firstRender) {await JS.InvokeVoidAsync("loadPDF", "#nutrient-container", "https://www.nutrient.io/downloads/nutrient-web-demo.pdf");}}}Load Nutrient Viewer into
Components/App.razorbefore the</body>tag:@* Include nutrient-viewer.js from your wwwroot directory *@<script src="nutrient-viewer.js"></script>@* Initialize Nutrient Viewer in Blazor by calling NutrientViewer.load(): *@<script>function loadPDF(container, document) {NutrientViewer.load({container: container,document: document})}</script>Server only — Make sure to add the MIME type mappings to allow conversion of Office files.
Go to
Program.cs.Import
using Microsoft.AspNetCore.StaticFiles;.Add the following code to your app builder:
FileExtensionContentTypeProvider extensionProvider = new();extensionProvider.Mappings.Add(".dll", "application/octet-stream");extensionProvider.Mappings.Add(".dat", "application/octet-stream");extensionProvider.Mappings.Add(".blat", "application/octet-stream");app.UseStaticFiles(new StaticFileOptions{ContentTypeProvider = extensionProvider});
To run the app, select Run > Start Debugging. This will start your app and open it in your default browser.
Troubleshooting
If you encounter issues, refer to the common issues guide.
Note for developers using AI coding assistants: To get accurate troubleshooting help, copy the content from the troubleshooting guide and include it in your prompt, along with your specific error.
You can test the SDK capabilities in our playground.
Prefer to jump straight into code? View the example repo on GitHub.