node-signpdf
Advanced tools
Comparing version
# CHANGELOG | ||
## [1.2.2] | ||
* Fixed an issue in plainAddPlaceholder that used to fail when the source PDF already contained /Annots. | ||
## [1.2.1] | ||
@@ -4,0 +8,0 @@ |
@@ -20,1 +20,2 @@ # Contributing | ||
* [therpobinski](https://github.com/therpobinski) | ||
* [Godoy](https://github.com/Godoy) |
@@ -17,9 +17,14 @@ "use strict"; | ||
const splittedDictionary = pagesDictionary.split('/Annots')[0]; | ||
let splittedIds = pagesDictionary.split('/Annots')[1]; // eslint-disable-next-line no-useless-escape | ||
const annotsStart = pagesDictionary.indexOf('/Annots'); | ||
const annotsEnd = pagesDictionary.indexOf(']', annotsStart); | ||
let annots = pagesDictionary.substr(annotsStart, annotsEnd - annotsStart + 1); | ||
annots = annots.substr(0, annots.length - 1); // remove the trailing ] | ||
splittedIds = splittedIds === undefined ? '' : splittedIds.replace(/[\[\]]/g, ''); | ||
const pagesDictionaryIndex = (0, _getIndexFromRef.default)(info.xref, pagesRef); | ||
const widgetValue = widget.toString(); | ||
return Buffer.concat([Buffer.from(`${pagesDictionaryIndex} 0 obj\n`), Buffer.from('<<\n'), Buffer.from(`${splittedDictionary}\n`), Buffer.from(`/Annots [${splittedIds} ${widgetValue}]`), Buffer.from('\n>>\nendobj\n')]); | ||
annots = annots + ' ' + widgetValue + ']'; // add the trailing ] back | ||
const preAnnots = pagesDictionary.substr(0, annotsStart); | ||
const postAnnots = pagesDictionary.substr(annotsEnd + 1); | ||
return Buffer.concat([Buffer.from(`${pagesDictionaryIndex} 0 obj\n`), Buffer.from('<<\n'), Buffer.from(`${preAnnots + annots + postAnnots}\n`), Buffer.from('\n>>\nendobj\n')]); | ||
}; | ||
@@ -26,0 +31,0 @@ |
{ | ||
"name": "node-signpdf", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Simple signing of PDFs in node.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -6,8 +6,8 @@ import findObject from './findObject'; | ||
const pagesDictionary = findObject(pdf, info.xref, pagesRef).toString(); | ||
// Extend page dictionary with newly created annotations | ||
const splittedDictionary = pagesDictionary.split('/Annots')[0]; | ||
let splittedIds = pagesDictionary.split('/Annots')[1]; | ||
// eslint-disable-next-line no-useless-escape | ||
splittedIds = splittedIds === undefined ? '' : splittedIds.replace(/[\[\]]/g, ''); | ||
const annotsStart = pagesDictionary.indexOf('/Annots'); | ||
const annotsEnd = pagesDictionary.indexOf(']', annotsStart); | ||
let annots = pagesDictionary.substr(annotsStart, annotsEnd - annotsStart + 1); | ||
annots = annots.substr(0, annots.length - 1); // remove the trailing ] | ||
@@ -17,7 +17,11 @@ const pagesDictionaryIndex = getIndexFromRef(info.xref, pagesRef); | ||
annots = annots + ' ' + widgetValue + ']'; // add the trailing ] back | ||
const preAnnots = pagesDictionary.substr(0, annotsStart); | ||
const postAnnots = pagesDictionary.substr(annotsEnd + 1); | ||
return Buffer.concat([ | ||
Buffer.from(`${pagesDictionaryIndex} 0 obj\n`), | ||
Buffer.from('<<\n'), | ||
Buffer.from(`${splittedDictionary}\n`), | ||
Buffer.from(`/Annots [${splittedIds} ${widgetValue}]`), | ||
Buffer.from(`${preAnnots + annots + postAnnots}\n`), | ||
Buffer.from('\n>>\nendobj\n'), | ||
@@ -24,0 +28,0 @@ ]); |
@@ -11,15 +11,17 @@ import createBufferPageWithAnnotation from './createBufferPageWithAnnotation'; | ||
describe('createBufferPageWithAnnotation', () => { | ||
it('should be tested', () => { | ||
it('Adds annotation to an existing array', () => { | ||
findObject.mockImplementation(() => ( | ||
'/Annots [1 0 R]\n/Something [ELSE HERE]' | ||
)); | ||
const info = {xref: {}}; | ||
info.xref.offsets = new Map(); | ||
info.xref.offsets.set(1, 1); | ||
const buffer = createBufferPageWithAnnotation( | ||
'pdf', | ||
info, | ||
'1 0 R', | ||
'2 0 R', | ||
); | ||
expect(buffer.toString().indexOf('/Annots [1 0 R 2 0 R]')).not.toBe(-1); | ||
}); | ||
// it('Reports unsupported feature', () => { | ||
// findObject.mockImplementation(() => '/Annots Exists'); | ||
// try { | ||
// createBufferPageWithAnnotation('pdf', 'info', 'pageRef', 'widget'); | ||
// expect('here').not.toBe('here'); | ||
// } catch (e) { | ||
// expect(e instanceof SignPdfError).toBe(true); | ||
// expect(e.type).toBe(SignPdfError.TYPE_PARSE); | ||
// expect(e.message).toMatchSnapshot(); | ||
// } | ||
// }); | ||
}); |
@@ -164,2 +164,16 @@ import PDFDocument from 'pdfkit'; | ||
}); | ||
it('signs a ready pdf containing a link', async () => { | ||
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/certificate.p12`); | ||
let pdfBuffer = fs.readFileSync(`${__dirname}/../resources/including-a-link.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 with ca, intermediate and multiple certificates bundle', async () => { | ||
@@ -166,0 +180,0 @@ let pdfBuffer = await createPdf(); |
Sorry, the diff of this file is not supported yet
625762
3.45%87
2.35%2359
0.9%