RichardBray / pdf_sign

🖋 Using node-signpdf and pdf-lib.js to sign a pdf
https://www.youtube.com/playlist?list=PLiKs97d-BatHWLh9MYrKwA9g6CD8RvAO3
39 stars 18 forks source link

date.getUTCFullYear is not a function #2

Open hermesalvesbr opened 2 years ago

hermesalvesbr commented 2 years ago

Thanks for your youtube channel!

warning ../../../package.json: No license field                                       
/home/hermes/Projetos/vue/pdf_sign/node_modules/pdf-lib/cjs/core/objects/PDFString.js:105
        var year = utils_1.padStart(String(date.getUTCFullYear()), 4, '0');
                                                ^

TypeError: date.getUTCFullYear is not a function
    at Function.PDFString.fromDate (/home/hermes/Projetos/vue/pdf_sign/node_modules/pdf-lib/cjs/core/objects/PDFString.js:105:49)
    at SignPDF._addPlaceholder (/home/hermes/Projetos/vue/pdf_sign/dist/SignPDF.js:58:28)
    at async SignPDF.signPDF (/home/hermes/Projetos/vue/pdf_sign/dist/SignPDF.js:29:18)
    at async main (/home/hermes/Projetos/vue/pdf_sign/dist/index.js:11:22)
error Command failed with exit code 1.    

I'm trying to run your code, but didn´t work. I have tried to update the dependencies too, and not working here.

Did you know why?

RichardBray commented 2 years ago

Hey, @hermesalvesbr that's totally my bad, I was using the wrong type of date.

Screenshot 2022-03-25 at 11 17 45

You can either update this file in your code or just pull the latest code from this repo.

Let me know if that worked for you.

hermesalvesbr commented 2 years ago

Perfect, it's solve this problem. Thank you.

Successfully compiled 3 files with Babel (221ms).
/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/asn1.js:357
    var error = new Error('Too few bytes to parse DER.');
                ^

Error: Too few bytes to parse DER.
    at _checkBufferLength (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/asn1.js:357:17)
    at _getValueLength (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/asn1.js:395:5)
    at _fromDer (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/asn1.js:498:16)
    at _fromDer (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/asn1.js:541:20)
    at Object.asn1.fromDer (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/asn1.js:458:15)
    at _decodeSafeContents (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/pkcs12.js:629:23)
    at _decodeAuthenticatedSafe (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/pkcs12.js:564:20)
    at Object.p12.pkcs12FromAsn1 (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-forge/lib/pkcs12.js:479:3)
    at SignPdf.sign (/home/hermes/Projetos/vue/pdf_sign/node_modules/node-signpdf/dist/signpdf.js:104:43)
    at SignPDF.signPDF (/home/hermes/Projetos/vue/pdf_sign/dist/SignPDF.js:30:35) {
  available: 2065,
  remaining: 95,
  requested: 105
}
error Command failed with exit code 1.

Now a error is about the byte place into pdf.

RichardBray commented 2 years ago

Hmmm, I haven't seen this issue before. I came across this answer after some Googling.

https://stackoverflow.com/a/26045255/2395062

Looks like it's related to node-forge. If you're not comfortable sharing your certificate and pdf you might have to get your hands dirty and try a few of these options in the source code.

Otherwise you can email me those resources and I can try it out myself.

hermesalvesbr commented 2 years ago

Thanks, i guess i need to change this line:

const DEFAULT_BYTE_RANGE_PLACEHOLDER = '**********';

I sent by email my code.

Kind regards

RichardBray commented 2 years ago

I haven't yet received an email. Could you send it again, please?

hermesalvesbr commented 2 years ago

I sent again, from hermes.alves@araripina.com.br

RichardBray commented 2 years ago

Thanks, I've got it now.

I've had a look at your files and I think it might have something to do with your certificate.

How did you create it? Or was it purchased?

hermesalvesbr commented 2 years ago

It's purchased, with this code works:

const signer = require('node-signpdf').default;
const fs = require('fs');
const { plainAddPlaceholder } = require('node-signpdf/dist/helpers');

const filename = '/home/hermes/Projetos/vue/signpdf/pdf/paraAssinar.pdf';
const cert = '/home/hermes/Projetos/vue/signpdf/pdf/softagon.pfx';

var output = plainAddPlaceholder({
    pdfBuffer: fs.readFileSync(filename),
    reason: 'Minha Assinatura',
    signatureLength: 8192
});

const signedPdf = signer.sign(output, fs.readFileSync(cert), { passphrase: '*37us82', asn1StrictParsing: true });
fs.writeFileSync(__dirname + '/last.pdf', signedPdf);
fs.writeFileSync(filename, signedPdf);
console.log(`Successfully signed ${filename}`);

I attached the signed last.pdf last.pdf

RichardBray commented 2 years ago

That's odd. I guess I could look into how plainAddPlaceholder works.

I've noticed your signature length is 8192 and my code has it as 3322. Maybe this is a limitation of pdf-lib.js 🤷