vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
688 stars 175 forks source link

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fonts' of undefined #133

Closed uclnj closed 2 years ago

uclnj commented 3 years ago

Testing code using Jest examples, fails on the mock up createPdf function.

(node:19460) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fonts' of undefined
    at PDFDocument.endAcroForm (C:\code\testpdf\node_modules\pdfkit\js\pdfkit.js:5472:39)
    at PDFDocument.end (C:\code\testpdf\node_modules\pdfkit\js\pdfkit.js:6233:10)
    at C:\code\testpdf\test.js:20:7

It never gets past the init of createPdf.

`const fs = require('fs') const PDFDocument = require('pdfkit') const signer = require('./signpdf').default const { pdfkitAddPlaceholder } = require('./helpers')

const createPdf = params => new Promise((resolve) => { const requestParams = { placeholder: {}, text: 'node-signpdf', addSignaturePlaceholder: true, pages: 1, ...params } const pdf = new PDFDocument({ autoFirstPage: false, size: 'A4', layout: 'portrait', bufferPages: true }) pdf.info.CreationDate = '' if (requestParams.pages < 1) { requestParams.pages = 1; } for (let i = 0; i < requestParams.pages; i++) { pdf.addPage().fillColor('#333').fontSize(25).moveDown().text(requestParams.text).save() } const pdfChunks = [] pdf.on('data', (data) => { pdfChunks.push(data) }) pdf.on('end', () => { resolve(Buffer.concat(pdfChunks)) }) const refs = pdfkitAddPlaceholder({ pdf, pdfBuffer: Buffer.from([pdf]), reason: 'I am the author', ...requestParams.placeholder }) Object.keys(refs).forEach(key => refs[key].end()) pdf.end() })

async function test() { const pdf = await createPdf({ placeholder: { signatureLength: 2, } }) pdf = signer.sign(pdf, fs.readFileSync(./resources/certificate.p12)) pdf.pipe(fs.createWriteStream('test.pdf')) pdf.end() }

(async () => { await test() })() `

bahinapster commented 3 years ago

same here

JohnDG89 commented 3 years ago

any solution?

petrkrizek commented 2 years ago

downgrade to pdfkit@^0.10.0 solves this

YATACOBLAS commented 2 years ago

@petrkrizek thanks a lot, its work

RdC1965 commented 2 years ago

@petrkrizek, yes, it works, thanks! Any news on why this could be happening?

vbuch commented 2 years ago

@RdC1965 the pdfkit helper in the library was written for pdfkit 10. Version 11 support is on the way here https://github.com/vbuch/node-signpdf/pull/115

RdC1965 commented 2 years ago

Thanks a lot, Valery.  I am looking forward to the upgrade. In the meantime, I am quite happy with version 10: it works perfectly. Regards, Rodrigo

On Tuesday, October 19, 2021, 08:37:34 AM GMT+2, Valery Buchinsky ***@***.***> wrote:  

@RdC1965 the pdfkit helper in the library was written for pdfkit 10. Version 11 support is on the way here #115

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.