Skip to content

Commit

Permalink
feat: managing onPassword requests
Browse files Browse the repository at this point in the history
Ref #115
  • Loading branch information
dennybiasiolli committed Dec 4, 2016
1 parent 3a4a28e commit 4bde4df
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions example/js/controllers/docCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ app.controller('DocCtrl', function($scope) {

$scope.pdfName = 'Relativity: The Special and General Theory by Albert Einstein';
$scope.pdfUrl = 'pdf/relativity.pdf';
$scope.pdfPassword = 'test';
$scope.scroll = 0;
$scope.loading = 'loading';

Expand All @@ -22,4 +23,12 @@ app.controller('DocCtrl', function($scope) {
console.log(progressData);
};

$scope.onPassword = function (updatePasswordFn, passwordResponse) {
if (passwordResponse === PDFJS.PasswordResponses.NEED_PASSWORD) {
updatePasswordFn($scope.pdfPassword);
} else if (passwordResponse === PDFJS.PasswordResponses.INCORRECT_PASSWORD) {
console.log('Incorrect password')
}
};

});
Binary file added example/pdf/relativity.protected.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Check [`bower.json` file](https://github.com/sayanee/angularjs-pdf/blob/master/b
- handles error
- show loading of pdf
- show progress percentage of loading pdf
- insert password for protected PDFs
- dynamically change the pdf url
- support retina canvas
- set authorization or http headers
Expand Down Expand Up @@ -207,6 +208,19 @@ $scope.onProgress = function(progress) {
}
```
### Managing password requests
In the controller, you can use the function `scope.onPassword`. This function is called when the PDF require an opening password.
```js
$scope.onPassword = function (updatePasswordFn, passwordResponse) {
// if passwordResponse === PDFJS.PasswordResponses.NEED_PASSWORD
// you can provide the password calling updatePasswordFn('THE_PASSWORD')
// else if passwordResponse === PDFJS.PasswordResponses.INCORRECT_PASSWORD
// provided password is not correct
};
```
## Variations
1. If using with [Angular UI modal](http://angular-ui.github.io/bootstrap/#/modal), `pageNum` attribute is no longer required. [Checkout the implementation](https://github.com/sayanee/angularjs-pdf/issues/12)
Expand Down
1 change: 1 addition & 0 deletions src/angular-pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
if (url && url.length) {
pdfLoaderTask = PDFJS.getDocument(params);
pdfLoaderTask.onProgress = scope.onProgress;
pdfLoaderTask.onPassword = scope.onPassword;
pdfLoaderTask.then(
function(_pdfDoc) {
if (typeof scope.onLoad === 'function') {
Expand Down
1 change: 1 addition & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = function(config) {
// list of files / patterns to load in the browser
files: [
{pattern: 'example/pdf/relativity.pdf', included: false, served: true},
{pattern: 'example/pdf/relativity.protected.pdf', included: false, served: true},
'bower_components/pdfjs-dist/web/compatibility.js',
'example/js/lib/pdf.js',
'example/js/lib/pdf.worker.js',
Expand Down
36 changes: 36 additions & 0 deletions test/spec/ngPdfSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,39 @@ describe('ngPdf', function() {
expect($scope.pageNum).toBe(13);
});
});

describe('ngPdf protected', function() {
console.log = function() {};
var element, $scope;

// Load the myApp module, which contains the directive
beforeEach(module('App'));

beforeEach(module('my.templates'));

beforeEach(inject(function(_$compile_, _$rootScope_, _$document_){
// The injector unwraps the underscores (_) from around the parameter names when matching
var $compile = _$compile_;
var $rootScope = _$rootScope_;
var $document = _$document_;
$scope = $rootScope.$new();
// Compile a piece of HTML containing the directive
var html = '<ng-pdf template-url="partials/viewer.html" canvasid="pdf" scale="page-fit" page=13></ng-pdf>';
var elmnt = angular.element(html);
$document.find('body').append(elmnt);
element = $compile(elmnt)($scope);
$scope.pdfUrl = '/pdf/relativity.protected.pdf';
$scope.onPassword = jasmine.createSpy('onPassword');
$scope.$digest();
}));

beforeEach(function(done){
setTimeout(function() {
done();
}, 9000);
}, 10000);

it('should call onPassword function', function() {
expect($scope.onPassword).toHaveBeenCalled();
});
});

0 comments on commit 4bde4df

Please sign in to comment.