Skip to content

Commit

Permalink
All covered and PLAIN fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuch committed Oct 4, 2023
1 parent d6b4629 commit 9a22a61
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 18 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/placeholder-pdfkit010/dist/pdfkitAddPlaceholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 2 additions & 0 deletions packages/placeholder-pdfkit010/src/pdfkitAddPlaceholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 16 additions & 8 deletions packages/placeholder-plain/dist/plainAddPlaceholder.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
};

/**
Expand Down Expand Up @@ -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')]);
Expand All @@ -74,6 +78,10 @@ export const plainAddPlaceholder = ({
data: {}
}
};
const acroFormRef = getAcroFormRef(info.root);
if (acroFormRef) {
pdfKitMock._root.data.AcroForm = acroFormRef;
}
const {
form,
widget
Expand All @@ -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)]);
Expand Down
1 change: 1 addition & 0 deletions packages/placeholder-plain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 17 additions & 8 deletions packages/placeholder-plain/src/plainAddPlaceholder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-underscore-dangle */
import {
pdfkitAddPlaceholder,
PDFKitReferenceMock,
Expand All @@ -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;
};

/**
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -97,6 +101,11 @@ export const plainAddPlaceholder = ({
},
};

const acroFormRef = getAcroFormRef(info.root);
if (acroFormRef) {
pdfKitMock._root.data.AcroForm = acroFormRef;
}

const {
form,
widget,
Expand All @@ -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([
Expand Down
1 change: 1 addition & 0 deletions packages/placeholder-plain/src/plainAddPlaceholder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9a22a61

Please sign in to comment.