vbuch / node-signpdf

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

Error with acroForm #147

Closed Dennis-96 closed 2 years ago

Dennis-96 commented 2 years ago

Describe the bug and the expected behaviour When I try to create a pdf I get the acroform error.

Is it a bug in signing or in the helpers? Helpers

To Reproduce 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;
                }

                // Add some content to the page(s)
                for (let i = 0; i < requestParams.pages; i += 1) {
                    pdf
                        .addPage()
                        .fillColor('#333')
                        .fontSize(25)
                        .moveDown()
                        .text(requestParams.text)
                        .save();
                }

                // Collect the ouput PDF
                // and, when done, resolve with it stored in a Buffer
                const pdfChunks = [];
                pdf.on('data', (data) => {
                    pdfChunks.push(data);
                });
                pdf.on('end', () => {
                    resolve(Buffer.concat(pdfChunks));
                });

               // if (requestParams.addSignaturePlaceholder) {
                    // Externally (to PDFKit) add the signature placeholder.
                    const refs = pdfkitAddPlaceholder({
                        pdf,
                        pdfBuffer: Buffer.from([pdf]),
                        reason: 'I am the author',
                       // ...requestParams.placeholder,
                    });
                    // Externally end the streams of the created objects.
                    // PDFKit doesn't know much about them, so it won't .end() them.
                   // Object.keys(refs).forEach(key => refs[key].end());
              //  }

                // Also end the PDFDocument stream.
                // See pdf.on('end'... on how it is then converted to Buffer.
                pdf.end();
            });

ERROR UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fonts' of undefined at PDFDocument.endAcroForm (node_modules\pdfkit\lib\mixins\acroform.js:78:37) at PDFDocument.end (node_modules\pdfkit\lib\document.js:311:10)

Thanks in advance for your help

vbuch commented 2 years ago

Could this be because of the pdfkit version used? The helper is written for 0.10. There is this PR that is for 0.11

Dennis-96 commented 2 years ago

Could this be because of the pdfkit version used? The helper is written for 0.10. There is this PR that is for 0.11

Thank you very much for the help @vbuch , now I am going to test it, I have one more question, was it possible to solve the problem so that the signature is visibly displayed in the pdf? , since I tried solutions with pdf-lib and failed to make this happen

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had activity in the past 90 days. It will be closed if no further activity occurs. Thank you for your contributions.