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

[api-minor] Support accessing both the original and modified PDF fingerprint

The PDF.js API has only ever supported accessing the original file ID, however the second one that (should) exist in *modified* documents have thus far been completely inaccessible through the API.
That seems like a simple oversight, caused e.g. by the viewer not needing it, since it really shouldn't hurt to provide API-users with the ability to check if a PDF document has been modified since its creation.[1]

Please refer to https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf#G13.2261661 for additional information.

For an example of how to update existing code to use the new API, please see the changes in the `web/app.js` file included in this patch.

*Please note:* While I'm not sure if we'll ever be able to remove the old `PDFDocumentProxy.fingerprint` getter, given that it's existed since "forever", that probably isn't a big deal given that it's now limited to only `GENERIC`-builds.

---
[1] Although this obviously depends on the PDF software following the specification, by updating the second file ID as intended.
This commit is contained in:
Jonas Jenwald 2021-07-02 16:36:27 +02:00
parent f9d506cf50
commit 661c60ecc9
5 changed files with 77 additions and 33 deletions

View file

@ -1153,30 +1153,44 @@ class PDFDocument {
return shadow(this, "documentInfo", docInfo);
}
get fingerprint() {
let hash;
get fingerprints() {
function validate(data) {
return (
typeof data === "string" &&
data.length > 0 &&
data !== EMPTY_FINGERPRINT
);
}
function hexString(hash) {
const buf = [];
for (let i = 0, ii = hash.length; i < ii; i++) {
const hex = hash[i].toString(16);
buf.push(hex.padStart(2, "0"));
}
return buf.join("");
}
const idArray = this.xref.trailer.get("ID");
if (
Array.isArray(idArray) &&
idArray[0] &&
isString(idArray[0]) &&
idArray[0] !== EMPTY_FINGERPRINT
) {
hash = stringToBytes(idArray[0]);
let hashOriginal, hashModified;
if (Array.isArray(idArray) && validate(idArray[0])) {
hashOriginal = stringToBytes(idArray[0]);
if (idArray[1] !== idArray[0] && validate(idArray[1])) {
hashModified = stringToBytes(idArray[1]);
}
} else {
hash = calculateMD5(
hashOriginal = calculateMD5(
this.stream.getByteRange(0, FINGERPRINT_FIRST_BYTES),
0,
FINGERPRINT_FIRST_BYTES
);
}
const fingerprintBuf = [];
for (let i = 0, ii = hash.length; i < ii; i++) {
const hex = hash[i].toString(16);
fingerprintBuf.push(hex.padStart(2, "0"));
}
return shadow(this, "fingerprint", fingerprintBuf.join(""));
return shadow(this, "fingerprints", [
hexString(hashOriginal),
hashModified ? hexString(hashModified) : null,
]);
}
_getLinearizationPage(pageIndex) {