vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
725 stars 178 forks source link

"Cannot read properties of undefined (reading 'fonts')" #265

Closed PedroNogueir4 closed 2 months ago

PedroNogueir4 commented 2 months ago

I'm following the documentation to generate a PDF with signature in real time, but it's generating this error in the title, I checked the library and the error is generated in the endAcroForm() function, but I couldn't identify where I have to indicate this "fonts" property

const pdf = new PDFDocument({ autoFirstPage: false, size: 'A4', layout: 'portrait', bufferPages: true, }); pdf.info.CreationDate = new Date();

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

  pdf.addPage().fillColor('#333').fontSize(25).moveDown().text('@signpdf').save();

  const refs = pdfkitAddPlaceholder({
    pdf,
    pdfBuffer: Buffer.from([pdf]),
    reason: 'Showing off.',
    fonts: 'Courier',
    contactInfo: 'signpdf@example.com',
    name: 'SignPDF',
    location: 'The digital world.',
  });

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

  const certificatePath = path.join(__dirname, 'tempTeste', 'certificado.p12');
  const certificateBuffer = fs.readFileSync(certificatePath);
  const signer = new P12Signer(certificateBuffer);

  pdfReady
    .then((pdfWithPlaceholder) => {
      console.log('opa');
      return signPdf.sign(pdfWithPlaceholder, signer);
    })
    .then((signedPdf) => {
      const targetPath = path.join(__dirname, 'tempTeste', 'success', 'invoice.pdf');
      fs.writeFileSync(targetPath, signedPdf);
    });

  pdf.end();
PedroNogueir4 commented 2 months ago

The error was due to the version of pdfkit, it was necessary to use 0.10