-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add pdfkit v0.11+ placeholder library
- Loading branch information
Showing
25 changed files
with
905 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../babel.config.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": [ | ||
"@signpdf/eslint-config" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Placehodler providing helper using PDFKit 0.11+ | ||
|
||
for [![@signpdf](https://raw.githubusercontent.com/vbuch/node-signpdf/master/resources/logo-horizontal.svg?sanitize=true)](https://github.com/vbuch/node-signpdf/) | ||
|
||
[![npm version](https://badge.fury.io/js/@signpdf%2Fplaceholder-pdfkit010.svg)](https://badge.fury.io/js/@signpdf%2Fplaceholder-pdfkit010) | ||
[![Donate to this project using Buy Me A Coffee](https://img.shields.io/badge/buy%20me%20a%20coffee-donate-yellow.svg)](https://buymeacoffee.com/vbuch) | ||
|
||
Works on top of `PDFKit 0.11+` and given a PDFDocument that is in the works, adds an e-signature placeholder. When the PDF is ready you can pass it to `@signpdf/signpdf` to complete the process. | ||
|
||
## Usage | ||
|
||
You will need `$ npm i -S @signpdf/signpdf @signpdf/placeholder-pdfkit node-forge`. This works in an identical way to the pdfkit010 package and the [pdfkit010.js example](/packages/examples/pdfkit010.js) is still relevant. | ||
|
||
## Notes | ||
|
||
* Make sure to have a look at the docs of the [@signpdf family of packages](https://github.com/vbuch/node-signpdf/). | ||
* Feel free to copy and paste any part of this code. See its defined [Purpose](https://github.com/vbuch/node-signpdf#purpose). | ||
|
||
### Signature length | ||
|
||
Signing in detached mode makes the signature length independent of the PDF's content length, but it may still vary between different signing certificates. So every time you sign using the same P12 you will get the same length of the output signature, no matter the length of the signed content. It is safe to find out the actual signature length your certificate produces and use it to properly configure the placeholder length. | ||
|
||
### PAdES compliant signatures | ||
|
||
To produce PAdES compliant signatures, the ETSI Signature Dictionary SubFilter value must be `ETSI.CAdES.detached` instead of the standard Adobe value. | ||
|
||
This can be declared using the subFilter option argument passed to `pdfkitAddPlaceholder` and `plainAddPlaceholder`. | ||
|
||
```js | ||
import { pdfkitAddPlaceholder } from '@signpdf/placeholder-pdfkit'; | ||
import { SUBFILTER_ETSI_CADES_DETACHED } from '@signpdf/utils'; | ||
|
||
const pdfToSign = pdfkitAddPlaceholder({ | ||
..., | ||
subFilter: SUBFILTER_ETSI_CADES_DETACHED, | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./pdfkitAddPlaceholder"; | ||
export { default as PDFObject } from "./pdfkit/pdfobject"; | ||
//# sourceMappingURL=index.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _exportNames = { | ||
PDFObject: true | ||
}; | ||
Object.defineProperty(exports, "PDFObject", { | ||
enumerable: true, | ||
get: function () { | ||
return _pdfobject.default; | ||
} | ||
}); | ||
var _pdfkitAddPlaceholder = require("./pdfkitAddPlaceholder"); | ||
Object.keys(_pdfkitAddPlaceholder).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _pdfkitAddPlaceholder[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _pdfkitAddPlaceholder[key]; | ||
} | ||
}); | ||
}); | ||
var _pdfobject = _interopRequireDefault(require("./pdfkit/pdfobject")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
6 changes: 6 additions & 0 deletions
6
packages/placeholder-pdfkit/dist/pdfkit/abstract_reference.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export default PDFAbstractReference; | ||
declare class PDFAbstractReference { | ||
toString(): void; | ||
end(): void; | ||
} | ||
//# sourceMappingURL=abstract_reference.d.ts.map |
1 change: 1 addition & 0 deletions
1
packages/placeholder-pdfkit/dist/pdfkit/abstract_reference.d.ts.map
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
packages/placeholder-pdfkit/dist/pdfkit/abstract_reference.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
/* | ||
PDFAbstractReference by Devon Govett used below. | ||
The class is part of pdfkit. See https://github.com/foliojs/pdfkit | ||
LICENSE: MIT. Included in this folder. | ||
Modifications may have been applied for the purposes of node-signpdf. | ||
*/ | ||
|
||
/* | ||
PDFAbstractReference - abstract class for PDF reference | ||
*/ | ||
|
||
class PDFAbstractReference { | ||
toString() { | ||
throw new Error('Must be implemented by subclasses'); | ||
} | ||
end() { | ||
// noop | ||
} | ||
} | ||
var _default = exports.default = PDFAbstractReference; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export default class PDFObject { | ||
static convert(object: any, encryptFn?: any): any; | ||
static number(n: any): number; | ||
} | ||
//# sourceMappingURL=pdfobject.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
"use strict"; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _abstract_reference = _interopRequireDefault(require("./abstract_reference")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/* | ||
PDFObject by Devon Govett used below. | ||
The class is part of pdfkit. See https://github.com/foliojs/pdfkit | ||
LICENSE: MIT. Included in this folder. | ||
Modifications may have been applied for the purposes of node-signpdf. | ||
*/ | ||
|
||
/* | ||
PDFObject - converts JavaScript types into their corresponding PDF types. | ||
By Devon Govett | ||
*/ | ||
|
||
const pad = (str, length) => (Array(length + 1).join('0') + str).slice(-length); | ||
const escapableRe = /[\n\r\t\b\f()\\]/g; | ||
const escapable = { | ||
'\n': '\\n', | ||
'\r': '\\r', | ||
'\t': '\\t', | ||
'\b': '\\b', | ||
'\f': '\\f', | ||
'\\': '\\\\', | ||
'(': '\\(', | ||
')': '\\)' | ||
}; | ||
|
||
// Convert little endian UTF-16 to big endian | ||
const swapBytes = buff => buff.swap16(); | ||
class PDFObject { | ||
static convert(object, encryptFn = null) { | ||
// String literals are converted to the PDF name type | ||
if (typeof object === 'string') { | ||
return `/${object}`; | ||
|
||
// String objects are converted to PDF strings (UTF-16) | ||
} | ||
if (object instanceof String) { | ||
let string = object; | ||
// Detect if this is a unicode string | ||
let isUnicode = false; | ||
for (let i = 0, end = string.length; i < end; i += 1) { | ||
if (string.charCodeAt(i) > 0x7f) { | ||
isUnicode = true; | ||
break; | ||
} | ||
} | ||
|
||
// If so, encode it as big endian UTF-16 | ||
let stringBuffer; | ||
if (isUnicode) { | ||
stringBuffer = swapBytes(Buffer.from(`\ufeff${string}`, 'utf16le')); | ||
} else { | ||
stringBuffer = Buffer.from(string, 'ascii'); | ||
} | ||
|
||
// Encrypt the string when necessary | ||
if (encryptFn) { | ||
string = encryptFn(stringBuffer).toString('binary'); | ||
} else { | ||
string = stringBuffer.toString('binary'); | ||
} | ||
|
||
// Escape characters as required by the spec | ||
string = string.replace(escapableRe, c => escapable[c]); | ||
return `(${string})`; | ||
|
||
// Buffers are converted to PDF hex strings | ||
} | ||
if (Buffer.isBuffer(object)) { | ||
return `<${object.toString('hex')}>`; | ||
} | ||
if (object instanceof _abstract_reference.default) { | ||
return object.toString(); | ||
} | ||
if (object instanceof Date) { | ||
let string = `D:${pad(object.getUTCFullYear(), 4)}${pad(object.getUTCMonth() + 1, 2)}${pad(object.getUTCDate(), 2)}${pad(object.getUTCHours(), 2)}${pad(object.getUTCMinutes(), 2)}${pad(object.getUTCSeconds(), 2)}Z`; | ||
|
||
// Encrypt the string when necessary | ||
if (encryptFn) { | ||
string = encryptFn(Buffer.from(string, 'ascii')).toString('binary'); | ||
|
||
// Escape characters as required by the spec | ||
string = string.replace(escapableRe, c => escapable[c]); | ||
} | ||
return `(${string})`; | ||
} | ||
if (Array.isArray(object)) { | ||
const items = object.map(e => PDFObject.convert(e, encryptFn)).join(' '); | ||
return `[${items}]`; | ||
} | ||
if ({}.toString.call(object) === '[object Object]') { | ||
const out = ['<<']; | ||
let streamData; | ||
|
||
// @todo this can probably be refactored into a reduce | ||
Object.entries(object).forEach(([key, val]) => { | ||
let checkedValue = ''; | ||
if (val.toString().indexOf('<<') !== -1) { | ||
checkedValue = val; | ||
} else { | ||
checkedValue = PDFObject.convert(val, encryptFn); | ||
} | ||
if (key === 'stream') { | ||
streamData = `${key}\n${val}\nendstream`; | ||
} else { | ||
out.push(`/${key} ${checkedValue}`); | ||
} | ||
}); | ||
out.push('>>'); | ||
if (streamData) { | ||
out.push(streamData); | ||
} | ||
return out.join('\n'); | ||
} | ||
if (typeof object === 'number') { | ||
return PDFObject.number(object); | ||
} | ||
return `${object}`; | ||
} | ||
static number(n) { | ||
if (n > -1e21 && n < 1e21) { | ||
return Math.round(n * 1e6) / 1e6; | ||
} | ||
throw new Error(`unsupported number: ${n}`); | ||
} | ||
} | ||
exports.default = PDFObject; |
28 changes: 28 additions & 0 deletions
28
packages/placeholder-pdfkit/dist/pdfkitAddPlaceholder.d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export function pdfkitAddPlaceholder({ pdf, reason, contactInfo, name, location, signatureLength, byteRangePlaceholder, subFilter, widgetRect, }: InputType): ReturnType; | ||
export type InputType = { | ||
/** | ||
* PDFDocument | ||
*/ | ||
pdf: object; | ||
pdfBuffer: Buffer; | ||
reason: string; | ||
contactInfo: string; | ||
name: string; | ||
location: string; | ||
signatureLength?: number; | ||
byteRangePlaceholder?: string; | ||
/** | ||
* One of SUBFILTER_* from \@signpdf/utils | ||
*/ | ||
subFilter?: string; | ||
/** | ||
* [x1, y1, x2, y2] widget rectangle | ||
*/ | ||
widgetRect?: number[]; | ||
}; | ||
export type ReturnType = { | ||
signature: any; | ||
form: any; | ||
widget: any; | ||
}; | ||
//# sourceMappingURL=pdfkitAddPlaceholder.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.