pdf_sign icon indicating copy to clipboard operation
pdf_sign copied to clipboard

date.getUTCFullYear is not a function

Open hermesalvesbr opened this issue 3 years ago • 9 comments

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?

hermesalvesbr avatar Mar 24 '22 22:03 hermesalvesbr

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.

RichardBray avatar Mar 25 '22 11:03 RichardBray

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.

hermesalvesbr avatar Mar 25 '22 11:03 hermesalvesbr

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.

RichardBray avatar Mar 25 '22 11:03 RichardBray

Thanks, i guess i need to change this line:

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

I sent by email my code.

Kind regards

hermesalvesbr avatar Mar 25 '22 12:03 hermesalvesbr

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

RichardBray avatar Mar 25 '22 14:03 RichardBray

I sent again, from [email protected]

hermesalvesbr avatar Mar 25 '22 15:03 hermesalvesbr

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?

RichardBray avatar Mar 25 '22 16:03 RichardBray

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

hermesalvesbr avatar Mar 25 '22 16:03 hermesalvesbr

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 🤷

RichardBray avatar Mar 25 '22 17:03 RichardBray