Skip to content

Commit

Permalink
Merge d213b4d into ab80f19
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuch committed Jan 30, 2020
2 parents ab80f19 + d213b4d commit 4510bc0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
Binary file added resources/including-a-link.pdf
Binary file not shown.
18 changes: 11 additions & 7 deletions src/helpers/plainAddPlaceholder/createBufferPageWithAnnotation.js
Expand Up @@ -3,21 +3,25 @@ import getIndexFromRef from './getIndexFromRef';

const createBufferPageWithAnnotation = (pdf, info, pagesRef, widget) => {
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 ]

const pagesDictionaryIndex = getIndexFromRef(info.xref, pagesRef);
const widgetValue = widget.toString();

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'),
]);
};
Expand Down
Expand Up @@ -8,17 +8,19 @@ jest.mock('./findObject', () => ({
}));

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();
// }
// });
});
14 changes: 14 additions & 0 deletions src/signpdf.test.js
Expand Up @@ -161,6 +161,20 @@ describe('Test signing', () => {
expect(typeof signature === 'string').toBe(true);
expect(signedData instanceof Buffer).toBe(true);
});
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 () => {
let pdfBuffer = await createPdf();
const p12Buffer = fs.readFileSync(`${__dirname}/../resources/bundle.p12`);
Expand Down

0 comments on commit 4510bc0

Please sign in to comment.