ng2-pdfjs-viewer 20.0.0 → 20.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +347 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,347 @@
1
+ # Angular PDFJS viewer with Mozilla's ViewerJS.
2
+
3
+ <p align="center">
4
+ <a href="https://www.npmjs.com/package/ng2-pdfjs-viewer">
5
+ <img src="https://img.shields.io/npm/dm/ng2-pdfjs-viewer.svg?style=flat" alt="downloads">
6
+ </a>
7
+ <a href="https://badge.fury.io/js/ng2-pdfjs-viewer">
8
+ <img src="https://badge.fury.io/js/ng2-pdfjs-viewer.svg" alt="npm version">
9
+ </a>
10
+ </p>
11
+
12
+ **_🎉 Thanks a ton to the community - We are talking Downloads in millions now!!!_**
13
+
14
+ **_This project is currently maintained by a single developer on his spare time. Inviting contributors and pull requests to pick-up momentum, and handle the expansive userbase and feature requests._**
15
+
16
+ This is a super simple angular component for displaying pdfs inline(embedded) OR in a new tab along with a feature rich viewer. It uses mozilla's pdfjs viewer(v2.2.171) behind the scenes and supports angular version from 2.0. Extremely lightweight, easiest to integrate and use, most reliable, this library has only one dependancy (@angular/core).
17
+
18
+ ## What is new?
19
+
20
+ 1. **Direct access to underlying viewer** - Exposed PDFViewerApplication and PDFViewerApplicationOptions objects opens a whole world of customizable PDFJS and ViewerJS properties and methods, allowing to change them programmatically; thus producing a unique viewer experience.
21
+ 2. **Dynamic page number** - Getter/Setter added for page number. Change page number after document loaded, or access current page in parent component.
22
+ 3. **Support for events** - You may now register several events such as document load, page change, before print, after print etc. Please make sure to provide `viewerId` for events to work properly.
23
+
24
+ ## Tutorials, Demos & Examples
25
+
26
+ **Hosted Demo app showing unique use cases**: https://ng2-pdfjs-viewer.azurewebsites.net
27
+ **Minimalistic angular sample app**: https://github.com/intbot/ng2-pdfjs-viewer/tree/master/SampleApp
28
+ Source code for the demo here : https://github.com/intbot/ng2-pdfjs-viewer/tree/master/Demo
29
+
30
+ ## Features
31
+
32
+ Some of below features are unique to this component, which is unavailable in native viewer or other implementations.
33
+ ✔️ **Open pdf in new window** 🌐 - Opens pdf viewer in new window. Also, you may set all allowable external window options.
34
+ ✔️ **Direct access to underlying viewer** 👨‍💻👩‍💻- Exposed PDFViewerApplication and PDFViewerApplicationOptions objects opens a whole world of customizable PDFJS and ViewerJS properties and methods, allowing to change them programmatically; thus producing a unique viewer experience.
35
+ ✔️ **Embed pdf** 🗎 - Embeds viewer and pdf inside your web page/component.
36
+ ✔️ **Blob and byte arrays** 🔟 - Have pdf as a byte array? Still works.
37
+ ✔️ **Events** ⚡ - Catch events such as document loaded, page change, before/after print etc.Please make sure to provide `viewerId` for events to work properly.
38
+ ✔️ **Print preview** 🖨️ - You can set the pdf to open in a new tab or another browser window and provide an immediate print preview, A use case will be a 'Print' button opening pdf in new window with print dialog.
39
+ ✔️ **Defaults** - There are a ton of built in functionality Mozilla's viewer supports; such as print, download, bookmark, fullscreen, open file, zoom, search, pan, spread, navigate, attachments etc which is also available as-is in this viewer. Not mentioning them individually.
40
+ ✔️ **Auto download** 💾 - This option allows you to download the pdf file to user device automatically without manually invoking the download.
41
+ ✔️ **Auto rotate** 🔄 - Rotate the document _clockwise_ or _anti-clockwise_ 90° (+/- 90°) just before displaying. Useful if the document is misaligned.
42
+ ✔️ **Language** 🌍 - Set the language of your choice to the viewer controls.
43
+ ✔️ **Custom error** ❌ - Hate the defaults pdfjs errors, use this feature to override or append to it.
44
+ ✔️ **Side bar** 📎 - Open and show the sidebar with relevant information. E.g. Show attachments.
45
+ ✔️ **Smart device friendly zoom** 📱 - A better zooming experience on small devices.
46
+ ✔️ **Progress spinner** ⏳ - An optional spinner which displays before document renders.
47
+ ✔️ **Show/Hide controls** 👻 - Do not want users to see specific controls (E.g. download button). Use relevant features to remove controls from viewer.
48
+ ✔️ **Navigate** ⏩ - Navigate the document using page number, section, or even directly jump to last page if you are expecting a signature from user.
49
+ ✔️ **Zoom** 🔍 - Several zoom methods, auto, percentage (E.g. 150%), page-with/height, fit page etc. with ability to set offset.
50
+ ✔️ **Cursor** 👆 - Love the age old hand cursor 👆 on your pdf? You got it.
51
+ ✔️ **Change file name** - You may set the file name of download to a different one, if user chooses to download.
52
+ ✔️ **Scroll** 📜 - Changes the scroll to your choice. Even wrapped scrolling is supported.
53
+ ✔️ **Spread** - Allows you to change spread to odd or even.
54
+
55
+ ### Open in a new tab/ external window
56
+
57
+ ![angular pdfjs viewer in new window](./SampleImages/ng2pdfjsviewerExternal.JPG)
58
+
59
+ ### Embed pdf into any angular component/page
60
+
61
+ ![angular pdfjs viewer embedded](./SampleImages/ng2pdfjsviewerEmbedded.JPG)
62
+
63
+ ## Installation
64
+
65
+ ## Step 1: Install `ng2-pdfjs-viewer`
66
+
67
+ ```bash
68
+ $ npm install ng2-pdfjs-viewer --save
69
+ ```
70
+
71
+ And then configure it in your Angular `AppModule`:
72
+
73
+ ```typescript
74
+ import { BrowserModule } from "@angular/platform-browser";
75
+ import { NgModule } from "@angular/core";
76
+ import { AppComponent } from "./app.component";
77
+
78
+ import { PdfJsViewerModule } from "ng2-pdfjs-viewer"; // <-- Import PdfJsViewerModule module
79
+
80
+ @NgModule({
81
+ declarations: [AppComponent],
82
+ imports: [
83
+ BrowserModule,
84
+ PdfJsViewerModule, // <-- Add to declarations
85
+ ],
86
+ providers: [],
87
+ bootstrap: [AppComponent],
88
+ })
89
+ export class AppModule {}
90
+ ```
91
+
92
+ ## Step 2: Copy task for pdfjs
93
+
94
+ For several advanced options to work, you need a copy of pdfjs from this npm package.
95
+ Edit your project's `angular.json` file and use `ng build` as described here https://angular.io/guide/workspace-config#assets-configuration
96
+
97
+ ```json
98
+ "assets": [
99
+ { "glob": "**/*", "input": "node_modules/ng2-pdfjs-viewer/pdfjs", "output": "/assets/pdfjs" },
100
+ ]
101
+ ```
102
+
103
+ _Please note, you must manually Copy `node_modules\ng2-pdfjs-viewer\pdfjs` to your `public` or `asset` folder or use any copy script as part of your build process. Another method could be to use `TransferWebpackPlugin` if you are using webpack(https://github.com/dkokorev90/transfer-webpack-plugin)._
104
+ `TransferWebpackPlugin` Sample code
105
+
106
+ ```typescript
107
+ var TransferWebpackPlugin = require('transfer-webpack-plugin');
108
+ ...
109
+ plugins: [
110
+ new TransferWebpackPlugin([
111
+ { from: 'node_modules\ng2-pdfjs-viewer\pdfjs', to: path.join(__dirname, 'assets') }
112
+ ])
113
+ ]
114
+ ```
115
+
116
+ _Please note if you decide to put `pdfjs` folder anywhere else other than the `assets` folder, make sure you also set `[viewerFolder]` property to help locate the folder._
117
+
118
+ ### Options
119
+
120
+ | Attribute | Description | Type | Default Value |
121
+ | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------- | -------------------------------------------------------- |
122
+ | `[pdfSrc]` | Fully qualified path to pdf file. (For remote pdf urls over http/https, CORS should be enabled) | `string` | |
123
+ | `PDFViewerApplication` | This public property exposes underlying PDFViewerApplication object. Make sure to access it after document is loaded. Opens up the whole world of underlying PDFJS properties and methods. Use it to customize the viewer and document experience. | `object` | |
124
+ | `PDFViewerApplicationOptions` | This public property exposes underlying PDFViewerApplicationOptions object. Make sure to access it after document is loaded. Opens up the whole world of underlying PDFJS options. Use it to customize the viewer and document experience. | `object` | |
125
+ | `[viewerFolder]` | Set path to _pdfjs's_ `web` and `build` folders. | `string` | `assets` folder path |
126
+ | `[externalWindow]` | Open in new tab. Set to `true` to open document in a new tab | `boolean` | `false` |
127
+ | `externalWindowOptions` | External window options. For allowed comma separated values, refer to https://developer.mozilla.org/en-US/docs/Web/API/Window/open | `string` | |
128
+ | `(onDocumentLoad)` | Event to be invoked once document is fully loaded(Must provide `viewerId`). Also returns number of pages in the `$event` parameter. E.g. `(onDocumentLoad)="testPagesLoaded($event)""` | `Function` | |
129
+ | `(onPageChange)` | Event to be invoked when user scrolls through pages(Must provide `viewerId`). Also returns current page number user is at in the `$event` parameter. E.g. `(onPageChange)="testPageChange($event)""` | `Function` | |
130
+ | `(onBeforePrint)` | Event to be invoked before document gets printed(Must provide `viewerId`). E.g. `(onBeforePrint)="testBeforePrint()"` | `Function` | |
131
+ | `(onAfterPrint)` | Event to be invoked after document gets printed(Must provide `viewerId`). E.g. `(onAfterPrint)="testAfterPrint()"` | `Function` | |
132
+ | `downloadFileName` | Sets/Changes the name of document to be downloaded. If the file name does not ends in `.pdf`, the component will automatically add it for you. | `string` | Actual name of the document |
133
+ | `[page]` | Show specific page. E.g _page=3_. You may also get/set the page number from your component using '.' notation explicitly, after document is loaded. E.g. `myPdfViewer.page = 3;` | `number` | `1` |
134
+ | `[lastPage]` | Show last page of the document once it is loaded(If set to `true`). If you use this option along with _`page`_ option, undesired effects might occur | `boolean` | `false` |
135
+ | `nameddest` | Go to a named destination. E.g. To go to section _5.1_ use like nameddest=5.1. Do not mix this option with _`page`_ and _`lastPage`_ options | `string` | |
136
+ | `zoom` | Set zoom level of document. Applicable values are `auto`, `page-width`, `page-height`, `page-fit`, `200`_(As a zoom percentage)_, `left offset`_(As in "auto,18,827")_, `top offset`_(As in "auto,18,127")_ | `string` | `auto` |
137
+ | `[print]` | Show/hide print button. Set `false` to hide | `boolean` | `true` |
138
+ | `[startPrint]` | Start print preview of document. This combined with _`externalWindow`_ could mimic a print preview functionality just like the one in gmail. | `boolean` | |
139
+ | `[download]` | Show/hide download button. Set `false` to hide | boolean | `true` |
140
+ | `[find]` | Show/hide search button. Set `false` to hide | boolean | `true` |
141
+ | `[startDownload]` | Download document as soon as it opens. You may put this to good use. | `boolean` | |
142
+ | `[rotatecw]` | Rotate document clock wise 90° | `boolean` | |
143
+ | `[rotateccw]` | Rotate document anti-clock wise 90° | `boolean` | |
144
+ | `cursor` | Type of cursor. Available options are _`HAND`/`H`_, _`SELECT`/`S`_,_`ZOOM`/`Z`_. Case is irrelevant. | _`SELECT`/`S`_ | |
145
+ | `scroll` | Sets scroll. Available options are _`VERTICAL`/`V`_, _`HORIZONTAL`/`H`_,_`WRAPPED`/`W`_. Case is irrelevant. | _`VERTICAL`/`V`_ | |
146
+ | `spread` | Sets Odd or Even spread. Available options are _`ODD`/`O`_, _`EVEN`/`E`_,_`NONE`/`N`_. Case is irrelevant. | _`NONE`/`N`_ | |
147
+ | `[fullScreen]` | Show/hide presentation(full screen) button. Set `false` to hide | `boolean` | `true` |
148
+ | `cursor` | Type of cursor. Available options are _`HAND`/`H`_, _`SELECT`/`S`_,_`ZOOM`/`Z`_. Case is irrelevant. | _`SELECT`/`S`_ | |
149
+ | `pagemode` | State of sidebar. Available options are _`none`_, _`thumbs`_,_`bookmarks`_,_`attachments`_. E.g. `pagemode=attachments`. | _`none`_ | |
150
+ | `[openFile]` | Show/hide open file button. Set `false` to hide | boolean | `true` |
151
+ | `[viewBookmark]` | Show/hide bookmark button. Set `false` to hide | boolean | `true` |
152
+ | `[showSpinner]` | Show a simple css based spinner/progress before the document loads | boolean | `true` |
153
+ | `locale` | Set locale(language) of toolbar/buttons and other viewer elements. E.g. 'de-AT', 'en-GB' etc | string | browser's default language if present, otherwise `en-US` |
154
+ | `[useOnlyCssZoom]` | Instructs the viewer to use css based zoom. This will produce better zoom effect on mobile devices and tablets. | boolean | `false` |
155
+ | `errorMessage` | Custom error message | string | |
156
+ | `[errorAppend]` | Appends custom error message to the end of other pdfjs error messages | `true` |
157
+ | `[errorOverride]` | Overrides all pdfjs error messages and shows only user's custom error message | boolean | `false` |
158
+ | `[diagnosticLogs]` | Turns on all diagnostic logs to the console | boolean | `true` |
159
+
160
+ **_Please note, copy step is mandatory to enjoy all of the different options listed above. You may also avoid this step and could directly use https://github.com/mozilla/pdf.js/wiki/Setup-pdf.js-in-a-website if you wish to just use the default viewer_**
161
+
162
+ ## Usage
163
+
164
+ _For your convenience a sample app using angular is available under this repository, if you would like to see it in action (Folder SampleApp). It shows many ways to configure this component for different needs._
165
+
166
+ Once your PdfJsViewerComponent is imported you can use it in your Angular application like this:
167
+
168
+ ```xml
169
+ <!-- You can now use your library component in app.component.html -->
170
+ <h1>
171
+ {{title}}
172
+ </h1>
173
+ <ng2-pdfjs-viewer pdfSrc="your pdf file path"></ng2-pdfjs-viewer>
174
+ ```
175
+
176
+ Here is a use case to download and open the pdf as byte array and open in new tab/window:
177
+ Please note, pdfSrc can be a Blob or Uint8Array as well
178
+ For [externalWindow]="true" to work, pop-ups needs to be enabled at browser level
179
+
180
+ ```xml
181
+ <!-- your.component.html -->
182
+ <button (click)="openPdf();">Open Pdf</button>
183
+
184
+ <div style="width: 800px; height: 400px">
185
+ <ng2-pdfjs-viewer
186
+ #pdfViewerOnDemand
187
+ [externalWindow]="true"
188
+ [downloadFileName]="'mytestfile.pdf'"
189
+ [openFile]="false"
190
+ [viewBookmark]="false"
191
+ [download]="false"></ng2-pdfjs-viewer>
192
+ </div>
193
+ <div>
194
+ <div style="width: 800px; height: 400px">
195
+ <ng2-pdfjs-viewer #pdfViewerAutoLoad></ng2-pdfjs-viewer>
196
+ </div>
197
+ <div style="height: 600px">
198
+ <ng2-pdfjs-viewer pdfSrc="gre_research_validity_data.pdf" viewerId="MyUniqueID" (onBeforePrint)="testBeforePrint()" (onAfterPrint)="testAfterPrint()" (onDocumentLoad)="testPagesLoaded($event)" (onPageChange)="testPageChange($event)">
199
+ </ng2-pdfjs-viewer>
200
+ </div>
201
+ ```
202
+
203
+ ```typescript
204
+ <!-- your.component.ts-->
205
+ import { Component, ViewChild } from '@angular/core';
206
+ import { HttpClient } from '@angular/common/http';
207
+ import { Observable } from 'rxjs';
208
+ import { map } from 'rxjs/operators';
209
+ ...
210
+
211
+ export class MyComponent implements OnInit {
212
+ @ViewChild('pdfViewerOnDemand') pdfViewerOnDemand;
213
+ @ViewChild('pdfViewerAutoLoad') pdfViewerAutoLoad;
214
+ ...
215
+
216
+ constructor(private http: HttpClient) {
217
+ let url = "api/document/getmypdf"; // Or your url
218
+ this.downloadFile(url).subscribe(
219
+ (res) => {
220
+ this.pdfViewerAutoLoad.pdfSrc = res; // pdfSrc can be Blob or Uint8Array
221
+ this.pdfViewerAutoLoad.refresh(); // Ask pdf viewer to load/refresh pdf
222
+ }
223
+ );
224
+ }
225
+
226
+ private downloadFile(url: string): any {
227
+ return this.http.get(url, { responseType: 'blob' })
228
+ .pipe(
229
+ map((result: any) => {
230
+ return result;
231
+ })
232
+ );
233
+ }
234
+
235
+ public openPdf() {
236
+ let url = "url to fetch pdf as byte array"; // E.g. http://localhost:3000/api/GetMyPdf
237
+ // url can be local url or remote http request to an api/pdf file.
238
+ // E.g: let url = "assets/pdf-sample.pdf";
239
+ // E.g: https://github.com/intbot/ng2-pdfjs-viewer/tree/master/sampledoc/pdf-sample.pdf
240
+ // E.g: http://localhost:3000/api/GetMyPdf
241
+ // Please note, for remote urls to work, CORS should be enabled at the server. Read: https://enable-cors.org/server.html
242
+
243
+ this.downloadFile(url).subscribe(
244
+ (res) => {
245
+ this.pdfViewerOnDemand.pdfSrc = res; // pdfSrc can be Blob or Uint8Array
246
+ this.pdfViewerOnDemand.refresh(); // Ask pdf viewer to load/reresh pdf
247
+ }
248
+ );
249
+ }
250
+ ```
251
+
252
+ # Additional Information
253
+
254
+ Given below are examples of writing server apis(In aspnetcore c#) which returns pdfs as byte array. You can choose any server side technology as long as pdf is returned as byte array
255
+
256
+ Use case 1. As a RDLC local report viewer
257
+
258
+ ```c#
259
+ [HttpGet]
260
+ [Route("MyReport")]
261
+ public IActionResult GetReport()
262
+ {
263
+ // var reportObjectList1
264
+ // var reportObjectList2
265
+ var reportViewer = new ReportViewer {ProcessingMode = ProcessingMode.Local};
266
+ reportViewer.LocalReport.ReportPath = "Reports/MyReport.rdlc";
267
+
268
+ reportViewer.LocalReport.DataSources.Add(new ReportDataSource("NameOfDataSource1", reportObjectList1));
269
+ reportViewer.LocalReport.DataSources.Add(new ReportDataSource("NameOfDataSource2", reportObjectList1));
270
+
271
+ Warning[] warnings;
272
+ string[] streamids;
273
+ string mimeType;
274
+ string encoding;
275
+ string extension;
276
+
277
+ var bytes = reportViewer.LocalReport.Render("application/pdf", null, out mimeType, out encoding, out extension, out streamids, out warnings);
278
+ return File(bytes, "application/pdf")
279
+ }
280
+ ```
281
+
282
+ Use case 2. Return a physical pdf from server
283
+
284
+ ```c#
285
+ [HttpGet]
286
+ [Route("GetMyPdf")]
287
+ public IActionResult GetMyPdf()
288
+ {
289
+ var stream = await {{__get_stream_here__}}
290
+ return File(stream, "application/pdf")); // FileStreamResult
291
+
292
+ // OR
293
+ // var bytes = await {{__get_bytes_here__}}
294
+ // return File(bytes, "application/pdf")
295
+ }
296
+
297
+ OR
298
+
299
+ [HttpGet]
300
+ [Route("GetMyPdf")]
301
+ public IActionResult GetMyPdf()
302
+ {
303
+ var pdfPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/assets/pdfjs/web/gre_research_validity_data.pdf");
304
+ byte[] bytes = System.IO.File.ReadAllBytes(pdfPath);
305
+ return File(bytes, "application/pdf");
306
+ }
307
+ ```
308
+
309
+ ## E-Signature Support
310
+
311
+ Original issue -> PDF.JS is not able to load the signature in the pdf if that is done via the any **E-Signature** tool.
312
+
313
+ Resolution of the issue -> To support this feature need to comment/remove the below code from pdf.worker.js file and file path i.e. lib\pdfjs\build\pdf.worker.js
314
+
315
+ **Original issue code**
316
+
317
+ ```javascript
318
+ if (data.fieldType === "Sig") {
319
+ data.fieldValue = null;
320
+
321
+ _this3.setFlags(_util.AnnotationFlag.HIDDEN);
322
+ }
323
+ ```
324
+
325
+ **Resoultion code**
326
+
327
+ ```javascript
328
+ if (data.fieldType === "Sig") {
329
+ data.fieldValue = null;
330
+
331
+ _this3.setFlags(_util.AnnotationFlag.HIDDEN); //remove or comment this line
332
+ }
333
+ ```
334
+
335
+ ## Stack Overflow Support
336
+
337
+ Have a question? Ask questions and find answers on Stack overflow.
338
+
339
+ [Stack Overflow](https://stackoverflow.com/questions/tagged/ng2-pdfjs-viewer)
340
+
341
+ ## Looking for old AngularJS? - The below library is quite useful
342
+
343
+ AngularJS [angular-pdfjs-viewer](https://github.com/legalthings/angular-pdfjs-viewer)
344
+
345
+ ## License
346
+
347
+ \*\*Apache V2 License - https://www.apache.org/licenses/LICENSE-2.0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ng2-pdfjs-viewer",
3
- "version": "20.0.0",
3
+ "version": "20.0.1",
4
4
  "$schema": "./node_modules/ng-packagr/package.schema.json",
5
5
  "peerDependencies": {
6
6
  "@angular/common": "^20.0.0",