diff --git a/README.md b/README.md index 4c5223ba..bfca27c9 100644 --- a/README.md +++ b/README.md @@ -30,37 +30,41 @@ ## Overview -* [Install](#install) -* [Usage](#usage) -* [Options](#options) -* [Render local PDF file](#render-local-pdf-file) -* [Set custom path to the worker](#set-custom-path-to-the-worker) -* [Search in the PDF](#search-in-the-pdf) -* [Contribute](#contribute) +- [Install](#install) +- [Usage](#usage) +- [Options](#options) +- [Render local PDF file](#render-local-pdf-file) +- [Set custom path to the worker](#set-custom-path-to-the-worker) +- [Search in the PDF](#search-in-the-pdf) +- [Contribute](#contribute) ## Install ### Angular >= 12 + ``` npm install ng2-pdf-viewer ``` + > Partial Ivy compilated library bundles. ### Angular >= 4 + ``` npm install ng2-pdf-viewer@^7.0.0 ``` ### Angular < 4 + ``` npm install ng2-pdf-viewer@~3.0.8 ``` ## Usage -*In case you're using ```systemjs``` see configuration [here](https://github.com/VadimDez/ng2-pdf-viewer/blob/master/SYSTEMJS.md).* +_In case you're using `systemjs` see configuration [here](https://github.com/VadimDez/ng2-pdf-viewer/blob/master/SYSTEMJS.md)._ -Add ```PdfViewerModule``` to your module's ```imports``` +Add `PdfViewerModule` to your module's `imports` ```typescript import { NgModule } from '@angular/core'; @@ -72,9 +76,8 @@ import { PdfViewerModule } from 'ng2-pdf-viewer'; @NgModule({ imports: [BrowserModule, PdfViewerModule], declarations: [AppComponent], - bootstrap: [AppComponent] + bootstrap: [AppComponent], }) - class AppModule {} platformBrowserDynamic().bootstrapModule(AppModule); @@ -87,47 +90,47 @@ import { Component } from '@angular/core'; @Component({ selector: 'example-app', - template: ` - - ` + template: ` `, }) export class AppComponent { - pdfSrc = "https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf"; + pdfSrc = 'https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf'; } ``` ## Options -* [[src]](#src) -* [[(page)]](#page) -* [[stick-to-page]](#stick-to-page) -* [[external-link-target]](#external-link-target) -* [[render-text]](#render-text) -* [[render-text-mode]](#render-text-mode) -* [[rotation]](#rotation) -* [[zoom]](#zoom) -* [[zoom-scale]](#zoom-scale) -* [[original-size]](#original-size) -* [[fit-to-page]](#fit-to-page) -* [[show-all]](#show-all) -* [[autoresize]](#autoresize) -* [[c-maps-url]](#c-maps-url) -* [[show-borders]](#show-borders) -* [(after-load-complete)](#after-load-complete) -* [(page-rendered)](#page-rendered) -* [(text-layer-rendered)](#text-layer-rendered) -* [(error)](#error) -* [(on-progress)](#on-progress) +- [[src]](#src) +- [[(page)]](#page) +- [[stick-to-page]](#stick-to-page) +- [[external-link-target]](#external-link-target) +- [[render-text]](#render-text) +- [[render-text-mode]](#render-text-mode) +- [[rotation]](#rotation) +- [[(zoom)]](#zoom) +- [[zoom-scale]](#zoom-scale) +- [[minZoom]](#minzoom) +- [[maxZoom]](#maxzoom) +- [[isZoomOptimization]](#iszoomoptimization) +- [[isWheelZoom]](#iswheelzoom) +- [[isWheelCtrlZoom]](#iswheelctrlzoom) +- [[enablePan]](#enablepan) +- [[original-size]](#original-size) +- [[fit-to-page]](#fit-to-page) +- [[show-all]](#show-all) +- [[autoresize]](#autoresize) +- [[c-maps-url]](#c-maps-url) +- [[show-borders]](#show-borders) +- [(after-load-complete)](#after-load-complete) +- [(page-rendered)](#page-rendered) +- [(text-layer-rendered)](#text-layer-rendered) +- [(error)](#error) +- [(on-progress)](#on-progress) #### [src] -| Property | Type | Required | -| --- | ---- | --- | -| [src] | *string, object, UInt8Array* | Required | +| Property | Type | Required | +| -------- | ---------------------------- | -------- | +| [src] | _string, object, UInt8Array_ | Required | Pass pdf location @@ -135,46 +138,48 @@ 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](https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L130-L222). +For more control you can pass options object to `[src]`. [See other attributes for the object here](https://github.com/mozilla/pdf.js/blob/master/src/display/api.js#L130-L222). Options object for loading protected PDF would be: - ```js - { - url: 'https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf', - withCredentials: true - } - ``` +```js +{ + url: 'https://vadimdez.github.io/ng2-pdf-viewer/assets/pdf-test.pdf', + withCredentials: true +} +``` #### [page] - -| Property | Type | Required | -| --- | ---- | --- | -| [page] or [(page)] | *number* | *Required* with [show-all]="false" or *Optional* with [show-all]="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: + ```css pdf-viewer { - height: 100vh; + height: 100vh; } ``` #### [stick-to-page] -| Property | Type | Required | -| --- | ---- | --- | -| [stick-to-page] | *boolean* | *Optional* | +| Property | Type | Required | +| --------------- | --------- | ---------- | +| [stick-to-page] | _boolean_ | _Optional_ | Sticks view to the page. Works in combination with `[show-all]="true"` and `page`. @@ -184,20 +189,21 @@ Sticks view to the page. Works in combination with `[show-all]="true"` and `page #### [render-text] -| Property | Type | Required | -| --- | ---- | --- | -| [render-text] | *boolean* | *Optional* | +| 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* | +| Property | Type | Required | +| ------------------ | ---------------- | ---------- | +| [render-text-mode] | _RenderTextMode_ | _Optional_ | Used in combination with `[render-text]="true"` @@ -215,59 +221,63 @@ Controls if the text layer is enabled, and the selection mode that is used. #### [external-link-target] -| Property | Type | Required | -| --- | ---- | --- | -| [external-link-target] | *string* | *Optional* | +| Property | Type | Required | +| ---------------------- | -------- | ---------- | +| [external-link-target] | _string_ | _Optional_ | Used in combination with `[render-text]="true"` Link target -* `blank` -* `none` -* `self` -* `parent` -* `top` + +- `blank` +- `none` +- `self` +- `parent` +- `top` + ``` [external-link-target]="'blank'" ``` #### [rotation] -| Property | Type | Required | -| --- | ---- | --- | -| [rotation] | *number* | *Optional* | +| Property | Type | Required | +| ---------- | -------- | ---------- | +| [rotation] | _number_ | _Optional_ | Rotate PDF -*Allowed step is 90 degree, ex. 0, 90, 180* +_Allowed step is 90 degree, ex. 0, 90, 180_ + ``` [rotation]="90" ``` #### [zoom] -| Property | Type | Required | -| --- | ---- | --- | -| [zoom] | *number* | *Optional* | +| Property | Type | Required | +| -------- | -------- | ---------- | +| [zoom] | _number_ | _Optional_ | + +Zoom pdf, it supports two-way bindings. -Zoom pdf ``` -[zoom]="0.5" +[(zoom)]="0.5" ``` #### [zoom-scale] -| Property | Type | Required | -| --- | ---- | --- | -| [zoom-scale] | *'page-width'\|'page-fit'\|'page-height'* | *Optional* | +| 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'. +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-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-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 +- _'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'" @@ -275,12 +285,12 @@ Defines how the Zoom scale is computed when `[original-size]="false"`, by defau #### [original-size] -| Property | Type | Required | -| --- | ---- | --- | -| [original-size] | *boolean* | *Optional* | +| 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 +- 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" @@ -288,9 +298,9 @@ Defines how the Zoom scale is computed when `[original-size]="false"`, by defau #### [fit-to-page] -| Property | Type | Required | -| --- | ---- | --- | -| [fit-to-page] | *boolean* | *Optional* | +| 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. @@ -300,9 +310,9 @@ Works in combination with `[original-size]="true"`. You can show your document i #### [show-all] -| Property | Type | Required | -| --- | ---- | --- | -| [show-all] | *boolean* | *Optional* | +| Property | Type | Required | +| ---------- | --------- | ---------- | +| [show-all] | _boolean_ | _Optional_ | Show single or all pages altogether @@ -312,9 +322,9 @@ Show single or all pages altogether #### [autoresize] -| Property | Type | Required | -| --- | ---- | --- | -| [autoresize] | *boolean* | *Optional* | +| Property | Type | Required | +| ------------ | --------- | ---------- | +| [autoresize] | _boolean_ | _Optional_ | Turn on or off auto resize. @@ -324,41 +334,151 @@ Turn on or off auto resize. [autoresize]="true" ``` +--- + +#### [minZoom] + +| Property | Type | Required | +| --------- | -------- | -------- | +| `minZoom` | _number_ | Optional | + +**Default**: `0`, that means no min value. + +Define a min value for the zoom. + +**Example:** + +```html +[minZoom]="0.1" +``` + +--- + +#### [maxZoom] + +| Property | Type | Required | +| --------- | -------- | -------- | +| `maxZoom` | _number_ | Optional | + +**Default**: `0`, that means no max value. + +Define a max value for the zoom. + +**Example:** + +```html +[maxZoom]="10" +``` + +--- + +#### [isZoomOptimization] + +| Property | Type | Required | +| -------------------- | --------- | -------- | +| `isZoomOptimization` | _boolean_ | Optional | + +**Default**: `true` + +Improves the zooming experience by maintaining the user's viewport position. It calculates and adjusts the scroll position before and after the zoom to prevent unexpected jumps. + +> Note: This option is always enabled if `[isWheelZoom]` is set to `true`. + +**Example:** + +```html +[isZoomOptimization]="true" +``` + +--- + +#### [isWheelZoom] + +| Property | Type | Required | +| ------------- | --------- | -------- | +| `isWheelZoom` | _boolean_ | Optional | + +**Default**: `true` + +Enables zooming with the mouse wheel. You can control whether zooming requires the `Ctrl` key using `[isWheelCtrlZoom]`. + +```html +[isWheelZoom]="true" +``` + +--- + +#### [isWheelCtrlZoom] + +| Property | Type | Required | +| ----------------- | --------- | -------- | +| `isWheelCtrlZoom` | _boolean_ | Optional | + +**Default**: `true` + +Allows zooming with the mouse wheel only when the `Ctrl` key is pressed. +If set to `false`, the mouse wheel will trigger zooming by default (without requiring `Ctrl`), replacing the typical scroll behavior. + +```html +[isWheelCtrlZoom]="true" +``` + +--- + +#### [enablePan] + +| Property | Type | Required | +| ----------- | --------- | -------- | +| `enablePan` | _boolean_ | Optional | + +**Default**: `false` + +When set to true, enables a "hand tool" mode allowing users to click and drag the PDF document to navigate around. The cursor changes to a grabbing hand while panning is active. + +```html +[enablePan]="true" +``` + +--- + #### [c-maps-url] -| Property | Type | Required | -| --- | ---- | --- | -| [c-maps-url] | *string* | Optional | +| 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/](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```. +To serve cmaps on your own you need to copy `node_modules/pdfjs-dist/cmaps` to `assets/cmaps`. ### [show-borders] -| Property | Type | Required | -| --- | ---- | --- | -| [show-borders] | *boolean* | Optional | +| Property | Type | Required | +| -------------- | --------- | -------- | +| [show-borders] | _boolean_ | Optional | Show page borders + ``` [show-borders]="true" ``` #### (after-load-complete) -| Property | Type | Required | -| --- | ---- | --- | -| (after-load-complete) | *callback* | *Optional* | +| Property | Type | Required | +| --------------------- | ---------- | ---------- | +| (after-load-complete) | _callback_ | _Optional_ | Get PDF information with callback First define callback function "callBackFn" in your controller, + ```typescript callBackFn(pdf: PDFDocumentProxy) { // do anything with "pdf" @@ -366,15 +486,16 @@ callBackFn(pdf: PDFDocumentProxy) { ``` And then use it in your template: + ``` (after-load-complete)="callBackFn($event)" ``` #### (page-rendered) -| Property | Type | Required | -| --- | ---- | --- | -| (page-rendered) | *callback* | *Optional* | +| Property | Type | Required | +| --------------- | ---------- | ---------- | +| (page-rendered) | _callback_ | _Optional_ | Get event when a page is rendered. Called for every page rendered. @@ -394,9 +515,9 @@ And then bind it to ``: #### (pages-initialized) -| Property | Type | Required | -| --- | ---- | --- | -| (pages-initialized) | *callback* | *Optional* | +| Property | Type | Required | +| ------------------- | ---------- | ---------- | +| (pages-initialized) | _callback_ | _Optional_ | Get event when the pages are initialized. @@ -416,9 +537,9 @@ And then bind it to ``: #### (text-layer-rendered) -| Property | Type | Required | -| --- | ---- | --- | -| (text-layer-rendered) | *callback* | *Optional* | +| Property | Type | Required | +| --------------------- | ---------- | ---------- | +| (text-layer-rendered) | _callback_ | _Optional_ | Get event when a text layer is rendered. @@ -438,9 +559,9 @@ And then bind it to ``: #### (error) -| Property | Type | Required | -| --- | ---- | --- | -| (error) | *callback* | *Optional* | +| Property | Type | Required | +| -------- | ---------- | ---------- | +| (error) | _callback_ | _Optional_ | Error handling callback @@ -460,9 +581,9 @@ Then add it to `pdf-component` in component's template #### (on-progress) -| Property | Type | Required | -| --- | ---- | --- | -| (on-progress) | *callback* | *Optional* | +| 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. @@ -485,7 +606,7 @@ Then add it to `pdf-component` in component's template In your `html` template add `input`: ```html - + ``` and then add `onFileSelected` method to your component: @@ -506,25 +627,26 @@ onFileSelected() { } ``` - ## Set custom path to the worker 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` + ```typescript (window as any).pdfWorkerSrc = '/pdf.worker.mjs'; ``` -*This should be set before `pdf-viewer` component is rendered.* +_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 +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. +custom path _just for that version_. This way setting the global window var won't conflict. + ```typescript -(window as any)["pdfWorkerSrc2.14.305"] = '/pdf.worker.mjs'; +(window as any)['pdfWorkerSrc2.14.305'] = '/pdf.worker.mjs'; ``` ## Search in the PDF @@ -533,8 +655,8 @@ Use `eventBus` for the search functionality. In your component's ts file: -* Add reference to `pdf-viewer` component, -* then when needed execute `search()` like this: +- Add reference to `pdf-viewer` component, +- then when needed execute `search()` like this: ```typescript @ViewChild(PdfViewerComponent) private pdfComponent: PdfViewerComponent; @@ -547,9 +669,11 @@ search(stringToSearch: string) { ``` ## Contribute + [See CONTRIBUTING.md](CONTRIBUTING.md) ## Donation + If this project help you reduce time to develop, you can give me a cup of tea :) [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.me/vadimdez) @@ -557,4 +681,3 @@ If this project help you reduce time to develop, you can give me a cup of tea :) ## License [MIT](https://tldrlegal.com/license/mit-license) © [Vadym Yatsyuk](https://github.com/vadimdez) - diff --git a/src/app/app.component.html b/src/app/app.component.html index 13b75386..36a659a7 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,19 +1,37 @@ - - ng2-pdf-viewer + ng2-pdf-viewer - Angular 5+ Component to render PDF + Angular 5+ Component to render PDF - - - Get Started + + + Get Started @@ -21,14 +39,17 @@ Set URL of the PDF file - +
-

- Or render preview by just selecting PDF file (no upload required) -

- +

Or render preview by just selecting PDF file (no upload required)

+
@@ -42,9 +63,7 @@
- - Fit to page - + Fit to page
@@ -65,6 +84,10 @@
+
+ Enable Pan +
+ Select an option @@ -76,8 +99,17 @@
- + Input is not a number!
+
+ Min zoom: + + + + Input is not a number! + +
+ +
+ Max zoom: + + + + Input is not a number! + +
@@ -107,39 +185,80 @@
-
+
- + Input is not a number! of {{ pdf.numPages }} - +
-