Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

angular-pdf-view

mykhalkevych319MIT1.0.1TypeScript support: included

Angular 5+ component for rendering PDF

angular, pdf, angular, angular-pdf, angular2-pdf, ng2, ng2-pdf, angular5

readme

Angular 5+ PDF Viewer

downloads npm version Gitter PayPal donate button

PDF Viewer Component for Angular 5+

Demo page

https://vadimdez.github.io/ng2-pdf-viewer/

Stackblitz Example

https://stackblitz.com/edit/ng2-pdf-viewer

Blog post

https://medium.com/@vadimdez/render-pdf-in-angular-4-927e31da9c76

Overview

Install

npm install angular-pdf-view --save

Note: For angular 4 or less use version 3.0.8

Usage

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 'angular-pdf-view';

@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: `
  <div>
      <label>PDF src</label>
      <input type="text" placeholder="PDF src" [(ngModel)]="pdfSrc">
  </div>
  <pdf-viewer [src]="pdfSrc" 
              [render-text]="true"
              style="display: block;"
  ></pdf-viewer>
  `
})
export class AppComponent {
  pdfSrc: string = '/pdf-test.pdf';
}

Options

[src]

Property Type Required
[src] string, object, UInt8Array Required

Pass pdf location

[src]="'https://vadimdez.github.io/ng2-pdf-viewer/pdf-test.pdf'"

For more control you can pass options object to [src].

Options object for loading protected PDF would be

 {
  url: 'https://vadimdez.github.io/ng2-pdf-viewer/pdf-test.pdf',
  withCredentials: true
 }

See more attributes here.

[page]

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"

[stick-to-page]

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"

[render-text]

Property Type Required
[render-text] boolean Optional

Enable text rendering, allows to select text

[render-text]="true"

[render-text-mode]

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

  • blank
  • none
  • self
  • parent
  • top
    [external-link-target]="'blank'"

[rotation]

Property Type Required
[rotation] number Optional

Rotate PDF

Allowed step is 90 degree, ex. 0, 90, 180

[rotation]="90"

[zoom]

Property Type Required
[zoom] number Optional

Zoom pdf

[zoom]="0.5"

[original-size]

Property Type Required
[original-size] boolean Optional
  • if set to true - size will be as same as original document
  • if set to false - size will be as same as container block
[original-size]="true"

[fit-to-page]

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"

[show-all]

Property Type Required
[show-all] boolean Optional

Show single or all pages altogether

[show-all]="true"

[autoresize]

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"

[c-maps-url]

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.

(after-load-complete)

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)"

(page-rendered)

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)"

(text-layer-rendered)

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)"

(error)

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)"

(on-progress)

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)"

Render local PDF file

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]);
  }
}

Set custom path to the worker

By default the worker is loaded from cdnjs.cloudflare.com.

In your code update path to the worker to be for example /pdf.worker.js

(<any>window).pdfWorkerSrc = '/pdf.worker.js';

This should be set before pdf-viewer component is rendered.

Search in the PDF

Use pdfFindController for search functionality.

In your component's ts file:

  • Add reference to pdf-viewer,
  • then when needed execute search()
@ViewChild(PdfViewerComponent) private pdfComponent: PdfViewerComponent;

search(stringToSearch: string) {
  this.pdfComponent.pdfFindController.executeCommand('find', {
    caseSensitive: false, findPrevious: undefined, highlightAll: true, phraseSearch: true, query: stringToSearch
  });
}

Contribute

See CONTRIBUTING.md

License

MIT © Vadym Yatsyuk

changelog

Changelog

5.2.5

  • [#465] - fix: Listen for textlayerrendered events from eventBus

5.2.4

  • [#457] - fix: allow pdfjs typings peer dependency v2
  • [#408] - Regression: Version 5.2 Broke (page-rendered)
  • [#420] - PR for Issue 408

5.2.3

  • [#402] - Page is not working

5.2.2

  • [#409] - suppress z-lib warning in angular6+
  • [#322] - Angular 6 support

5.2.1

  • [#371] - Cannot read property 'version' of undefined when rendering on the server

5.2.0

  • [#393] - Fix for broken search functionality when showPage=false
  • [#375] - Search and highlight not working when [show-all]="false" [page]="4"
  • [#314] - Search functionality not working if i set show all false.
  • [#293] - Highlight on single page load

5.1.3

  • [#390] - enable setting render text mode to enchanced

You can now set text rendering mode to RenderTextMode.DISABLED, RenderTextMode.ENABLED or RenderTextMode.ENHANCED

[render-text-mode]="1"

5.1.2

  • [#366] - add text-layer-rendered event emitter

5.1.1

  • [#338] - support for non-latin characters
  • [#324] - PDF preview does not display Chinese

5.1.0

  • [#338] - Upgrade dependency
  • [#336] - fix: Cleanup unused pdf document proxies

5.0.1

  • [#311] - Update pdfjs
  • [#308] - Not working on IOS on both Chrome and Safari
  • [#302] - ng2-pdf-viewer dependencies have conflicts with webpack >= 3

Breaking changes

PDFJS is not attached to the window therefore setting custom path to the worker changed to window.pdfWorkerSrc:

(<any>window).pdfWorkerSrc = '/pdf.worker.js';

4.1.2

  • [#281] - Fix error on resize during loading of pdf

4.1.1

  • [#260] - Public PDFFindController
  • [#279] - make pdfFindController public

Features

pdfFindController is now public, that means you can now execute a search on the pdf.

In your component's ts file:

  • Add reference to pdf-viewer,
  • then when needed execute search()
@ViewChild(PdfViewerComponent) private pdfComponent: PdfViewerComponent;

search(stringToSearch: string) {
  this.pdfComponent.pdfFindController.executeCommand('find', {
    caseSensitive: false, findPrevious: undefined, highlightAll: true, phraseSearch: true, query: stringToSearch
  });
}

4.1.0

  • [#274] - Page two way data binding. Do not reset page to 1.
  • [#265] - adding after-render-complete emitter
  • [#81] - Add a rendered event in promise

Page rendered event is added, use (page-rendered)=callback($event)

4.0.0

  • [#268] - Feature/restructure
  • [#209] - Angular 5

Breaking change

Location of bundles is changed.

Therefor SYSTEMJS config is now updated to:

var map = {
    'ng2-pdf-viewer': 'node_modules/ng2-pdf-viewer/bundles',
    'pdfjs-dist': 'node_modules/pdfjs-dist'
}

and then add to packages

var packages = {
    'ng2-pdf-viewer': { defaultExtension: 'js' },
    'pdfjs-dist': { defaultExtension: 'js' }
}

3.0.8

  • [#63] - How to scroll to page?
  • [#233] - Bookmark option

Features

Now both pdfViewer and pdfLinkService are public.

You can now scroll to a page from code:

// define view child
@ViewChild(PdfViewerComponent) private pdfComponent: PdfViewerComponent;

// ...

scrollToPage(page: number) {
  this.pdfComponent.pdfViewer.scrollPageIntoView({
    pageNumber: page
  });
}

3.0.6

  • [#200] - PDFDocumentProxy, PDFProgressData are undefined for compiler

Now you can import types where you need them directly from 'ng2-pdf-viewer' package:

import { PDFDocumentProxy, PDFProgressData } from 'ng2-pdf-viewer';

3.0.5

  • [#238] - added @types/pdf as peer dependency

3.0.4

  • [#162] - Worker Source
  • [#232] - Don't overwrite PDFJS.workerSrc if already set
  • [#226] - Update Size scale Error
  • [#237] - Prevent throwing error: "PDFViewer._setScale: '0' is an unknown zoom value"
  • [#182] - Documentation: Load preview for local files

Set custom path for worker:

In your code update path to the worker to be /pdf.worker.js

(<any>window).PDFJS.workerSrc = '/pdf.worker.js';

This should be set before pdf-viewer component is rendered.

3.0.3

  • [#168] - Dependency to a prebuilt-version of pdf.js (pdfjs-dist)
  • [#195] - progressData.total is undefined
  • [#222] - [render-text]="false" is not working

3.0.2

  • [#173] - SSR support

3.0.1

  • [#194] - Fit original document into the viewport.
  • [#206] - Updated Readme for new [fit-to-page] option.

3.0.0

  • [#196] - ng2-pdf-viewer breaks the AOT build with Angular CLI 1.5.0
  • [#187] - ng build fails when AOT is enabled

BREAKING CHANGES

Now ng2-pdf-viewer exports Module instead of Component. That means you have to import PdfViewerModule instead of PdfViewerComponent.

import { PdfViewerModule } from 'ng2-pdf-viewer';

@NgModule({
  imports: [PdfViewerModule]
})
export class YourModule() {}

2.0.3

  • [#190] - Fixed original size for showAll=false
  • [#57] - Disable autoresize

2.0.2

  • [#174] - 2.0.1 no more scrollbar while zooming

2.0.1

  • [#171] - AOT compile issue
  • [#172] - error

2.0.0

  • [#36] - Render text layer, hyperlinks, progressive loading, etc.
  • [#18] - Is it possible to enable hyperlinks in the rendered PDF?
  • [#165] - pdf.js and pdf.worker.js using different versions
  • [#50] - Default Rotation
  • [#164] - Doesn't load blob url with 1.2.7
  • [#160] - some pdf showing in invert color
  • [#156] - Chrome error: Invalid SVG dimensions when 'original-size' = false
  • [#154] - Multiple display issues on Safari (iOS+Desktop)
  • [#147] - GET http://localhost:4200/nullUniGB-UCS2-H 404 (Not Found)
  • [#145] - Issue since update from 1.1.1 to 1.2.1 and not fixed in 1.2.2
  • [#144] - pdf annotation not shown in svg format
  • [#142] - Error: svg:path attribute d: Expected moveto path command ('M' or 'm'), "L 2318.47 6033.1…".
  • [#141] - Mirror/Upside Down document display
  • [#139] - PDF from pdfmake not loading
  • [#134] - PDF not loading. Shows black color
  • [#108] - PDF is loading as plain text as well as PDF, overlapping
  • [#92] - Performance error
  • [#80] - One by one page

1.2.7

Updated dependencies

1.2.6

  • [#140] - Incorrect Font and Font Size Rendering
  • [#151] - force inline style to prevent css override font-family and font-size

1.2.5

  • Update dist

1.2.4

  • [#150] - Used not minified version of pdfjs

1.2.3

  • [#146] - Load web worker separately, use minified version of pdfjs

1.2.2

  • [#94] - Add load progress callback

1.2.1

  • [#130] - ng build --prod fails for AOT

1.2.0

  • [#98] - PDF Auto Resize

1.1.5

  • [#124] - PDF background disappeared after update to 1.1.4

1.1.4

  • Switched canvas to SVG rendering.
  • Changed text layer rendering.
  • [#89] - Added data-page-number attribute

1.1.3

  • [#46] - Retina display support

1.1.2

  • [#112] - Error handling
  • Updated documentation

1.1.1

  • [#111] - out of order

1.1.0

  • [#72] - Updated for Angular 4

1.0.2

  • [#54] - [show-all] is showing first two page only.

1.0.1

  • [#48] - Use ngOnchanges() instead of update() inside setters

1.0.0

Breaking changes

  • [#47] - Use @Output for after load events

Use (after-load-complete)="afterLoadComplete($event) instead of [after-load-complete]="afterLoadComplete($event) on <pdf-viewer>.

You do not need to bind your callback anymore.

Example of afterLoadComplete callback:

afterLoadComplete(pdf: PDFDocumentProxy) {
  this.pdf = pdf;
}

0.1.6

  • [#39] - background-color css causes PDF text to become invisible.
  • [#43] - PDF is drawn behind the container

0.1.5

  • [#37] - Fix the case where src is still not having a value

0.1.4

  • [#35] - New release causing blank pdf

0.1.3

  • [#29] - Make text selectable

0.1.2

  • [#28] - Fixed set initial page
  • [#30] - Do not reload PDF several times

0.1.1

  • [#27] - Added support for rotating

0.1.0

Breaking changes

  • [#25] - Renamed "on-load-complete" to "after-load-complete"

0.0.15

  • [#26] - Re-added pdfjs as dependency

0.0.14

Re-installing of package may be required.

  • [#20] - Unable to declare in App Module
  • Systemjs integration was changed, now you need to link dist/index.js file. See [SYSTEMJS.md]
  • Support for AoT

0.0.13

  • [#22] - Support zoom
  • other minor changes

0.0.12

  • [#7] - Return pdf info

0.0.11

  • [#10] - Need to Pass cookies with request for document src

0.0.10

  • [#11] - Update to Angular RC6
  • [#12] - Update how-to documentation to RC6
  • Removed version lock from peer dependency

0.0.9

  • [#6] - Limit [page]
  • Updated dependencies
  • Minor changes

0.0.8

  • [#5] - Fixed web worker issue
  • [#4] - Updated angular dependencies to RC.5

0.0.7

  • #2 - Added webpack compatibility
  • Changed default value for original size flag

0.0.6

  • Added option to show one or all pdf pages altogether

0.0.5

  • Updated dependency pdfjs-dist

0.0.4

  • Updated peer dependency

0.0.3

  • Added simple typings file