1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-25 17:48:07 +02:00

Support password and add the relevant l10n strings

This commit is contained in:
Jakob Miland 2012-05-14 20:45:07 +02:00
parent b7ea788b0c
commit 0a30d3961b
9 changed files with 122 additions and 39 deletions

View file

@ -7,20 +7,46 @@
* is used, which means it must follow the same origin rules that any XHR does
* e.g. No cross domain requests without CORS.
*
* @param {string|TypedAray} source Either a url to a PDF is located or a
* typed array (Uint8Array) already populated with data.
* @param {Object} headers An object containing the http headers like this:
* { Authorization: "BASIC XXX" }.
* @param {string|TypedAray|object} source Can be an url to where a PDF is
* located, a typed array (Uint8Array) already populated with data or
* and parameter object with the following possible fields:
* - url - The URL of the PDF.
* - data - A typed array with PDF data.
* - httpHeaders - Basic authentication headers.
* - password - For decrypting password-protected PDFs.
*
* @return {Promise} A promise that is resolved with {PDFDocumentProxy} object.
*/
PDFJS.getDocument = function getDocument(source, headers) {
PDFJS.getDocument = function getDocument(source) {
var url, data, headers, password, parameters = {};
if (typeof source === 'string') {
url = params;
} else if (isArrayBuffer(source)) {
data = source;
} else if (typeof source === 'object') {
url = source.url;
data = source.data;
headers = source.httpHeaders;
password = source.password;
parameters.password = password || null;
if (!url && !data)
error('Invalid parameter array, need either .data or .url');
} else {
error('Invalid parameter in getDocument, need either Uint8Array, ' +
'string or a parameter object');
}
var promise = new PDFJS.Promise();
var transport = new WorkerTransport(promise);
if (typeof source === 'string') {
if (data) {
// assuming the data is array, instantiating directly from it
transport.sendData(data, parameters);
} else if (url) {
// fetch url
PDFJS.getPdf(
{
url: source,
url: url,
progress: function getPDFProgress(evt) {
if (evt.lengthComputable)
promise.progress({
@ -35,12 +61,10 @@ PDFJS.getDocument = function getDocument(source, headers) {
headers: headers
},
function getPDFLoad(data) {
transport.sendData(data);
transport.sendData(data, parameters);
});
} else {
// assuming the source is array, instantiating directly from it
transport.sendData(source);
}
return promise;
};
@ -122,6 +146,11 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
});
return promise;
},
isEncrypted: function PDFDocumentProxy_isEncrypted() {
var promise = new PDFJS.Promise();
promise.resolve(this.pdfInfo.encrypted);
return promise;
},
destroy: function PDFDocumentProxy_destroy() {
this.transport.destroy();
}
@ -467,6 +496,14 @@ var WorkerTransport = (function WorkerTransportClosure() {
this.workerReadyPromise.resolve(pdfDocument);
}, this);
messageHandler.on('NeedPassword', function transportPassword(data) {
this.workerReadyPromise.reject(data.exception.message, data.exception);
}, this);
messageHandler.on('IncorrectPassword', function transportBadPass(data) {
this.workerReadyPromise.reject(data.exception.message, data.exception);
}, this);
messageHandler.on('GetPage', function transportPage(data) {
var pageInfo = data.pageInfo;
var page = new PDFPageProxy(pageInfo, this);
@ -569,8 +606,8 @@ var WorkerTransport = (function WorkerTransportClosure() {
});
},
sendData: function WorkerTransport_sendData(data) {
this.messageHandler.send('GetDocRequest', data);
sendData: function WorkerTransport_sendData(data, params) {
this.messageHandler.send('GetDocRequest', {data: data, params: params});
},
getPage: function WorkerTransport_getPage(pageNumber, promise) {