node-signpdf
Advanced tools
Comparing version
# CHANGELOG | ||
## [1.3.3] | ||
* plainAddPlaceholder: Fixed loss of PDF metadata when adding placeholder; | ||
* Export helpers from root; | ||
* Bumped dependencies; | ||
## [1.3.2] | ||
@@ -4,0 +10,0 @@ |
@@ -17,3 +17,3 @@ "use strict"; | ||
rows = rows.filter(row => row !== undefined); | ||
return Buffer.concat([Buffer.from('xref\n'), Buffer.from(`${info.xref.startingIndex} 1\n`), Buffer.from(rows.join('\n')), Buffer.from('\ntrailer\n'), Buffer.from('<<\n'), Buffer.from(`/Size ${info.xref.maxIndex + 1}\n`), Buffer.from(`/Prev ${info.xRefPosition}\n`), Buffer.from(`/Root ${info.rootRef}\n`), Buffer.from('/Info 15 0 R\n'), Buffer.from('>>\n'), Buffer.from('startxref\n'), Buffer.from(`${pdf.length}\n`), Buffer.from('%%EOF')]); | ||
return Buffer.concat([Buffer.from('xref\n'), Buffer.from(`${info.xref.startingIndex} 1\n`), Buffer.from(rows.join('\n')), Buffer.from('\ntrailer\n'), Buffer.from('<<\n'), Buffer.from(`/Size ${info.xref.maxIndex + 1}\n`), Buffer.from(`/Root ${info.rootRef}\n`), Buffer.from(info.infoRef ? `/Info ${info.infoRef}\n` : ''), Buffer.from(`/Prev ${info.xRefPosition}\n`), Buffer.from('>>\n'), Buffer.from('startxref\n'), Buffer.from(`${pdf.length}\n`), Buffer.from('%%EOF')]); | ||
}; | ||
@@ -20,0 +20,0 @@ |
@@ -14,2 +14,18 @@ "use strict"; | ||
const getValue = (trailer, key) => { | ||
let index = trailer.indexOf(key); | ||
if (index === -1) { | ||
return undefined; | ||
} | ||
const slice = trailer.slice(index); | ||
index = slice.indexOf('/', 1); | ||
if (index === -1) { | ||
index = slice.indexOf('>', 1); | ||
} | ||
return slice.slice(key.length + 1, index).toString().trim(); // key + at least one space | ||
}; | ||
/** | ||
@@ -23,2 +39,4 @@ * Simplified parsing of a PDF Buffer. | ||
*/ | ||
const readPdf = pdfBuffer => { | ||
@@ -32,13 +50,5 @@ // Extract the trailer dictionary. | ||
const refTable = (0, _readRefTable.default)(pdfBuffer); | ||
let rootSlice = trailer.slice(trailer.indexOf('/Root')); | ||
let rootIndex = rootSlice.indexOf('/', 1); | ||
if (rootIndex === -1) { | ||
rootIndex = rootSlice.indexOf('>', 1); | ||
} | ||
rootSlice = rootSlice.slice(0, rootIndex); | ||
const rootRef = rootSlice.slice(6).toString().trim(); // /Root + at least one space | ||
const rootRef = getValue(trailer, '/Root'); | ||
const root = (0, _findObject.default)(pdfBuffer, refTable, rootRef).toString(); | ||
const infoRef = getValue(trailer, '/Info'); | ||
return { | ||
@@ -48,2 +58,3 @@ xref: refTable, | ||
root, | ||
infoRef, | ||
trailerStart, | ||
@@ -50,0 +61,0 @@ previousXrefs: [], |
@@ -6,2 +6,7 @@ "use strict"; | ||
}); | ||
var _exportNames = { | ||
DEFAULT_BYTE_RANGE_PLACEHOLDER: true, | ||
SignPdf: true, | ||
SignPdfError: true | ||
}; | ||
Object.defineProperty(exports, "SignPdfError", { | ||
@@ -21,2 +26,13 @@ enumerable: true, | ||
Object.keys(_helpers).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _helpers[key]; | ||
} | ||
}); | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -23,0 +39,0 @@ |
{ | ||
"name": "node-signpdf", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Simple signing of PDFs in node.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -50,3 +50,3 @@ #  | ||
const signedPdf = signer.sign( | ||
fs.readFileSync(PATH_TO_PDF_FILE) | ||
fs.readFileSync(PATH_TO_PDF_FILE), | ||
fs.readFileSync(PATH_TO_P12_CERTIFICATE), | ||
@@ -53,0 +53,0 @@ ); |
@@ -18,5 +18,5 @@ const createBufferTrailer = (pdf, info, addedReferences) => { | ||
Buffer.from(`/Size ${info.xref.maxIndex + 1}\n`), | ||
Buffer.from(`/Root ${info.rootRef}\n`), | ||
Buffer.from(info.infoRef ? `/Info ${info.infoRef}\n` : ''), | ||
Buffer.from(`/Prev ${info.xRefPosition}\n`), | ||
Buffer.from(`/Root ${info.rootRef}\n`), | ||
Buffer.from('/Info 15 0 R\n'), | ||
Buffer.from('>>\n'), | ||
@@ -23,0 +23,0 @@ Buffer.from('startxref\n'), |
import readRefTable from './readRefTable'; | ||
import findObject from './findObject'; | ||
const getValue = (trailer, key) => { | ||
let index = trailer.indexOf(key); | ||
if (index === -1) { | ||
return undefined; | ||
} | ||
const slice = trailer.slice(index); | ||
index = slice.indexOf('/', 1); | ||
if (index === -1) { | ||
index = slice.indexOf('>', 1); | ||
} | ||
return slice.slice(key.length + 1, index).toString().trim(); // key + at least one space | ||
}; | ||
/** | ||
@@ -23,11 +38,7 @@ * Simplified parsing of a PDF Buffer. | ||
let rootSlice = trailer.slice(trailer.indexOf('/Root')); | ||
let rootIndex = rootSlice.indexOf('/', 1); | ||
if (rootIndex === -1) { | ||
rootIndex = rootSlice.indexOf('>', 1); | ||
} | ||
rootSlice = rootSlice.slice(0, rootIndex); | ||
const rootRef = rootSlice.slice(6).toString().trim(); // /Root + at least one space | ||
const rootRef = getValue(trailer, '/Root'); | ||
const root = findObject(pdfBuffer, refTable, rootRef).toString(); | ||
const infoRef = getValue(trailer, '/Info'); | ||
return { | ||
@@ -37,2 +48,3 @@ xref: refTable, | ||
root, | ||
infoRef, | ||
trailerStart, | ||
@@ -39,0 +51,0 @@ previousXrefs: [], |
@@ -6,2 +6,3 @@ import forge from 'node-forge'; | ||
export {default as SignPdfError} from './SignPdfError'; | ||
export * from './helpers'; | ||
@@ -8,0 +9,0 @@ export const DEFAULT_BYTE_RANGE_PLACEHOLDER = '**********'; |
@@ -175,2 +175,17 @@ import PDFDocument from 'pdfkit'; | ||
}); | ||
it('signs a ready pdf that does not have metadata', async () => { | ||
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`); | ||
let pdfBuffer = fs.readFileSync(`${__dirname}/../resources/no-metadata.pdf`); | ||
pdfBuffer = plainAddPlaceholder({ | ||
pdfBuffer, | ||
reason: 'I have reviewed it.', | ||
signatureLength: 1612, | ||
}); | ||
pdfBuffer = signer.sign(pdfBuffer, p12Buffer); | ||
const {signature, signedData} = extractSignature(pdfBuffer); | ||
expect(typeof signature === 'string').toBe(true); | ||
expect(signedData instanceof Buffer).toBe(true); | ||
}); | ||
it('signs a ready pdf two times', async () => { | ||
@@ -177,0 +192,0 @@ const secondP12Buffer = fs.readFileSync(`${__dirname}/../resources/withpass.p12`); |
543392
2.86%153
0.66%4500
1.01%