Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions lib/mixins/attachments.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) &&

@blikblum blikblum Nov 12, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK is not needed bytesEqual helper.

Either use the .toString or `${a.Params.CheckSum}` === `${b.Params.CheckSum}`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tested to make sure:

https://onecompiler.com/nodejs/458xxybu8

a.Params.Size === b.Params.Size &&
isSameDate(a.Params.CreationDate, b.Params.CreationDate) &&
isSameDate(a.Params.ModDate, b.Params.ModDate)
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/attachments.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('file', () => {
/Params <<
/CreationDate (D:20180201000000Z)
/ModDate (D:20180201000000Z)
/CheckSum (${md5})
/CheckSum <${md5}>
/Size 12
>>
/Subtype /text#2Fplain
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('file', () => {
/Params <<
/CreationDate (D:20180201000000Z)
/ModDate (D:20180201000000Z)
/CheckSum (${md5})
/CheckSum <${md5}>
/Size 12
>>
/Length 20
Expand Down Expand Up @@ -155,7 +155,7 @@ describe('file', () => {
/Params <<
/CreationDate (D:20180201000000Z)
/ModDate (D:20180201000000Z)
/CheckSum (${md5})
/CheckSum <${md5}>
/Size 12
>>
/Length 20
Expand Down
Loading