Add PDF functionality with Angular
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
index.htmlfile:src/index.html <!doctype html><html lang="en"><head><meta charset="utf-8" /><title>Angular App</title><base href="/" /><meta name="viewport" content="width=device-width, initial-scale=1" /><link rel="icon" type="image/x-icon" href="favicon.ico" /><script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.10.0/nutrient-viewer.js"></script></head><body><app-root></app-root></body></html>You’re now ready to use Nutrient Web SDK and reference
window.NutrientViewerin the client-side code.
Add the Nutrient Web SDK (
@nutrient-sdk/viewer) dependency:If you tried CDN installation first, make sure to remove the script tag:
src/index.html <script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.10.0/nutrient-viewer.js"></script>You’re now ready to use Nutrient Web SDK locally in your Angular project.
Rendering a PDF
Generate a new PDF viewer component:
Terminal window ng generate component pdf-viewerReplace the contents of
src/app/pdf-viewer/pdf-viewer.component.htmlwith the following:<div class="pdf-viewer"><div id="nutrient-container" style="width: 100%; height: 100vh"></div></div>Replace the contents of
src/app/pdf-viewer/pdf-viewer.component.tswith the following:import { Component, OnInit } from "@angular/core";@Component({selector: "pdf-viewer",templateUrl: "./pdf-viewer.component.html",styleUrls: ["./pdf-viewer.component.css"],standalone: true,})export class PdfViewerComponent implements OnInit {ngOnInit(): void {const { NutrientViewer } = window as any;if (NutrientViewer) {NutrientViewer.load({document: "/assets/document.pdf",container: "#nutrient-container",}).then((instance: any) => {// For the sake of this demo, store the Nutrient instance// on the global object so that you can open the dev tools and// play with the Nutrient API.(window as any).instance = instance;});}}}Update the component where you want to use the PDF viewer — for example,
src/app/app.component.ts— to import thePdfViewerComponent:import { Component } from "@angular/core";import { RouterOutlet } from "@angular/router";import { PdfViewerComponent } from "./pdf-viewer/pdf-viewer.component";@Component({selector: "app-root",imports: [RouterOutlet, PdfViewerComponent],templateUrl: "./app.component.html",styleUrl: "./app.component.css",})export class AppComponent {title = "angular";}Add the
pdf-viewerto the page where you want to display it — for example,src/app/app.component.html:<pdf-viewer></pdf-viewer>Start the development server:
You’ll see the PDF rendered in the Nutrient Web SDK user interface (UI).
Generate a new PDF viewer component:
Terminal window ng generate component pdf-viewerReplace the contents of
src/app/pdf-viewer/pdf-viewer.component.htmlwith the following:<div class="pdf-viewer"><div id="nutrient-container" style="width: 100%; height: 100vh"></div></div>Replace the contents of
src/app/pdf-viewer/pdf-viewer.component.tswith the following:import { Component, OnInit } from "@angular/core";import NutrientViewer from "@nutrient-sdk/viewer";@Component({selector: "pdf-viewer",templateUrl: "./pdf-viewer.component.html",styleUrls: ["./pdf-viewer.component.css"],standalone: true,})export class PdfViewerComponent implements OnInit {ngOnInit(): void {NutrientViewer.load({document: "/assets/document.pdf",container: "#nutrient-container",useCDN: true,}).then((instance) => {// For the sake of this demo, store the Nutrient instance// on the global object so that you can open the dev tools and// play with the Nutrient API.(window as any).instance = instance;});}}Note: Adding
useCDN: truewill load the SDK assets from the CDN ifbaseUrlisn’t provided. This flag was introduced in version 1.9.0. If your setup relies on the previous behavior of autodetecting the assets withoutbaseUrl, you can omit this flag for now, but be aware that this behavior is deprecated. In future versions, loading from CDN will become a default whenbaseUrlisn’t explicitly provided.useCDN: truehas no effect ifbaseUrlis set.Optional: If you decide to self-host the Nutrient Web SDK assets as described in this guide, make sure to provide a
baseUrlin your configuration, as shown below:NutrientViewer.load({document: "/assets/document.pdf",useCDN: true,container: "#nutrient-container",baseUrl: `${location.protocol}//${location.host}/${(window as any).PUBLIC_URL ?? "" // Usually empty for Angular, but supports custom deployments.}`,});Update the component where you want to use the PDF viewer — for example,
src/app/app.component.ts— to import thePdfViewerComponent:import { Component } from "@angular/core";import { RouterOutlet } from "@angular/router";import { PdfViewerComponent } from "./pdf-viewer/pdf-viewer.component";@Component({selector: "app-root",imports: [RouterOutlet, PdfViewerComponent],templateUrl: "./app.component.html",styleUrl: "./app.component.css",})export class AppComponent {title = "angular";}Add the
pdf-viewerto the page where you want to display it — for example,src/app/app.component.html:<pdf-viewer></pdf-viewer>Start the development server:
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.