From 9a22a61b8ff9ff4c18e1b616ceeb78dcfa11d0e3 Mon Sep 17 00:00:00 2001 From: Valery Buchinsky Date: Wed, 4 Oct 2023 14:04:39 +0300 Subject: [PATCH] All covered and PLAIN fixed --- .../dist/pdfkitAddPlaceholder.d.ts.map | 2 +- .../dist/pdfkitAddPlaceholder.js | 2 ++ .../src/pdfkitAddPlaceholder.js | 2 ++ .../dist/plainAddPlaceholder.d.ts.map | 2 +- .../dist/plainAddPlaceholder.js | 24 ++++++++++++------ packages/placeholder-plain/package.json | 1 + .../src/plainAddPlaceholder.js | 25 +++++++++++++------ .../src/plainAddPlaceholder.test.js | 1 + 8 files changed, 41 insertions(+), 18 deletions(-) diff --git a/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.d.ts.map b/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.d.ts.map index 493fb0f7..35b36c9d 100644 --- a/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.d.ts.map +++ b/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"pdfkitAddPlaceholder.d.ts","sourceRoot":"","sources":["../src/pdfkitAddPlaceholder.js"],"names":[],"mappings":"AAmCO,iJAHI,SAAS,GACP,UAAU,CAgHtB;;;;;SAvIY,MAAM;eACN,MAAM;YACN,MAAM;iBACN,MAAM;UACN,MAAM;cACN,MAAM;qBACN,MAAM;0BACN,MAAM;;;;eACN,MAAM;;;eAKN,GAAG;UACH,GAAG;YACH,GAAG"} \ No newline at end of file +{"version":3,"file":"pdfkitAddPlaceholder.d.ts","sourceRoot":"","sources":["../src/pdfkitAddPlaceholder.js"],"names":[],"mappings":"AAmCO,iJAHI,SAAS,GACP,UAAU,CAkHtB;;;;;SAzIY,MAAM;eACN,MAAM;YACN,MAAM;iBACN,MAAM;UACN,MAAM;cACN,MAAM;qBACN,MAAM;0BACN,MAAM;;;;eACN,MAAM;;;eAKN,GAAG;UACH,GAAG;YACH,GAAG"} \ No newline at end of file diff --git a/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.js b/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.js index 5722564c..e75dd23a 100644 --- a/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.js +++ b/packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.js @@ -65,6 +65,8 @@ export const pdfkitAddPlaceholder = ({ if (isAcroFormExists) { /* FIXME: We're working with a PDFDocument. * Needing to work with strings here doesn't make sense. + * It only exists to support plainAddPlaceholder the reference to /AcroForm + * would be external to PDFKit at this point. */ const acroFormPosition = pdfBuffer.lastIndexOf('/Type /AcroForm'); diff --git a/packages/placeholder-pdfkit010/src/pdfkitAddPlaceholder.js b/packages/placeholder-pdfkit010/src/pdfkitAddPlaceholder.js index 8f8e8f43..ea3db8ce 100644 --- a/packages/placeholder-pdfkit010/src/pdfkitAddPlaceholder.js +++ b/packages/placeholder-pdfkit010/src/pdfkitAddPlaceholder.js @@ -72,6 +72,8 @@ export const pdfkitAddPlaceholder = ({ if (isAcroFormExists) { /* FIXME: We're working with a PDFDocument. * Needing to work with strings here doesn't make sense. + * It only exists to support plainAddPlaceholder the reference to /AcroForm + * would be external to PDFKit at this point. */ const acroFormPosition = pdfBuffer.lastIndexOf('/Type /AcroForm'); diff --git a/packages/placeholder-plain/dist/plainAddPlaceholder.d.ts.map b/packages/placeholder-plain/dist/plainAddPlaceholder.d.ts.map index bc3865ed..3bd765ed 100644 --- a/packages/placeholder-plain/dist/plainAddPlaceholder.d.ts.map +++ b/packages/placeholder-plain/dist/plainAddPlaceholder.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"plainAddPlaceholder.d.ts","sourceRoot":"","sources":["../src/plainAddPlaceholder.js"],"names":[],"mappings":"AAoDO,qHAHI,SAAS,GACP,MAAM,CAsFlB;;eAzGY,MAAM;YACN,MAAM;iBACN,MAAM;UACN,MAAM;cACN,MAAM;qBACN,MAAM;;;;eACN,MAAM"} \ No newline at end of file +{"version":3,"file":"plainAddPlaceholder.d.ts","sourceRoot":"","sources":["../src/plainAddPlaceholder.js"],"names":[],"mappings":"AAwDO,qHAHI,SAAS,GACP,MAAM,CA2FlB;;eA9GY,MAAM;YACN,MAAM;iBACN,MAAM;UACN,MAAM;cACN,MAAM;qBACN,MAAM;;;;eACN,MAAM"} \ No newline at end of file diff --git a/packages/placeholder-plain/dist/plainAddPlaceholder.js b/packages/placeholder-plain/dist/plainAddPlaceholder.js index 99dd7d3f..08ac06f4 100644 --- a/packages/placeholder-plain/dist/plainAddPlaceholder.js +++ b/packages/placeholder-plain/dist/plainAddPlaceholder.js @@ -1,3 +1,4 @@ +/* eslint-disable no-underscore-dangle */ import { pdfkitAddPlaceholder, PDFKitReferenceMock, PDFObject } from '@signpdf/placeholder-pdfkit010'; import { removeTrailingNewLine, DEFAULT_SIGNATURE_LENGTH, SUBFILTER_ADOBE_PKCS7_DETACHED } from '@signpdf/utils'; import getIndexFromRef from './getIndexFromRef'; @@ -8,13 +9,16 @@ import createBufferPageWithAnnotation from './createBufferPageWithAnnotation'; import createBufferTrailer from './createBufferTrailer'; /** - * @param {Buffer} pdf - * @returns {boolean} + * @param {string} pdf + * @returns {string | undefined} */ -const isContainBufferRootWithAcroform = pdf => { +const getAcroFormRef = slice => { const bufferRootWithAcroformRefRegex = /\/AcroForm\s+(\d+\s\d+\sR)/g; - const match = bufferRootWithAcroformRefRegex.exec(pdf.toString()); - return match != null && match[1] != null && match[1] !== ''; + const match = bufferRootWithAcroformRefRegex.exec(slice); + if (match != null && match[1] != null && match[1] !== '') { + return match[1]; + } + return undefined; }; /** @@ -55,9 +59,9 @@ export const plainAddPlaceholder = ({ const pageIndex = getIndexFromRef(info.xref, pageRef); const addedReferences = new Map(); const pdfKitMock = { - ref: (input, additionalIndex) => { + ref: (input, knownIndex) => { info.xref.maxIndex += 1; - const index = additionalIndex != null ? additionalIndex : info.xref.maxIndex; + const index = knownIndex != null ? knownIndex : info.xref.maxIndex; addedReferences.set(index, pdf.length + 1); // + 1 new line pdf = Buffer.concat([pdf, Buffer.from('\n'), Buffer.from(`${index} 0 obj\n`), Buffer.from(PDFObject.convert(input)), Buffer.from('\nendobj\n')]); @@ -74,6 +78,10 @@ export const plainAddPlaceholder = ({ data: {} } }; + const acroFormRef = getAcroFormRef(info.root); + if (acroFormRef) { + pdfKitMock._root.data.AcroForm = acroFormRef; + } const { form, widget @@ -87,7 +95,7 @@ export const plainAddPlaceholder = ({ signatureLength, subFilter }); - if (!isContainBufferRootWithAcroform(pdf)) { + if (!getAcroFormRef(pdf.toString())) { const rootIndex = getIndexFromRef(info.xref, info.rootRef); addedReferences.set(rootIndex, pdf.length + 1); pdf = Buffer.concat([pdf, Buffer.from('\n'), createBufferRootWithAcroform(pdf, info, form)]); diff --git a/packages/placeholder-plain/package.json b/packages/placeholder-plain/package.json index 5c7cdbda..1de108f3 100644 --- a/packages/placeholder-plain/package.json +++ b/packages/placeholder-plain/package.json @@ -51,6 +51,7 @@ "@babel/plugin-syntax-object-rest-spread": "^7.0.0", "@babel/preset-env": "^7.4.2", "@signpdf/internal-utils": "*", + "@signpdf/placeholder-pdfkit010": "*", "@types/node": ">=12.0.0", "@types/node-forge": "^1.2.1", "assertion-error": "^1.1.0", diff --git a/packages/placeholder-plain/src/plainAddPlaceholder.js b/packages/placeholder-plain/src/plainAddPlaceholder.js index 873865da..344dc2e9 100644 --- a/packages/placeholder-plain/src/plainAddPlaceholder.js +++ b/packages/placeholder-plain/src/plainAddPlaceholder.js @@ -1,3 +1,4 @@ +/* eslint-disable no-underscore-dangle */ import { pdfkitAddPlaceholder, PDFKitReferenceMock, @@ -17,14 +18,17 @@ import createBufferPageWithAnnotation from './createBufferPageWithAnnotation'; import createBufferTrailer from './createBufferTrailer'; /** - * @param {Buffer} pdf - * @returns {boolean} + * @param {string} pdf + * @returns {string | undefined} */ -const isContainBufferRootWithAcroform = (pdf) => { +const getAcroFormRef = (slice) => { const bufferRootWithAcroformRefRegex = /\/AcroForm\s+(\d+\s\d+\sR)/g; - const match = bufferRootWithAcroformRefRegex.exec(pdf.toString()); + const match = bufferRootWithAcroformRefRegex.exec(slice); - return match != null && match[1] != null && match[1] !== ''; + if (match != null && match[1] != null && match[1] !== '') { + return match[1]; + } + return undefined; }; /** @@ -66,10 +70,10 @@ export const plainAddPlaceholder = ({ const addedReferences = new Map(); const pdfKitMock = { - ref: (input, additionalIndex) => { + ref: (input, knownIndex) => { info.xref.maxIndex += 1; - const index = additionalIndex != null ? additionalIndex : info.xref.maxIndex; + const index = knownIndex != null ? knownIndex : info.xref.maxIndex; addedReferences.set(index, pdf.length + 1); // + 1 new line @@ -97,6 +101,11 @@ export const plainAddPlaceholder = ({ }, }; + const acroFormRef = getAcroFormRef(info.root); + if (acroFormRef) { + pdfKitMock._root.data.AcroForm = acroFormRef; + } + const { form, widget, @@ -111,7 +120,7 @@ export const plainAddPlaceholder = ({ subFilter, }); - if (!isContainBufferRootWithAcroform(pdf)) { + if (!getAcroFormRef(pdf.toString())) { const rootIndex = getIndexFromRef(info.xref, info.rootRef); addedReferences.set(rootIndex, pdf.length + 1); pdf = Buffer.concat([ diff --git a/packages/placeholder-plain/src/plainAddPlaceholder.test.js b/packages/placeholder-plain/src/plainAddPlaceholder.test.js index eab59d4a..9238814d 100644 --- a/packages/placeholder-plain/src/plainAddPlaceholder.test.js +++ b/packages/placeholder-plain/src/plainAddPlaceholder.test.js @@ -15,6 +15,7 @@ describe(plainAddPlaceholder, () => { expect(output).toBeInstanceOf(Buffer); expect(output.indexOf('/ByteRange')).not.toBe(-1); }); + it('adds placeholder when there already is one', () => { const input = readTestResource('signed-once.pdf'); expect(input.indexOf('/ByteRange')).toBe(13342);