On this page
Guides
How to Make a Angular App With ComPDFKit for Web
Prerequisites
To get started, you'll need:
- The latest stable version of Node.js.
- A package manager compatible with npm(8.5.0 or later).
- Apply the License Key: Contact ComPDFKit's sales team to get a free 30-day license to test the project.
Create a New Project
- Create a new Angular project:
shell
npm init @angular compdfkit-app
When prompted to make a choice, press Enter to accept the default option.
- Change your directory to compdfkit-app:
shell
cd compdfkit-app
Add ComPDFKit for Web
- Install webviewer as a dependency with npm:
shell
npm i @compdfkit_pdf_sdk/webviewer --save
- Add the "webviewer" folder that contains the required static resource files to run the ComPDFKit Web demo, to your project’s public resource folder.The folder you need to copy is
node_modules/@compdfkit_pdf_sdk/webviewer/dist
.
shell
cp -r ./node_modules/@compdfkit_pdf_sdk/webviewer/dist ./src/webviewer
- Add the following to your
angular.json
file. Angular will copy the ComPDFKit library assets to the assets directory before running your app:
json
"assets": [
"src/favicon.ico",
"src/assets",
"src/webviewer",
"src/@compdfkit"
]
Display a PDF
Add the PDF document you want to display to the
src/webviewer/example
directory. You can use our demo document as an example.Replace the contents of
app.component.html
with:
html
<div #viewer class="viewer"></div>
- When using the Web SDK, you need to use the
path
parameter to tell it where the copied static resources are located, if not, they will be relative to the current path.
Replace the contents of app.component.ts
with:
javascript
import { Component, AfterViewInit, ViewChild, ElementRef } from '@angular/core';
import { RouterOutlet } from '@angular/router';
// @ts-ignore
import WebViewer from '@compdfkit_pdf_sdk/webviewer';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
templateUrl: './app.component.html',
styleUrl: './app.component.css'
})
export class AppComponent implements AfterViewInit{
@ViewChild('viewer') viewer!: ElementRef;
ngAfterViewInit(): void {
let docViewer: any;
WebViewer.init({
path: '/',
pdfUrl: '/webviewer/example/developer_guide_web.pdf',
license: '<Input your license here>'
}, this.viewer.nativeElement).then((instance: any) => {
docViewer = instance.docViewer;
docViewer.addEvent('documentloaded', async () => {
console.log('document loaded');
})
})
}
}
- Add the following styles to the
src/app/app.component.css
file:
css
.viewer { width: 100vw; height: 100vh; }
- Start the app and open it in your default browser:
shell
npm start