vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
732 stars 180 forks source link

Cannot set property 'Annots' of undefined #19

Closed mumapu closed 5 years ago

mumapu commented 5 years ago

`const signer = require('node-signpdf').default; const fs = require('fs'); const { addSignaturePlaceholder } = require('./node-signpdf-master/dist/helpers'); const PDFDocument = require('pdfkit');

const createPdf = (params = { placeholder: { reason : 'Digital signed by my key' }, text: 'this is content pdf', }) => new Promise((resolve) => { const pdf = new PDFDocument({ autoFirstPage: true, size: 'A4', layout: 'portrait', bufferPages: true, }); pdf.info.CreationDate = ''; pdf.fillColor('#333').fontSize(25).moveDown().text(params.text);

const pdfChunks = []; pdf.on('data', (data) => { pdfChunks.push(data); }); pdf.on('end', () => { resolve(Buffer.concat(pdfChunks)); });

const refs = addSignaturePlaceholder({ pdf, reason: 'I am the author', ...params.placeholder, });

Object.keys(refs).forEach(key => refs[key].end()); pdf.end();

});

const action = async () => { let pdfBuffer = await createPdf(); let p12Buffer = fs.readFileSync(__dirname + '/baominh.key.p12')

let pdf = signer.sign(pdfBuffer, p12Buffer, { passphrase : '12345678', asn1StrictParsing : true }); fs.writeFileSync(__dirname + '/demo.pdf', pdf); }

action();` Hello! I have been the error: 'Cannot set property 'Annots' of undefined' when i runed the code above. Can you help me with this?

vbuch commented 5 years ago

I'll try to have a look a bit later. Could you make sure your snippet is well formatted so I can just copy and paste it?

vbuch commented 5 years ago

I think this error would be caused by a Page not being created for some reason (without having lloked at your code in detail)

mumapu commented 5 years ago

I think the cause is about the version of pdfkit. First i used the version 0.9. So I used the version 0.8.3 and now the code works. I think the cause is pdfkit version. Can you test this?

vbuch commented 5 years ago

You May be right as it was release 24 days ago. Will check and let you know. I see we use "pdfkit": "^0.8.3" so really sounds like you are right.

vbuch commented 5 years ago

Just ran the tests with pdfkit 0.9.0 and I am indeed getting the same error as you originally reported. A page used to be an object with a data prop where we were finding the Annots member. It is now a Buffer. Currently the only option I see is documenting that limitation. But I want to have a real solution for pdfkit 0.9.0 users so I'll think about it and check with the other contributors as well for some ideas.

vbuch commented 5 years ago

Here is what I did to make it work with 0.9.0: https://github.com/vbuch/node-signpdf/commit/3755311b32592a165c0a9c3ed7c7cfeac76e8ff4

Some kind of fix will surely be needed for this and it should go with a next release.

Separate note: Another thing that needs to happen in that release is fix babel ->... braces vulnerabilities reported by npm and/or yarn. And... while looking at the npm audit I saw that pdfkit also has a vulnerable dep that will need to get upgraded. So... we have some time until the pdfkit team resolves their vulnerability. Once they do, we'll also need to have a ready fix for the 0.9 issue we're discussing here.

Another separate note: There should be a nicer way to add annotations in a page. I'm basing this assumption on this code: https://github.com/foliojs/pdfkit/blob/5d527a32c87955f636b4220ad983dec208b4ffeb/lib/mixins/annotations.js So hopefully this can be improved as well for the next release. Should be something like pdfDocument.annotate({...annotation configuration})

mumapu commented 5 years ago

That's true! Let's wait for the next release to use the new version of pdfkit. About the new form of insert annotation, it's will be more easy to understand the code. I think this is a good idea. Thanks for the explanation! Best regards from Brazil!

vbuch commented 5 years ago

This has rolled out into 1.0.0