@signpdf/placeholder-pdf-lib
Advanced tools
Comparing version
@@ -1,5 +0,6 @@ | ||
export function pdflibAddPlaceholder({ pdfDoc, reason, contactInfo, name, location, signatureLength, byteRangePlaceholder, subFilter, widgetRect, }: InputType): void; | ||
export function pdflibAddPlaceholder({ pdfDoc, pdfPage, reason, contactInfo, name, location, signingTime, signatureLength, byteRangePlaceholder, subFilter, widgetRect, appName, }: InputType): void; | ||
export type PDFDocument = import('pdf-lib').PDFDocument; | ||
export type InputType = { | ||
pdfDoc: PDFDocument; | ||
pdfPage: PDFPage; | ||
reason: string; | ||
@@ -9,2 +10,3 @@ contactInfo: string; | ||
location: string; | ||
signingTime?: Date; | ||
signatureLength?: number; | ||
@@ -20,3 +22,7 @@ byteRangePlaceholder?: string; | ||
widgetRect?: number[]; | ||
/** | ||
* Name of the application generating the signature | ||
*/ | ||
appName?: string; | ||
}; | ||
//# sourceMappingURL=pdflibAddPlaceholder.d.ts.map |
@@ -16,2 +16,3 @@ "use strict"; | ||
* @property {PDFDocument} pdfDoc | ||
* @property {PDFPage} pdfPage | ||
* @property {string} reason | ||
@@ -21,2 +22,3 @@ * @property {string} contactInfo | ||
* @property {string} location | ||
* @property {Date} [signingTime] | ||
* @property {number} [signatureLength] | ||
@@ -26,2 +28,3 @@ * @property {string} [byteRangePlaceholder] | ||
* @property {number[]} [widgetRect] [x1, y1, x2, y2] widget rectangle | ||
* @property {string} [appName] Name of the application generating the signature | ||
*/ | ||
@@ -38,3 +41,4 @@ | ||
const pdflibAddPlaceholder = ({ | ||
pdfDoc, | ||
pdfDoc = undefined, | ||
pdfPage = undefined, | ||
reason, | ||
@@ -44,12 +48,18 @@ contactInfo, | ||
location, | ||
signingTime = undefined, | ||
signatureLength = _utils.DEFAULT_SIGNATURE_LENGTH, | ||
byteRangePlaceholder = _utils.DEFAULT_BYTE_RANGE_PLACEHOLDER, | ||
subFilter = _utils.SUBFILTER_ADOBE_PKCS7_DETACHED, | ||
widgetRect = [0, 0, 0, 0] | ||
widgetRect = [0, 0, 0, 0], | ||
appName = undefined | ||
}) => { | ||
const page = pdfDoc.getPage(0); | ||
if (pdfDoc === undefined && pdfPage === undefined) { | ||
throw new _utils.SignPdfError('PDFDoc or PDFPage must be set.', _utils.SignPdfError.TYPE_INPUT); | ||
} | ||
const doc = pdfDoc !== null && pdfDoc !== void 0 ? pdfDoc : pdfPage.doc; | ||
const page = pdfPage !== null && pdfPage !== void 0 ? pdfPage : doc.getPages()[0]; | ||
// Create a placeholder where the the last 3 parameters of the | ||
// actual range will be replaced when signing is done. | ||
const byteRange = _pdfLib.PDFArray.withContext(pdfDoc.context); | ||
const byteRange = _pdfLib.PDFArray.withContext(doc.context); | ||
byteRange.push(_pdfLib.PDFNumber.of(0)); | ||
@@ -64,3 +74,8 @@ byteRange.push(_pdfLib.PDFName.of(byteRangePlaceholder)); | ||
// Create a signature dictionary to be referenced in the signature widget. | ||
const signatureDict = pdfDoc.context.obj({ | ||
const appBuild = appName ? { | ||
App: { | ||
Name: appName | ||
} | ||
} : {}; | ||
const signatureDict = doc.context.obj({ | ||
Type: 'Sig', | ||
@@ -72,13 +87,29 @@ Filter: 'Adobe.PPKLite', | ||
Reason: _pdfLib.PDFString.of(reason), | ||
M: _pdfLib.PDFString.fromDate(new Date()), | ||
M: _pdfLib.PDFString.fromDate(signingTime !== null && signingTime !== void 0 ? signingTime : new Date()), | ||
ContactInfo: _pdfLib.PDFString.of(contactInfo), | ||
Name: _pdfLib.PDFString.of(name), | ||
Location: _pdfLib.PDFString.of(location) | ||
}, pdfDoc.index); | ||
const signatureDictRef = pdfDoc.context.register(signatureDict); | ||
Location: _pdfLib.PDFString.of(location), | ||
Prop_Build: { | ||
Filter: { | ||
Name: 'Adobe.PPKLite' | ||
}, | ||
...appBuild | ||
} | ||
}); | ||
// Register signatureDict as a PDFInvalidObject to prevent PDFLib from serializing it | ||
// in an object stream. | ||
const signatureBuffer = new Uint8Array(signatureDict.sizeInBytes()); | ||
signatureDict.copyBytesInto(signatureBuffer, 0); | ||
const signatureObj = _pdfLib.PDFInvalidObject.of(signatureBuffer); | ||
const signatureDictRef = doc.context.register(signatureObj); | ||
// Create the signature widget | ||
const rect = _pdfLib.PDFArray.withContext(pdfDoc.context); | ||
const rect = _pdfLib.PDFArray.withContext(doc.context); | ||
widgetRect.forEach(c => rect.push(_pdfLib.PDFNumber.of(c))); | ||
const widgetDict = pdfDoc.context.obj({ | ||
const apStream = doc.context.formXObject([], { | ||
BBox: widgetRect, | ||
Resources: {} // Necessary to avoid Acrobat bug (see https://stackoverflow.com/a/73011571) | ||
}); | ||
const widgetDict = doc.context.obj({ | ||
Type: 'Annot', | ||
@@ -91,10 +122,14 @@ Subtype: 'Widget', | ||
F: _utils.ANNOTATION_FLAGS.PRINT, | ||
P: page.ref | ||
}, pdfDoc.index); | ||
const widgetDictRef = pdfDoc.context.register(widgetDict); | ||
P: page.ref, | ||
AP: { | ||
N: doc.context.register(apStream) | ||
} // Required for PDF/A compliance | ||
}); | ||
// Annotate the widget on the first page | ||
const widgetDictRef = doc.context.register(widgetDict); | ||
// Annotate the widget on the given page | ||
let annotations = page.node.lookupMaybe(_pdfLib.PDFName.of('Annots'), _pdfLib.PDFArray); | ||
if (typeof annotations === 'undefined') { | ||
annotations = pdfDoc.context.obj([]); | ||
annotations = doc.context.obj([]); | ||
} | ||
@@ -105,10 +140,10 @@ annotations.push(widgetDictRef); | ||
// Add an AcroForm or update the existing one | ||
let acroForm = pdfDoc.catalog.lookupMaybe(_pdfLib.PDFName.of('AcroForm'), _pdfLib.PDFDict); | ||
let acroForm = doc.catalog.lookupMaybe(_pdfLib.PDFName.of('AcroForm'), _pdfLib.PDFDict); | ||
if (typeof acroForm === 'undefined') { | ||
// Need to create a new AcroForm | ||
acroForm = pdfDoc.context.obj({ | ||
acroForm = doc.context.obj({ | ||
Fields: [] | ||
}); | ||
const acroFormRef = pdfDoc.context.register(acroForm); | ||
pdfDoc.catalog.set(_pdfLib.PDFName.of('AcroForm'), acroFormRef); | ||
const acroFormRef = doc.context.register(acroForm); | ||
doc.catalog.set(_pdfLib.PDFName.of('AcroForm'), acroFormRef); | ||
} | ||
@@ -115,0 +150,0 @@ |
{ | ||
"name": "@signpdf/placeholder-pdf-lib", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Use PDF-LIB to insert a signature placeholder.", | ||
@@ -41,3 +41,3 @@ "repository": { | ||
"dependencies": { | ||
"@signpdf/utils": "^3.1.0" | ||
"@signpdf/utils": "^3.2.0" | ||
}, | ||
@@ -55,3 +55,3 @@ "peerDependencies": { | ||
"@signpdf/eslint-config": "^3.1.0", | ||
"@signpdf/internal-utils": "^3.0.0", | ||
"@signpdf/internal-utils": "^3.2.0", | ||
"@types/node": ">=12.0.0", | ||
@@ -74,3 +74,3 @@ "@types/node-forge": "^1.2.1", | ||
}, | ||
"gitHead": "56f621c5b6b240f363927cdab47ec8e0bb9fa180" | ||
"gitHead": "17a9e6ba4133e75af2916340683d7b4189ad47f0" | ||
} |
@@ -1,2 +0,2 @@ | ||
# Placehodler providing helper using PDF-LIB | ||
# Helper that provides placeholder using PDF-LIB | ||
@@ -3,0 +3,0 @@ for [](https://github.com/vbuch/node-signpdf/) |
Sorry, the diff of this file is not supported yet
11789
27.3%6
20%170
29.77%Updated