Skip to content

Commit

Permalink
[api-minor] Add support for relative URLs, in both annotations and th…
Browse files Browse the repository at this point in the history
…e outline, by adding a `docBaseUrl` parameter to `PDFJS.getDocument` (bug 766086)

Note that in `FIREFOX/MOZCENTRAL/CHROME` builds of the standard viewer the `docBaseUrl` parameter will be set by default, since in that case it makes sense to use the current URL as a base.
For the `GENERIC` viewer, or the API itself, it doesn't make sense to try and set the `docBaseUrl` by default. However, custom deployments/implementations may still find the parameter useful.
  • Loading branch information
Snuffleupagus committed Oct 19, 2016
1 parent 71a781e commit d284cfd
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 47 deletions.
5 changes: 4 additions & 1 deletion src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ {
/**
* @param {XRef} xref
* @param {Object} ref
* @param {PDFManager} pdfManager
* @param {string} uniquePrefix
* @param {Object} idCounters
* @returns {Annotation}
*/
create: function AnnotationFactory_create(xref, ref,
create: function AnnotationFactory_create(xref, ref, pdfManager,
uniquePrefix, idCounters) {
var dict = xref.fetchIfRef(ref);
if (!isDict(dict)) {
Expand All @@ -88,6 +89,7 @@ AnnotationFactory.prototype = /** @lends AnnotationFactory.prototype */ {
ref: isRef(ref) ? ref : null,
subtype: subtype,
id: id,
pdfManager: pdfManager,
};

switch (subtype) {
Expand Down Expand Up @@ -846,6 +848,7 @@ var LinkAnnotation = (function LinkAnnotationClosure() {
Catalog.parseDestDictionary({
destDict: params.dict,
resultObj: data,
docBaseUrl: params.pdfManager.docBaseUrl,
});
}

Expand Down
1 change: 1 addition & 0 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ var Page = (function PageClosure() {
for (var i = 0, n = annotationRefs.length; i < n; ++i) {
var annotationRef = annotationRefs[i];
var annotation = annotationFactory.create(this.xref, annotationRef,
this.pdfManager,
this.uniquePrefix,
this.idCounters);
if (annotation) {
Expand Down
6 changes: 5 additions & 1 deletion src/core/obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ var Catalog = (function CatalogClosure() {
Catalog.parseDestDictionary({
destDict: outlineDict,
resultObj: data,
docBaseUrl: this.pdfManager.docBaseUrl,
});
var title = outlineDict.get('Title');
var flags = outlineDict.get('F') || 0;
Expand Down Expand Up @@ -590,6 +591,8 @@ var Catalog = (function CatalogClosure() {
* @param {Dict} destDict - The dictionary containing the destination.
* @param {Object} resultObj - The object where the parsed destination
* properties will be placed.
* @param {string} docBaseUrl - (optional) The document base URL that is used
* when attempting to recover valid absolute URLs from relative ones.
*/
Catalog.parseDestDictionary = function Catalog_parseDestDictionary(params) {
// Lets URLs beginning with 'www.' default to using the 'http://' protocol.
Expand Down Expand Up @@ -619,6 +622,7 @@ var Catalog = (function CatalogClosure() {
warn('Catalog_parseDestDictionary: "resultObj" must be an object.');
return;
}
var docBaseUrl = params.docBaseUrl || null;

var action = destDict.get('A'), url, dest;
if (isDict(action)) {
Expand Down Expand Up @@ -694,7 +698,7 @@ var Catalog = (function CatalogClosure() {

if (isString(url)) {
url = tryConvertUrlEncoding(url);
var absoluteUrl = createValidAbsoluteUrl(url);
var absoluteUrl = createValidAbsoluteUrl(url, docBaseUrl);
if (absoluteUrl) {
resultObj.url = absoluteUrl.href;
}
Expand Down
24 changes: 22 additions & 2 deletions src/core/pdf_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
}(this, function (exports, sharedUtil, coreStream, coreChunkedStream,
coreDocument) {

var warn = sharedUtil.warn;
var createValidAbsoluteUrl = sharedUtil.createValidAbsoluteUrl;
var shadow = sharedUtil.shadow;
var NotImplementedException = sharedUtil.NotImplementedException;
var MissingDataException = sharedUtil.MissingDataException;
var createPromiseCapability = sharedUtil.createPromiseCapability;
Expand All @@ -49,6 +52,19 @@ var BasePdfManager = (function BasePdfManagerClosure() {
return this._docId;
},

get docBaseUrl() {
var docBaseUrl = null;
if (this._docBaseUrl) {
var absoluteUrl = createValidAbsoluteUrl(this._docBaseUrl);
if (absoluteUrl) {
docBaseUrl = absoluteUrl.href;
} else {
warn('Invalid absolute docBaseUrl: "' + this._docBaseUrl + '".');
}
}
return shadow(this, 'docBaseUrl', docBaseUrl);
},

onLoadedStream: function BasePdfManager_onLoadedStream() {
throw new NotImplementedException();
},
Expand Down Expand Up @@ -110,8 +126,10 @@ var BasePdfManager = (function BasePdfManagerClosure() {
})();

var LocalPdfManager = (function LocalPdfManagerClosure() {
function LocalPdfManager(docId, data, password, evaluatorOptions) {
function LocalPdfManager(docId, data, password, evaluatorOptions,
docBaseUrl) {
this._docId = docId;
this._docBaseUrl = docBaseUrl;
this.evaluatorOptions = evaluatorOptions;
var stream = new Stream(data);
this.pdfDocument = new PDFDocument(this, stream, password);
Expand Down Expand Up @@ -158,8 +176,10 @@ var LocalPdfManager = (function LocalPdfManagerClosure() {
})();

var NetworkPdfManager = (function NetworkPdfManagerClosure() {
function NetworkPdfManager(docId, pdfNetworkStream, args, evaluatorOptions) {
function NetworkPdfManager(docId, pdfNetworkStream, args, evaluatorOptions,
docBaseUrl) {
this._docId = docId;
this._docBaseUrl = docBaseUrl;
this.msgHandler = args.msgHandler;
this.evaluatorOptions = evaluatorOptions;

Expand Down
7 changes: 4 additions & 3 deletions src/core/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ var WorkerMessageHandler = {
var WorkerTasks = [];

var docId = docParams.docId;
var docBaseUrl = docParams.docBaseUrl;
var workerHandlerName = docParams.docId + '_worker';
var handler = new MessageHandler(workerHandlerName, docId, port);

Expand Down Expand Up @@ -544,7 +545,7 @@ var WorkerMessageHandler = {
if (source.data) {
try {
pdfManager = new LocalPdfManager(docId, source.data, source.password,
evaluatorOptions);
evaluatorOptions, docBaseUrl);
pdfManagerCapability.resolve(pdfManager);
} catch (ex) {
pdfManagerCapability.reject(ex);
Expand Down Expand Up @@ -593,7 +594,7 @@ var WorkerMessageHandler = {
length: fullRequest.contentLength,
disableAutoFetch: disableAutoFetch,
rangeChunkSize: source.rangeChunkSize
}, evaluatorOptions);
}, evaluatorOptions, docBaseUrl);
pdfManagerCapability.resolve(pdfManager);
cancelXHRs = null;
}).catch(function (reason) {
Expand All @@ -610,7 +611,7 @@ var WorkerMessageHandler = {
// the data is array, instantiating directly from it
try {
pdfManager = new LocalPdfManager(docId, pdfFile, source.password,
evaluatorOptions);
evaluatorOptions, docBaseUrl);
pdfManagerCapability.resolve(pdfManager);
} catch (ex) {
pdfManagerCapability.reject(ex);
Expand Down
4 changes: 4 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ if (typeof PDFJSDev !== 'undefined' &&
* 2^16 = 65536.
* @property {PDFWorker} worker - The worker that will be used for the loading
* and parsing of the PDF data.
* @property {string} docBaseUrl - (optional) The base URL of the document,
* used when attempting to recover valid absolute URLs for annotations, and
* outline items, that (incorrectly) only specify relative URLs.
*/

/**
Expand Down Expand Up @@ -301,6 +304,7 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
disableCreateObjectURL: getDefaultSetting('disableCreateObjectURL'),
postMessageTransfers: getDefaultSetting('postMessageTransfers') &&
!isPostMessageTransfersDisabled,
docBaseUrl: source.docBaseUrl,
}).then(function (workerId) {
if (worker.destroyed) {
throw new Error('Worker was destroyed');
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
!arial_unicode_ab_cidfont.pdf
!arial_unicode_en_cidfont.pdf
!asciihexdecode.pdf
!bug766086.pdf
!bug793632.pdf
!bug1020858.pdf
!bug1050040.pdf
Expand Down
Binary file added test/pdfs/bug766086.pdf
Binary file not shown.
Loading

0 comments on commit d284cfd

Please sign in to comment.