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

Preliminary attachments support

Added a partial Filespec support
Added getAttachments in API
Added a new attachments view in UI (with a new icon by @shorlander)
This commit is contained in:
Samuel Chantaraud 2014-03-18 16:32:47 -04:00
parent 4379f16346
commit 25ee0e8572
13 changed files with 292 additions and 38 deletions

View file

@ -454,6 +454,30 @@ var Catalog = (function CatalogClosure() {
}
return shadow(this, 'destinations', dests);
},
get attachments() {
var xref = this.xref;
var attachments, nameTreeRef;
var obj = this.catDict.get('Names');
if (obj) {
nameTreeRef = obj.getRaw('EmbeddedFiles');
}
if (nameTreeRef) {
var nameTree = new NameTree(nameTreeRef, xref);
var names = nameTree.getAll();
for (var name in names) {
if (!names.hasOwnProperty(name)) {
continue;
}
var fs = new FileSpec(names[name], xref);
if (!attachments) {
attachments = {};
}
attachments[stringToPDFString(name)] = fs.serializable;
}
}
return shadow(this, 'attachments', attachments);
},
get javaScript() {
var xref = this.xref;
var obj = this.catDict.get('Names');
@ -1315,6 +1339,97 @@ var NameTree = (function NameTreeClosure() {
return NameTree;
})();
/**
* "A PDF file can refer to the contents of another file by using a File
* Specification (PDF 1.1)", see the spec (7.11) for more details.
* NOTE: Only embedded files are supported (as part of the attachments support)
* TODO: support the 'URL' file system (with caching if !/V), portable
* collections attributes and related files (/RF)
*/
var FileSpec = (function FileSpecClosure() {
function FileSpec(root, xref) {
if (!root || !isDict(root)) {
return;
}
this.xref = xref;
this.root = root;
if (root.has('FS')) {
this.fs = root.get('FS');
}
this.description = root.has('Desc') ?
stringToPDFString(root.get('Desc')) :
'';
if (root.has('RF')) {
warn('Related file specifications are not supported');
}
this.contentAvailable = true;
if (!root.has('EF')) {
this.contentAvailable = false;
warn('Non-embedded file specifications are not supported');
}
}
function pickPlatformItem(dict) {
// Look for the filename in this order:
// UF, F, Unix, Mac, DOS
if (dict.has('UF')) {
return dict.get('UF');
} else if (dict.has('F')) {
return dict.get('F');
} else if (dict.has('Unix')) {
return dict.get('Unix');
} else if (dict.has('Mac')) {
return dict.get('Mac');
} else if (dict.has('DOS')) {
return dict.get('DOS');
} else {
return null;
}
}
FileSpec.prototype = {
get filename() {
if (!this._filename && this.root) {
var filename = pickPlatformItem(this.root) || 'unnamed';
this._filename = stringToPDFString(filename).
replace(/\\\\/g, '\\').
replace(/\\\//g, '/').
replace(/\\/g, '/');
}
return this._filename;
},
get content() {
if (!this.contentAvailable) {
return null;
}
if (!this.contentRef && this.root) {
this.contentRef = pickPlatformItem(this.root.get('EF'));
}
var content = null;
if (this.contentRef) {
var xref = this.xref;
var fileObj = xref.fetchIfRef(this.contentRef);
if (fileObj && isStream(fileObj)) {
content = fileObj.getBytes();
} else {
warn('Embedded file specification points to non-existing/invalid ' +
'content');
}
} else {
warn('Embedded file specification does not have a content');
}
return content;
},
get serializable() {
return {
filename: this.filename,
content: this.content
};
}
};
return FileSpec;
})();
/**
* A helper for loading missing data in object graphs. It traverses the graph
* depth first and queues up any objects that have missing data. Once it has