diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eae7517..cf4a6a89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Unreleased +- Fix the `/CheckSum` of an embedded file being written as a 32-character literal string of hex digits, instead of the 16 raw bytes of the MD5 digest the PDF specification requires, which validators such as veraPDF and pdfcpu reject. It is now written as a hex string of those bytes. Fixes #1531 - Fix gradients whose last stop comes before 1 stretching the stops over the whole gradient. The last color now stays solid from that stop to the end - Add a `headerRows` table option, which repeats the first rows at the top of each new page. Fixes #1657 - Add a `scaleDown` option to `doc.image()`, which works like `fit` but keeps an image smaller than the rectangle at its own size instead of enlarging it. Fixes #1347 diff --git a/lib/mixins/attachments.js b/lib/mixins/attachments.js index e9f7e6eb..360969f6 100644 --- a/lib/mixins/attachments.js +++ b/lib/mixins/attachments.js @@ -1,5 +1,5 @@ import fs from '#fs'; -import { md5Hex } from '../crypto/md5'; +import { md5Hash } from '../crypto/md5'; import { escapeName } from '../object.js'; import { fromBase64 } from '../binary'; @@ -67,8 +67,7 @@ export default { } // add checksum and size information - const checksum = md5Hex(new Uint8Array(data)); - refBody.Params.CheckSum = new String(checksum); + refBody.Params.CheckSum = md5Hash(new Uint8Array(data)); refBody.Params.Size = data.byteLength; // save some space when embedding the same file again @@ -113,11 +112,15 @@ export default { }, }; +function bytesEqual(a, b) { + return a.length === b.length && a.every((byte, i) => byte === b[i]); +} + /** check two embedded file metadata objects for equality */ function isEqual(a, b) { return ( a.Subtype === b.Subtype && - a.Params.CheckSum.toString() === b.Params.CheckSum.toString() && + bytesEqual(a.Params.CheckSum, b.Params.CheckSum) && a.Params.Size === b.Params.Size && isSameDate(a.Params.CreationDate, b.Params.CreationDate) && isSameDate(a.Params.ModDate, b.Params.ModDate) diff --git a/tests/unit/attachments.spec.js b/tests/unit/attachments.spec.js index d911526a..1efb2466 100644 --- a/tests/unit/attachments.spec.js +++ b/tests/unit/attachments.spec.js @@ -39,7 +39,7 @@ describe('file', () => { /Params << /CreationDate (D:20180201000000Z) /ModDate (D:20180201000000Z) -/CheckSum (${md5}) +/CheckSum <${md5}> /Size 12 >> /Subtype /text#2Fplain @@ -97,7 +97,7 @@ describe('file', () => { /Params << /CreationDate (D:20180201000000Z) /ModDate (D:20180201000000Z) -/CheckSum (${md5}) +/CheckSum <${md5}> /Size 12 >> /Length 20 @@ -155,7 +155,7 @@ describe('file', () => { /Params << /CreationDate (D:20180201000000Z) /ModDate (D:20180201000000Z) -/CheckSum (${md5}) +/CheckSum <${md5}> /Size 12 >> /Length 20