ng2-pdf-viewer, ngx-extended-pdf-viewer, pdfjs-dist, react-pdf, and vue-pdf-embed are all npm packages designed to render PDF documents in web applications, leveraging Mozilla's PDF.js under the hood. pdfjs-dist is the official, low-level JavaScript library that provides core PDF parsing and rendering capabilities. The other four packages are framework-specific wrappers: ng2-pdf-viewer and ngx-extended-pdf-viewer target Angular, react-pdf targets React, and vue-pdf-embed targets Vue. While they all solve the same fundamental problem — displaying PDFs in the browser — they differ significantly in terms of abstraction level, built-in features (like text selection, search, and navigation controls), rendering approach (canvas vs. SVG), and ease of integration within their respective ecosystems.
Displaying PDFs in the browser is a common requirement, but choosing the right library depends heavily on your framework, feature needs, and performance constraints. All five packages—ng2-pdf-viewer, ngx-extended-pdf-viewer, pdfjs-dist, react-pdf, and vue-pdf-embed—are built on Mozilla’s PDF.js, but they differ significantly in abstraction level, framework integration, and capabilities. Let’s break down how they work in practice.
pdfjs-dist is the official, low-level PDF.js distribution. It gives you full control but requires manual DOM management, worker setup, and rendering logic.
// pdfjs-dist: Manual rendering
import * as pdfjsLib from 'pdfjs-dist';
import { getDocument } from 'pdfjs-dist/webpack';
pdfjsLib.GlobalWorkerOptions.workerSrc = './pdf.worker.min.js';
const loadingTask = getDocument('sample.pdf');
const pdf = await loadingTask.promise;
const page = await pdf.getPage(1);
const canvas = document.getElementById('pdf-canvas');
const context = canvas.getContext('2d');
const viewport = page.getViewport({ scale: 1.5 });
canvas.height = viewport.height;
canvas.width = viewport.width;
await page.render({ canvasContext: context, viewport }).promise;
ng2-pdf-viewer, ngx-extended-pdf-viewer, react-pdf, and vue-pdf-embed are framework-specific wrappers that abstract away this boilerplate.
// ng2-pdf-viewer (Angular)
<pdf-viewer [src]="'sample.pdf'" [page]="1"></pdf-viewer>
// react-pdf (React)
import { Document, Page } from 'react-pdf';
<Document file="sample.pdf">
<Page pageNumber={1} />
</Document>
<!-- vue-pdf-embed (Vue) -->
<template>
<vue-pdf-embed :src="'sample.pdf'" page="1" />
</template>
ngx-extended-pdf-viewer includes full text layer support out of the box, enabling copy-paste, search, and accessibility features without extra code.
<!-- ngx-extended-pdf-viewer -->
<ngx-extended-pdf-viewer
[src]="'sample.pdf'"
[showHandTool]="true"
[showFindButton]="true"
></ngx-extended-pdf-viewer>
react-pdf supports text layers via the renderTextLayer prop, but you must enable it explicitly:
<Page pageNumber={1} renderTextLayer={true} />
ng2-pdf-viewer and vue-pdf-embed do not include text layer rendering by default. You’d need to extend them or use pdfjs-dist directly for this functionality.
pdfjs-dist gives you complete control—you can render text layers manually using page.getTextContent() and pdfjsLib.renderTextLayer().
ngx-extended-pdf-viewer ships with a full toolbar (zoom, rotate, print, download, search, etc.) that’s highly configurable.
<ngx-extended-pdf-viewer
[src]="pdfSrc"
[zoom]="1.5"
[rotation]="90"
[showZoomButtons]="true"
></ngx-extended-pdf-viewer>
react-pdf provides no UI controls—you build your own toolbar and manage state (e.g., current page, zoom level) manually:
const [pageNumber, setPageNumber] = useState(1);
return (
<div>
<button onClick={() => setPageNumber(prev => prev - 1)}>Prev</button>
<Document file="sample.pdf">
<Page pageNumber={pageNumber} />
</Document>
<button onClick={() => setPageNumber(prev => prev + 1)}>Next</button>
</div>
);
ng2-pdf-viewer offers basic navigation events (pageChange, error) but no built-in UI. vue-pdf-embed is similarly minimal—just a single-page embed component.
All libraries rely on PDF.js’s worker for performance. How they handle it varies:
pdfjs-dist: You must configure the worker path manually (as shown earlier).react-pdf: Uses a custom webpack loader or requires explicit worker import:
import { pdfjs } from 'react-pdf';
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.min.js`;
ngx-extended-pdf-viewer: Bundles optimized worker files and handles loading automatically.ng2-pdf-viewer and vue-pdf-embed: Require manual worker setup similar to pdfjs-dist.ngx-extended-pdf-viewer uses SVG rendering by default (with optional canvas), which provides sharp text at any zoom level and better accessibility.
react-pdf, ng2-pdf-viewer, and vue-pdf-embed use canvas rendering, which is faster for complex pages but results in blurry text when zoomed.
pdfjs-dist lets you choose: page.render() for canvas, or page.getTextContent() + renderTextLayer() for SVG-like text.
ngx-extended-pdf-viewer is far more feature-complete than ng2-pdf-viewer. The latter is essentially a basic wrapper with limited customization.react-pdf is the de facto standard, though it requires more manual work for advanced features.vue-pdf-embed is lightweight but only handles single-page rendering. For multi-page or interactive features, you’d likely need to drop down to pdfjs-dist.As of 2024:
ng2-pdf-viewer is still maintained but largely superseded by ngx-extended-pdf-viewer for non-trivial use cases.ngx-extended-pdf-viewer, pdfjs-dist, react-pdf, and vue-pdf-embed are actively maintained with recent releases.| Feature | ng2-pdf-viewer | ngx-extended-pdf-viewer | pdfjs-dist | react-pdf | vue-pdf-embed |
|---|---|---|---|---|---|
| Framework | Angular | Angular | None | React | Vue |
| Text Layer | ❌ | ✅ | ✅ (manual) | ✅ (opt-in) | ❌ |
| Built-in Toolbar | ❌ | ✅ | ❌ | ❌ | ❌ |
| Multi-page View | ✅ | ✅ | ✅ (manual) | ✅ | ❌ (single page) |
| Rendering Mode | Canvas | SVG (default) / Canvas | Both | Canvas | Canvas |
| Worker Handling | Manual | Automatic | Manual | Manual | Manual |
ngx-extended-pdf-viewerreact-pdfvue-pdf-embedpdfjs-distng2-pdf-viewer (but consider if ngx-extended-pdf-viewer’s features justify its size)The key takeaway: Don’t default to the simplest wrapper. If you need text selection, search, or responsive zoom, the extra setup of ngx-extended-pdf-viewer or manual pdfjs-dist usage will save you headaches later.
Choose ng2-pdf-viewer if you're working in an Angular application and need a lightweight, basic PDF viewer that displays single or multiple pages with minimal setup. It's suitable for simple use cases where you don't require text selection, search functionality, or a built-in toolbar, and you're comfortable handling PDF.js worker configuration manually. However, for any advanced requirements, consider ngx-extended-pdf-viewer instead, as this package offers limited extensibility.
Choose ngx-extended-pdf-viewer if you're building an Angular application that demands a full-featured, production-ready PDF viewer with built-in support for text selection, search, zoom, rotation, printing, and a customizable toolbar. It handles worker loading automatically, uses SVG rendering by default for crisp text, and is actively maintained. This is the go-to choice for Angular projects requiring robust PDF interaction without reinventing the wheel.
Choose pdfjs-dist if you need maximum control over PDF rendering, are building a framework-agnostic solution, or require custom behavior not supported by higher-level wrappers. You'll manage DOM elements, worker setup, text layers, and event handling yourself, but gain the flexibility to implement exactly what your application needs. This is ideal for teams with deep PDF.js expertise or those building specialized document viewers.
Choose react-pdf if you're developing a React application and prefer a component-based API that integrates naturally with React's state and lifecycle. It supports text layers (when enabled) and multi-page rendering, but requires you to build your own UI controls for navigation, zoom, and search. It's the standard choice for React projects, balancing ease of use with sufficient customization for most common scenarios.
Choose vue-pdf-embed if you're using Vue and only need to embed a single PDF page without interactive features like text selection, search, or navigation controls. It's a minimal wrapper around PDF.js with straightforward props, but lacks multi-page support and built-in UI. For anything beyond basic static display, you'll likely need to combine it with pdfjs-dist or consider alternative approaches.
PDF Viewer Component for Angular 5+
https://vadimdez.github.io/ng2-pdf-viewer/
https://stackblitz.com/edit/ng2-pdf-viewer
https://medium.com/@vadimdez/render-pdf-in-angular-4-927e31da9c76
npm install ng2-pdf-viewer
Partial Ivy compilated library bundles.
npm install ng2-pdf-viewer@^7.0.0
npm install ng2-pdf-viewer@~3.0.8
In case you're using systemjs see configuration here.
Add PdfViewerModule to your module's imports
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app/app.component';
import { PdfViewerModule } from 'ng2-pdf-viewer';
@NgModule({
imports: [BrowserModule, PdfViewerModule],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
class AppModule {}
platformBrowserDynamic().bootstrapModule(AppModule);
And then use it in your component
import { Component } from '@angular/core';
@Component({
selector: 'example-app',
template: `
<pdf-viewer [src]="pdfSrc"
[render-text]="true"
[original-size]="false"
style="width: 400px; height: 500px"
></pdf-viewer>
`
})
export class AppComponent {
pdfSrc = "https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf";
}
| Property | Type | Required |
|---|---|---|
| [src] | string, object, UInt8Array | Required |
Pass pdf location
[src]="'https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf'"
For more control you can pass options object to [src]. See other attributes for the object here.
Options object for loading protected PDF would be:
{
url: 'https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf',
withCredentials: true
}
| Property | Type | Required |
|---|---|---|
| [page] or [(page)] | number | Required with [show-all]="false" or Optional with [show-all]="true" |
Page number
[page]="1"
supports two way data binding as well
[(page)]="pageVariable"
If you want that the two way data binding actually updates your page variable on page change/scroll - you have to be sure that you define the height of the container, for example:
pdf-viewer {
height: 100vh;
}
| Property | Type | Required |
|---|---|---|
| [stick-to-page] | boolean | Optional |
Sticks view to the page. Works in combination with [show-all]="true" and page.
[stick-to-page]="true"
| Property | Type | Required |
|---|---|---|
| [render-text] | boolean | Optional |
Enable text rendering, allows to select text
[render-text]="true"
| Property | Type | Required |
|---|---|---|
| [render-text-mode] | RenderTextMode | Optional |
Used in combination with [render-text]="true"
Controls if the text layer is enabled, and the selection mode that is used.
0 = RenderTextMode.DISABLED - disable the text selection layer
1 = RenderTextMode.ENABLED - enables the text selection layer
2 = RenderTextMode.ENHANCED - enables enhanced text selection
[render-text-mode]="1"
| Property | Type | Required |
|---|---|---|
| [external-link-target] | string | Optional |
Used in combination with [render-text]="true"
Link target
blanknoneselfparenttop[external-link-target]="'blank'"
| Property | Type | Required |
|---|---|---|
| [rotation] | number | Optional |
Rotate PDF
Allowed step is 90 degree, ex. 0, 90, 180
[rotation]="90"
| Property | Type | Required |
|---|---|---|
| [zoom] | number | Optional |
Zoom pdf
[zoom]="0.5"
| Property | Type | Required |
|---|---|---|
| [zoom-scale] | 'page-width'|'page-fit'|'page-height' | Optional |
Defines how the Zoom scale is computed when [original-size]="false", by default set to 'page-width'.
'page-width' with zoom of 1 will display a page width that take all the possible horizontal space in the container
'page-height' with zoom of 1 will display a page height that take all the possible vertical space in the container
'page-fit' with zoom of 1 will display a page that will be scaled to either width or height to fit completely in the container
[zoom-scale]="'page-width'"
| Property | Type | Required |
|---|---|---|
| [original-size] | boolean | Optional |
[original-size]="true"
| Property | Type | Required |
|---|---|---|
| [fit-to-page] | boolean | Optional |
Works in combination with [original-size]="true". You can show your document in original size, and make sure that it's not bigger then container block.
[fit-to-page]="false"
| Property | Type | Required |
|---|---|---|
| [show-all] | boolean | Optional |
Show single or all pages altogether
[show-all]="true"
| Property | Type | Required |
|---|---|---|
| [autoresize] | boolean | Optional |
Turn on or off auto resize.
!Important To make [autoresize] work - make sure that [original-size]="false" and pdf-viewer tag has max-width or display are set.
[autoresize]="true"
| Property | Type | Required |
|---|---|---|
| [c-maps-url] | string | Optional |
Url for non-latin characters source maps.
[c-maps-url]="'assets/cmaps/'"
Default url is: https://unpkg.com/pdfjs-dist@2.0.550/cmaps/
To serve cmaps on your own you need to copy node_modules/pdfjs-dist/cmaps to assets/cmaps.
| Property | Type | Required |
|---|---|---|
| [show-borders] | boolean | Optional |
Show page borders
[show-borders]="true"
| Property | Type | Required |
|---|---|---|
| (after-load-complete) | callback | Optional |
Get PDF information with callback
First define callback function "callBackFn" in your controller,
callBackFn(pdf: PDFDocumentProxy) {
// do anything with "pdf"
}
And then use it in your template:
(after-load-complete)="callBackFn($event)"
| Property | Type | Required |
|---|---|---|
| (page-rendered) | callback | Optional |
Get event when a page is rendered. Called for every page rendered.
Define callback in your component:
pageRendered(e: CustomEvent) {
console.log('(page-rendered)', e);
}
And then bind it to <pdf-viewer>:
(page-rendered)="pageRendered($event)"
| Property | Type | Required |
|---|---|---|
| (pages-initialized) | callback | Optional |
Get event when the pages are initialized.
Define callback in your component:
pageInitialized(e: CustomEvent) {
console.log('(pages-initialized)', e);
}
And then bind it to <pdf-viewer>:
(pages-initialized)="pageInitialized($event)"
| Property | Type | Required |
|---|---|---|
| (text-layer-rendered) | callback | Optional |
Get event when a text layer is rendered.
Define callback in your component:
textLayerRendered(e: CustomEvent) {
console.log('(text-layer-rendered)', e);
}
And then bind it to <pdf-viewer>:
(text-layer-rendered)="textLayerRendered($event)"
| Property | Type | Required |
|---|---|---|
| (error) | callback | Optional |
Error handling callback
Define callback in your component's class
onError(error: any) {
// do anything
}
Then add it to pdf-component in component's template
(error)="onError($event)"
| Property | Type | Required |
|---|---|---|
| (on-progress) | callback | Optional |
Loading progress callback - provides progress information total and loaded bytes. Is called several times during pdf loading phase.
Define callback in your component's class
onProgress(progressData: PDFProgressData) {
// do anything with progress data. For example progress indicator
}
Then add it to pdf-component in component's template
(on-progress)="onProgress($event)"
In your html template add input:
<input (change)="onFileSelected()" type="file" id="file">
and then add onFileSelected method to your component:
onFileSelected() {
let $img: any = document.querySelector('#file');
if (typeof (FileReader) !== 'undefined') {
let reader = new FileReader();
reader.onload = (e: any) => {
this.pdfSrc = e.target.result;
};
reader.readAsArrayBuffer($img.files[0]);
}
}
By default the worker is loaded from cdn.jsdelivr.net.
In your code update path to the worker to be for example /pdf.worker.mjs
(window as any).pdfWorkerSrc = '/pdf.worker.mjs';
This should be set before pdf-viewer component is rendered.
If you ever have a (super rare) edge case where you run in an environment that multiple components are somehow loaded within the same web page, sharing the same window, but using different versions of pdf.worker, support has been added. You can do the above, except that you can append the specific version of pdfjs required and override the custom path just for that version. This way setting the global window var won't conflict.
(window as any)["pdfWorkerSrc2.14.305"] = '/pdf.worker.mjs';
Use eventBus for the search functionality.
In your component's ts file:
pdf-viewer component,search() like this:@ViewChild(PdfViewerComponent) private pdfComponent: PdfViewerComponent;
search(stringToSearch: string) {
this.pdfComponent.eventBus.dispatch('find', {
query: stringToSearch, type: 'again', caseSensitive: false, findPrevious: undefined, highlightAll: true, phraseSearch: true
});
}
If this project help you reduce time to develop, you can give me a cup of tea :)