Skip to content

Commit

Permalink
🐛 [placeholder-pdfkit010] Fix parsing issue #281
Browse files Browse the repository at this point in the history
  • Loading branch information
mazali01 authored and vbuch committed Feb 22, 2025
1 parent 4235fe7 commit 5f4a279
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/placeholder-pdfkit010/src/pdfkitAddPlaceholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const pdfkitAddPlaceholder = ({
const acroForm = pdfSlice.slice(0, pdfSlice.indexOf('endobj')).toString();
acroFormId = parseInt(pdf._root.data.AcroForm.toString());

const acroFormFields = acroForm.slice(acroForm.indexOf('/Fields [') + 9, acroForm.indexOf(']'));
const acroFormFields = acroForm.slice(acroForm.indexOf('/Fields [') + 9, acroForm.indexOf(']')).trim();
fieldIds = acroFormFields
.split(' ')
.filter((element, i) => i % 3 === 0)
Expand Down
33 changes: 33 additions & 0 deletions packages/placeholder-pdfkit010/src/pdfkitAddPlaceholder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,37 @@ describe(pdfkitAddPlaceholder, () => {
expect(propBuild.Filter.Name).toEqual('Adobe.PPKLite');
expect(propBuild.App.Name).toEqual('signpdf');
});

it('parses field ids with wrapping spaces', () => {
const {pdf} = createPdfkitDocument(PDFDocument, {});
const fakeAcroFormStr = `
99 0 obj
<<
/Type /AcroForm
/SigFlags 3
/Fields [ 12 0 R 13 0 R ]
>>
endobj`;
const fakeBuffer = Buffer.from(fakeAcroFormStr);
const existingForm = pdf.ref({
Type: 'AcroForm',
SigFlags: SIG_FLAGS.SIGNATURES_EXIST | SIG_FLAGS.APPEND_ONLY,
Fields: [],
});

/* eslint-disable no-underscore-dangle */
pdf._root.data.AcroForm = existingForm;

const refs = pdfkitAddPlaceholder({
...defaults,
pdf,
pdfBuffer: fakeBuffer,
reason: 'test reason',
});

const acroFormFields = refs.form.data.Fields;
expect(acroFormFields).toHaveLength(3);
expect(acroFormFields[0].index).toBe('12');
expect(acroFormFields[1].index).toBe('13');
});
});

0 comments on commit 5f4a279

Please sign in to comment.