Skip to content
This repository was archived by the owner on Jun 25, 2024. It is now read-only.

Commit ee7de57

Browse files
authored
Merge pull request #36 from legalthings/source-as-binary
Added attribute to set raw data as the source of the pdf
2 parents cedb0e7 + a0fc098 commit ee7de57

File tree

7 files changed

+197
-78
lines changed

7 files changed

+197
-78
lines changed

README.md

+104-58
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# PDF.js viewer Angular directive
22

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
44
of pdf's we all love. The directive embeds the [full viewer](https://mozilla.github.io/pdf.js/web/viewer.html), which
55
allows you to scroll through the pdf.
66

7-
## Installation
87

8+
![viewer-example](https://cloud.githubusercontent.com/assets/5793511/24605022/6dd5abee-1867-11e7-881a-0d68dc7c77f3.png)
9+
10+
11+
## Installation
912
bower install angular-pdfjs-viewer --save
1013

1114
## 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.
1218

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
1720
```html
1821
<!DOCTYPE html>
19-
<html lang="en" data-ng-app="app" ng-controller="AppCtrl">
22+
<html ng-app="app" ng-controller="AppCtrl">
2023
<head>
21-
<meta charset="utf-8"/>
2224
<title>Angular PDF.js demo</title>
23-
<meta name="viewport" content="width=device-width, initial-scale=1">
24-
2525
<script src="bower_components/pdf.js-viewer/pdf.js"></script>
2626
<link rel="stylesheet" href="bower_components/pdf.js-viewer/viewer.css">
2727

@@ -30,72 +30,120 @@ that images, translations and such are being loaded from the `web` folder.
3030
<script src="app.js"></script>
3131

3232
<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%; }
4435
</style>
4536
</head>
4637
<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>
5240
</div>
5341
</body>
5442
</html>
5543
```
5644

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
7046
```js
7147
angular.module('app', ['pdfjsViewer']);
7248

7349
angular.module('app').controller('AppCtrl', function($scope) {
7450
$scope.pdf = {
7551
src: 'example.pdf',
7652
};
77-
78-
$scope.$watch('scale', function() {
79-
...
80-
});
81-
82-
$scope.onInit = function() {
83-
...
84-
};
85-
86-
$scope.onPageLoad = function(page) {
87-
...
88-
};
8953
});
9054
```
9155

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.
9458

95-
## Demo
9659

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.
99147

100148
cd demo
101149
npm install
@@ -108,7 +156,6 @@ Afterwards run the server like so.
108156
The server will be running on localhost:8080
109157

110158
## Advanced configuration
111-
112159
By default the location of PDF.js assets are automatically determined. However if you place them on alternative
113160
locations they may not be found. If so, you can configure these locations.
114161

@@ -129,4 +176,3 @@ Note that a number of images used in the PDF.js viewer are loaded by the `viewer
129176
through JavaScript. Instead you need to compile the `viewer.less` file as
130177

131178
lessc --global-var='pdfjsImagePath=/assets/pdf.js-viewer/images' viewer.less viewer.css
132-

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"dependencies": {
1717
"angular": "^1.5.8",
18-
"pdf.js-viewer": "~1.6.210"
18+
"pdf.js-viewer": "~1.6.211"
1919
},
2020
"ignore": [
2121
"**/.*",

demo/app.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
angular.module('app', ['pdfjsViewer']);
22

3-
angular.module('app').controller('AppCtrl', function($scope) {
3+
angular.module('app').controller('AppCtrl', function ($scope, $http, $timeout) {
4+
var url = 'example.pdf';
5+
46
$scope.pdf = {
5-
src: 'example.pdf'
7+
src: url, // get pdf source from a URL that points to a pdf
8+
data: null // get pdf source from raw data of a pdf
69
};
10+
11+
getPdfAsArrayBuffer(url).then(function (response) {
12+
$scope.pdf.data = new Uint8Array(response.data);
13+
}, function (err) {
14+
console.log('failed to get pdf as binary:', err);
15+
});
16+
17+
function getPdfAsArrayBuffer (url) {
18+
return $http.get(url, {
19+
responseType: 'arraybuffer',
20+
headers: {
21+
'foo': 'bar'
22+
}
23+
});
24+
}
725
});

demo/bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"private": "true",
66
"dependencies": {
77
"angular": "^1.5.8",
8-
"pdf.js-viewer": "~1.6.210"
8+
"pdf.js-viewer": "~1.6.211"
99
}
1010
}

demo/index.html

+7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@
2828
</head>
2929
<body>
3030
<div class='some-pdf-container'>
31+
<!-- Example of loading pdf from URL string. Note that this must be an interpolation string -->
3132
<pdfjs-viewer src="{{ pdf.src }}"></pdfjs-viewer>
33+
34+
<!--
35+
Example of loading pdf from raw binary data. Note that this must be a scope variable.
36+
Comment upper viewer out if you use this viewer (because multi pdf viewers are currently not supported)
37+
-->
38+
<!-- <pdfjs-viewer data="pdf.data"></pdfjs-viewer> -->
3239
</div>
3340
</body>
3441
</html>

dist/angular-pdfjs-viewer.js

+32-9
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@
379379
onInit: '&',
380380
onPageLoad: '&',
381381
scale: '=?',
382+
src: '@?',
383+
data: '=?'
382384
},
383385
link: function ($scope, $element, $attrs) {
384386
$element.children().wrap('<div class="pdfjs" style="width: 100%; height: 100%;"></div>');
@@ -387,6 +389,13 @@
387389
var loaded = {};
388390
var numLoaded = 0;
389391

392+
if (!window.PDFJS) {
393+
return console.warn("PDFJS is not set! Make sure that pdf.js is loaded before angular-pdfjs-viewer.js is loaded.");
394+
}
395+
396+
// initialize the pdf viewer with (with empty source)
397+
window.PDFJS.webViewerLoad();
398+
390399
function onPdfInit() {
391400
initialised = true;
392401

@@ -403,7 +412,11 @@
403412
}
404413

405414
var poller = $interval(function () {
406-
var pdfViewer = PDFViewerApplication.pdfViewer;
415+
if (!window.PDFViewerApplication) {
416+
return;
417+
}
418+
419+
var pdfViewer = window.PDFViewerApplication.pdfViewer;
407420

408421
if (pdfViewer) {
409422
if ($scope.scale !== pdfViewer.currentScale) {
@@ -414,7 +427,7 @@
414427
} else {
415428
console.warn("PDFViewerApplication.pdfViewer is not set");
416429
}
417-
430+
418431
var pages = document.querySelectorAll('.page');
419432
angular.forEach(pages, function (page) {
420433
var element = angular.element(page);
@@ -442,11 +455,25 @@
442455
$interval.cancel(poller);
443456
});
444457

458+
// watch pdf source
459+
$scope.$watchGroup([
460+
function () { return $scope.src; },
461+
function () { return $scope.data; }
462+
], function (values) {
463+
var src = values[0];
464+
var data = values[1];
465+
466+
if (!src && !data) {
467+
return;
468+
}
469+
470+
window.PDFViewerApplication.open(src || data);
471+
});
472+
473+
// watch other attributes
445474
$scope.$watch(function () {
446-
return $attrs.src;
475+
return $attrs;
447476
}, function () {
448-
if (!$attrs.src) return;
449-
450477
if ($attrs.open === 'false') {
451478
document.getElementById('openFile').setAttribute('hidden', 'true');
452479
document.getElementById('secondaryOpenFile').setAttribute('hidden', 'true');
@@ -469,12 +496,8 @@
469496
if ($attrs.height) {
470497
document.getElementById('outerContainer').style.height = $attrs.height;
471498
}
472-
473-
PDFJS.webViewerLoad($attrs.src);
474499
});
475500
}
476501
};
477502
}]);
478-
479-
//
480503
}();

0 commit comments

Comments
 (0)