Merge pull request #9920 from Snuffleupagus/getMetadata-linearization

[api-minor] Add an `IsLinearized` property to the `PDFDocument.documentInfo` getter, to allow accessing the linearization status through the API (via `PDFDocumentProxy.getMetadata`)
This commit is contained in:
Tim van der Meij 2018-07-29 20:23:22 +02:00 committed by GitHub
commit 3521424576
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 38 deletions

View File

@ -120,6 +120,11 @@ document_properties_page_size_dimension_string={{width}} × {{height}} {{unit}}
# "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by # "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by
# the size, respectively their unit of measurement, name, and orientation, of the (current) page. # the size, respectively their unit of measurement, name, and orientation, of the (current) page.
document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}}) document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})
# LOCALIZATION NOTE (document_properties_linearized): The linearization status of
# the document; usually called "Fast Web View" in English locales of Adobe software.
document_properties_linearized=Fast Web View:
document_properties_linearized_yes=Yes
document_properties_linearized_no=No
document_properties_close=Close document_properties_close=Close
print_progress_message=Preparing document for printing… print_progress_message=Preparing document for printing…

View File

@ -120,6 +120,11 @@ document_properties_page_size_dimension_string={{width}} × {{height}} {{unit}}
# "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by # "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by
# the size, respectively their unit of measurement, name, and orientation, of the (current) page. # the size, respectively their unit of measurement, name, and orientation, of the (current) page.
document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}}) document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})
# LOCALIZATION NOTE (document_properties_linearized): The linearization status of
# the document; usually called "Fast Web View" in English locales of Adobe software.
document_properties_linearized=Snelle webweergave:
document_properties_linearized_yes=Ja
document_properties_linearized_no=Nee
document_properties_close=Sluiten document_properties_close=Sluiten
print_progress_message=Document voorbereiden voor afdrukken… print_progress_message=Document voorbereiden voor afdrukken…

View File

@ -120,6 +120,11 @@ document_properties_page_size_dimension_string={{width}} × {{height}} {{unit}}
# "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by # "{{width}}", "{{height}}", {{unit}}, {{name}}, and {{orientation}} will be replaced by
# the size, respectively their unit of measurement, name, and orientation, of the (current) page. # the size, respectively their unit of measurement, name, and orientation, of the (current) page.
document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}}) document_properties_page_size_dimension_name_string={{width}} × {{height}} {{unit}} ({{name}}, {{orientation}})
# LOCALIZATION NOTE (document_properties_linearized): The linearization status of
# the document; usually called "Fast Web View" in English locales of Adobe software.
document_properties_linearized=Snabb webbvisning:
document_properties_linearized_yes=Ja
document_properties_linearized_no=Nej
document_properties_close=Stäng document_properties_close=Stäng
print_progress_message=Förbereder sidor för utskrift… print_progress_message=Förbereder sidor för utskrift…

View File

@ -379,22 +379,16 @@ var PDFDocument = (function PDFDocumentClosure() {
return true; /* found */ return true; /* found */
} }
var DocumentInfoValidators = { const DocumentInfoValidators = {
get entries() { Title: isString,
// Lazily build this since all the validation functions below are not Author: isString,
// defined until after this file loads. Subject: isString,
return shadow(this, 'entries', { Keywords: isString,
Title: isString, Creator: isString,
Author: isString, Producer: isString,
Subject: isString, CreationDate: isString,
Keywords: isString, ModDate: isString,
Creator: isString, Trapped: isName,
Producer: isString,
CreationDate: isString,
ModDate: isString,
Trapped: isName,
});
},
}; };
PDFDocument.prototype = { PDFDocument.prototype = {
@ -426,16 +420,14 @@ var PDFDocument = (function PDFDocumentClosure() {
}, },
get linearization() { get linearization() {
var linearization = null; let linearization = null;
if (this.stream.length) { try {
try { linearization = Linearization.create(this.stream);
linearization = Linearization.create(this.stream); } catch (err) {
} catch (err) { if (err instanceof MissingDataException) {
if (err instanceof MissingDataException) { throw err;
throw err;
}
info(err);
} }
info(err);
} }
// shadow the prototype getter with a data property // shadow the prototype getter with a data property
return shadow(this, 'linearization', linearization); return shadow(this, 'linearization', linearization);
@ -531,12 +523,13 @@ var PDFDocument = (function PDFDocumentClosure() {
return shadow(this, 'numPages', num); return shadow(this, 'numPages', num);
}, },
get documentInfo() { get documentInfo() {
var docInfo = { const docInfo = {
PDFFormatVersion: this.pdfFormatVersion, PDFFormatVersion: this.pdfFormatVersion,
IsLinearized: !!this.linearization,
IsAcroFormPresent: !!this.acroForm, IsAcroFormPresent: !!this.acroForm,
IsXFAPresent: !!this.xfa, IsXFAPresent: !!this.xfa,
}; };
var infoDict; let infoDict;
try { try {
infoDict = this.xref.trailer.get('Info'); infoDict = this.xref.trailer.get('Info');
} catch (err) { } catch (err) {
@ -545,14 +538,13 @@ var PDFDocument = (function PDFDocumentClosure() {
} }
info('The document information dictionary is invalid.'); info('The document information dictionary is invalid.');
} }
if (infoDict) { if (isDict(infoDict)) {
var validEntries = DocumentInfoValidators.entries;
// Only fill the document info with valid entries from the spec. // Only fill the document info with valid entries from the spec.
for (var key in validEntries) { for (let key in DocumentInfoValidators) {
if (infoDict.has(key)) { if (infoDict.has(key)) {
var value = infoDict.get(key); const value = infoDict.get(key);
// Make sure the value conforms to the spec. // Make sure the value conforms to the spec.
if (validEntries[key](value)) { if (DocumentInfoValidators[key](value)) {
docInfo[key] = (typeof value !== 'string' ? docInfo[key] = (typeof value !== 'string' ?
value : stringToPDFString(value)); value : stringToPDFString(value));
} else { } else {

View File

@ -28,6 +28,7 @@ import {
} from '../../src/display/api'; } from '../../src/display/api';
import { GlobalWorkerOptions } from '../../src/display/worker_options'; import { GlobalWorkerOptions } from '../../src/display/worker_options';
import isNodeJS from '../../src/shared/is_node'; import isNodeJS from '../../src/shared/is_node';
import { Metadata } from '../../src/display/metadata';
describe('api', function() { describe('api', function() {
let basicApiFileName = 'basicapi.pdf'; let basicApiFileName = 'basicapi.pdf';
@ -802,11 +803,18 @@ describe('api', function() {
}); });
it('gets metadata', function(done) { it('gets metadata', function(done) {
var promise = doc.getMetadata(); var promise = doc.getMetadata();
promise.then(function(metadata) { promise.then(function({ info, metadata, contentDispositionFilename, }) {
expect(metadata.info['Title']).toEqual('Basic API Test'); expect(info['Title']).toEqual('Basic API Test');
expect(metadata.info['PDFFormatVersion']).toEqual('1.7'); // The following are PDF.js specific, non-standard, properties.
expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test'); expect(info['PDFFormatVersion']).toEqual('1.7');
expect(metadata.contentDispositionFilename).toEqual(null); expect(info['IsLinearized']).toEqual(false);
expect(info['IsAcroFormPresent']).toEqual(false);
expect(info['IsXFAPresent']).toEqual(false);
expect(metadata instanceof Metadata).toEqual(true);
expect(metadata.get('dc:title')).toEqual('Basic API Test');
expect(contentDispositionFilename).toEqual(null);
done(); done();
}).catch(function (reason) { }).catch(function (reason) {
done.fail(reason); done.fail(reason);

View File

@ -128,9 +128,10 @@ class PDFDocumentProperties {
return this._parsePageSize(getPageSizeInches(pdfPage), return this._parsePageSize(getPageSizeInches(pdfPage),
pagesRotation); pagesRotation);
}), }),
this._parseLinearization(info.IsLinearized),
]); ]);
}).then(([info, metadata, fileName, fileSize, creationDate, modDate, }).then(([info, metadata, fileName, fileSize, creationDate, modDate,
pageSize]) => { pageSize, isLinearized]) => {
freezeFieldData({ freezeFieldData({
'fileName': fileName, 'fileName': fileName,
'fileSize': fileSize, 'fileSize': fileSize,
@ -145,6 +146,7 @@ class PDFDocumentProperties {
'version': info.PDFFormatVersion, 'version': info.PDFFormatVersion,
'pageCount': this.pdfDocument.numPages, 'pageCount': this.pdfDocument.numPages,
'pageSize': pageSize, 'pageSize': pageSize,
'linearized': isLinearized,
'_currentPageNumber': currentPageNumber, '_currentPageNumber': currentPageNumber,
'_pagesRotation': pagesRotation, '_pagesRotation': pagesRotation,
}); });
@ -406,6 +408,15 @@ class PDFDocumentProperties {
{ date: dateString, time: timeString, }, { date: dateString, time: timeString, },
'{{date}}, {{time}}'); '{{date}}, {{time}}');
} }
/**
* @private
*/
_parseLinearization(isLinearized) {
return this.l10n.get('document_properties_linearized_' +
(isLinearized ? 'yes' : 'no'), null,
(isLinearized ? 'Yes' : 'No'));
}
} }
export { export {

View File

@ -378,6 +378,10 @@ See https://github.com/adobe-type-tools/cmap-resources
<div class="row"> <div class="row">
<span data-l10n-id="document_properties_page_size">Page Size:</span> <p id="pageSizeField">-</p> <span data-l10n-id="document_properties_page_size">Page Size:</span> <p id="pageSizeField">-</p>
</div> </div>
<div class="separator"></div>
<div class="row">
<span data-l10n-id="document_properties_linearized">Fast Web View:</span> <p id="linearizedField">-</p>
</div>
<div class="buttonRow"> <div class="buttonRow">
<button id="documentPropertiesClose" class="overlayButton"><span data-l10n-id="document_properties_close">Close</span></button> <button id="documentPropertiesClose" class="overlayButton"><span data-l10n-id="document_properties_close">Close</span></button>
</div> </div>

View File

@ -166,6 +166,7 @@ function getViewerConfiguration() {
'version': document.getElementById('versionField'), 'version': document.getElementById('versionField'),
'pageCount': document.getElementById('pageCountField'), 'pageCount': document.getElementById('pageCountField'),
'pageSize': document.getElementById('pageSizeField'), 'pageSize': document.getElementById('pageSizeField'),
'linearized': document.getElementById('linearizedField'),
}, },
}, },
errorWrapper: { errorWrapper: {