1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-22 16:18:08 +02:00

Implement a permissions API

This commit is contained in:
Tim van der Meij 2018-08-26 21:37:05 +02:00
parent 4874e9ace0
commit 959ed3705b
No known key found for this signature in database
GPG key ID: 8C3FD2925A5F2762
9 changed files with 137 additions and 4 deletions

View file

@ -15,9 +15,9 @@
import {
bytesToString, createPromiseCapability, createValidAbsoluteUrl, FormatError,
info, InvalidPDFException, isBool, isString, MissingDataException, shadow,
stringToPDFString, stringToUTF8String, toRomanNumerals, unreachable, warn,
XRefParseException
info, InvalidPDFException, isBool, isNum, isString, MissingDataException,
PermissionFlag, shadow, stringToPDFString, stringToUTF8String,
toRomanNumerals, unreachable, warn, XRefParseException
} from '../shared/util';
import {
Dict, isCmd, isDict, isName, isRef, isRefsEqual, isStream, Ref, RefSet,
@ -177,6 +177,48 @@ class Catalog {
return (root.items.length > 0 ? root.items : null);
}
get permissions() {
let permissions = null;
try {
permissions = this._readPermissions();
} catch (ex) {
if (ex instanceof MissingDataException) {
throw ex;
}
warn('Unable to read permissions.');
}
return shadow(this, 'permissions', permissions);
}
/**
* @private
*/
_readPermissions() {
const encrypt = this.xref.trailer.get('Encrypt');
if (!isDict(encrypt)) {
return null;
}
let flags = encrypt.get('P');
if (!isNum(flags)) {
return null;
}
// PDF integer objects are represented internally in signed 2's complement
// form. Therefore, convert the signed decimal integer to a signed 2's
// complement binary integer so we can use regular bitwise operations on it.
flags += 2 ** 32;
const permissions = [];
for (const key in PermissionFlag) {
const value = PermissionFlag[key];
if (flags & value) {
permissions.push(value);
}
}
return permissions;
}
get numPages() {
const obj = this.toplevelPagesDict.get('Count');
if (!Number.isInteger(obj)) {

View file

@ -703,6 +703,10 @@ var WorkerMessageHandler = {
}
);
handler.on('GetPermissions', function(data) {
return pdfManager.ensureCatalog('permissions');
});
handler.on('GetMetadata',
function wphSetupGetMetadata(data) {
return Promise.all([pdfManager.ensureDoc('documentInfo'),

View file

@ -691,6 +691,15 @@ class PDFDocumentProxy {
return this._transport.getOutline();
}
/**
* @return {Promise} A promise that is resolved with an {Array} that contains
* the permission flags for the PDF document, or `null` when
* no permissions are present in the PDF file.
*/
getPermissions() {
return this._transport.getPermissions();
}
/**
* @return {Promise} A promise that is resolved with an {Object} that has
* `info` and `metadata` properties. `info` is an {Object} filled with
@ -2141,6 +2150,10 @@ class WorkerTransport {
return this.messageHandler.sendWithPromise('GetOutline', null);
}
getPermissions() {
return this.messageHandler.sendWithPromise('GetPermissions', null);
}
getMetadata() {
return this.messageHandler.sendWithPromise('GetMetadata', null).
then((results) => {

View file

@ -25,6 +25,18 @@ const NativeImageDecoding = {
DISPLAY: 'display',
};
// Permission flags from Table 22, Section 7.6.3.2 of the PDF specification.
const PermissionFlag = {
PRINT: 0x04,
MODIFY_CONTENTS: 0x08,
COPY: 0x10,
MODIFY_ANNOTATIONS: 0x20,
FILL_INTERACTIVE_FORMS: 0x100,
COPY_FOR_ACCESSIBILITY: 0x200,
ASSEMBLE: 0x400,
PRINT_HIGH_QUALITY: 0x800,
};
var TextRenderingMode = {
FILL: 0,
STROKE: 1,
@ -1014,6 +1026,7 @@ export {
NativeImageDecoding,
PasswordException,
PasswordResponses,
PermissionFlag,
StreamType,
TextRenderingMode,
UnexpectedResponseException,