1
1
# PDF.js viewer Angular directive
2
2
3
- Embed [ PDF.js] ( https://mozilla.github.io/pdf.js/ ) viewer into your angular application, maintaining that look and feel
3
+ Embed Mozilla's [ PDF.js] ( https://mozilla.github.io/pdf.js/ ) viewer into your angular application, maintaining that look and feel
4
4
of pdf's we all love. The directive embeds the [ full viewer] ( https://mozilla.github.io/pdf.js/web/viewer.html ) , which
5
5
allows you to scroll through the pdf.
6
6
7
- ## Installation
8
7
8
+ ![ viewer-example] ( https://cloud.githubusercontent.com/assets/5793511/24605022/6dd5abee-1867-11e7-881a-0d68dc7c77f3.png )
9
+
10
+
11
+ ## Installation
9
12
bower install angular-pdfjs-viewer --save
10
13
11
14
## Usage
15
+ Below you will find a basic example of how the directive can be used.
16
+ Note that the order of the scripts matters. Stick to the order of dependencies as shown in the example below.
17
+ Also note that images, translations and such are being loaded from the ` web ` folder.
12
18
13
- Note that the order of the scripts matters. Stick to the order of dependencies as shown in the example below. Also note
14
- that images, translations and such are being loaded from the ` web ` folder.
15
-
16
- ** View**
19
+ ### View
17
20
``` html
18
21
<!DOCTYPE html>
19
- <html lang = " en " data- ng-app =" app" ng-controller =" AppCtrl" >
22
+ <html ng-app =" app" ng-controller =" AppCtrl" >
20
23
<head >
21
- <meta charset =" utf-8" />
22
24
<title >Angular PDF.js demo</title >
23
- <meta name =" viewport" content =" width=device-width, initial-scale=1" >
24
-
25
25
<script src =" bower_components/pdf.js-viewer/pdf.js" ></script >
26
26
<link rel =" stylesheet" href =" bower_components/pdf.js-viewer/viewer.css" >
27
27
@@ -30,72 +30,120 @@ that images, translations and such are being loaded from the `web` folder.
30
30
<script src =" app.js" ></script >
31
31
32
32
<style >
33
- html , body {
34
- height : 100% ;
35
- width : 100% ;
36
- margin : 0 ;
37
- padding : 0 ;
38
- }
39
-
40
- .some-pdf-container {
41
- width : 100% ;
42
- height : 100% ;
43
- }
33
+ html , body { height : 100% ; width : 100% ; margin : 0 ; padding : 0 ; }
34
+ .some-pdf-container { width : 100% ; height : 100% ; }
44
35
</style >
45
36
</head >
46
37
<body >
47
- <div class =' some-pdf-container' >
48
- <pdfjs-viewer src =" {{ pdf.src }}" scale =" scale"
49
- download =" true" print =" false" open =" false"
50
- on-init =" onInit()" on-page-load =" onPageLoad(page)" >
51
- </pdfjs-viewer >
38
+ <div class =" some-pdf-container" >
39
+ <pdfjs-viewer src =" {{ pdf.src }}" ></pdfjs-viewer >
52
40
</div >
53
41
</body >
54
42
</html >
55
43
```
56
44
57
- The ` scale ` attribute can be used to obtain the current scale (zoom level) of the PDF. This is read only.
58
-
59
- The directive takes the following optional attributes to modify the toolbar
60
-
61
- download="false" print="false" open="false"
62
-
63
- Omitting these attributes will by default show the options in the toolbar.
64
-
65
- The ` on-init ` function is called when PDF.JS is fully loaded. The ` on-page-load ` function is each time a page is
66
- loaded and will pass the page number. When the scale changes all pages are unloaded, so ` on-page-load ` will be called
67
- again for each page.
68
-
69
- ** Controller**
45
+ ### Controller
70
46
``` js
71
47
angular .module (' app' , [' pdfjsViewer' ]);
72
48
73
49
angular .module (' app' ).controller (' AppCtrl' , function ($scope ) {
74
50
$scope .pdf = {
75
51
src: ' example.pdf' ,
76
52
};
77
-
78
- $scope .$watch (' scale' , function () {
79
- ...
80
- });
81
-
82
- $scope .onInit = function () {
83
- ...
84
- };
85
-
86
- $scope .onPageLoad = function (page ) {
87
- ...
88
- };
89
53
});
90
54
```
91
55
92
- _ If ` onPageLoad() ` returns ` false ` , the page will not be marked as loaded and ` onPageLoad ` will be called again for
93
- that page on the next (200ms) interval. _
56
+ ## Directive options
57
+ The ` <pdfjs-viewer> ` directive takes the following attributes.
94
58
95
- ## Demo
96
59
97
- You can test out a demo of this directive. You must run the node server first due to CORS. First make sure
98
- the dependencies are installed.
60
+ #### ` src `
61
+ The source should point to the URL of a publicly available pdf.
62
+ Note that the ` src ` must be passed in as an interpolation string.
63
+
64
+ ``` html
65
+ <pdfjs-viewer src =" {{ $ctrl.src }}" ></pdfjs-viewer >
66
+ ```
67
+
68
+ ``` javascript
69
+ $scope .src = " http://example.com/file.pdf" ;
70
+ ```
71
+ ---
72
+
73
+ #### ` data `
74
+ In the case that you cannot simply use the URL of the pdf, you can pass in raw data as
75
+ a [ Uint8Array] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array ) object.
76
+ The ` data ` attribute takes a scope variable as its argument.
77
+
78
+ ``` html
79
+ <pdfjs-viewer data =" $ctrl.data" ></pdfjs-viewer >
80
+ ```
81
+
82
+ ``` javascript
83
+ $scope .data = null ; // this is loaded async
84
+
85
+ $http .get (" http://example.com/file.pdf" , {
86
+ responseType: ' arraybuffer'
87
+ }).then (function (response ) {
88
+ $scope .data = new Uint8Array (response .data );
89
+ });
90
+ ```
91
+ ---
92
+
93
+ #### ` scale `
94
+ The ` scale ` attribute can be used to obtain the current scale (zoom level) of the PDF.
95
+ The value will be stored in the variable specified. ** This is read only** .
96
+
97
+ ``` html
98
+ <pdfjs-viewer scale =" $ctrl.scale" ></pdfjs-viewer >
99
+ ```
100
+ ---
101
+
102
+ #### ` download, print, open `
103
+ These buttons are by default all visible in the toolbar and can be hidden.
104
+
105
+ ``` html
106
+ <pdfjs-viewer download =" false" print =" false" ... ></pdfjs-viewer >
107
+ ```
108
+ ---
109
+
110
+ #### ` on-init `
111
+ The ` on-init ` function is called when PDF.JS is fully loaded.
112
+
113
+ ``` html
114
+ <pdfjs-viewer on-init =" $ctrl.onInit()" ></pdfjs-viewer >
115
+ ```
116
+
117
+ ``` javascript
118
+ $scope .onInit = function () {
119
+ // pdf.js is initialized
120
+ }
121
+ ```
122
+ ---
123
+
124
+ #### ` on-page-load `
125
+ The ` on-page-load ` function is each time a page is loaded and will pass the page number.
126
+ When the scale changes all pages are unloaded, so ` on-page-load ` will be called again for each page.
127
+ _ If ` onPageLoad() ` returns ` false ` , the page will not be marked as loaded and ` onPageLoad ` will be called again for that page on the next (200ms) interval._
128
+
129
+ ``` html
130
+ <pdfjs-viewer on-init =" $ctrl.onPageLoad()" ></pdfjs-viewer >
131
+ ```
132
+
133
+ ``` javascript
134
+ $scope .onPageLoad = function (page ) {
135
+ // page is loaded
136
+ };
137
+ ```
138
+ ---
139
+
140
+ ## Styling
141
+ The ` <pdfjs-viewer> ` directive automatically expands to the height and width of its first immediate parent, in the case of the example ` .some-pdf-container ` .
142
+ If no parent container is given the html ` body ` will be used. Height and width are required to properly display the contents of the pdf.
143
+
144
+ ## Demo
145
+ You can test out a [ demo] ( https://github.com/legalthings/angular-pdfjs-viewer/tree/master/demo ) of this directive.
146
+ You must run the node server first due to CORS. First make sure the dependencies are installed.
99
147
100
148
cd demo
101
149
npm install
@@ -108,7 +156,6 @@ Afterwards run the server like so.
108
156
The server will be running on localhost:8080
109
157
110
158
## Advanced configuration
111
-
112
159
By default the location of PDF.js assets are automatically determined. However if you place them on alternative
113
160
locations they may not be found. If so, you can configure these locations.
114
161
@@ -129,4 +176,3 @@ Note that a number of images used in the PDF.js viewer are loaded by the `viewer
129
176
through JavaScript. Instead you need to compile the ` viewer.less ` file as
130
177
131
178
lessc --global-var='pdfjsImagePath=/assets/pdf.js-viewer/images' viewer.less viewer.css
132
-
0 commit comments