Closed erickximenes closed 4 years ago
@ZaunSupremoXV were you able to fix the issue? I have similar issue calling pdfkitAddPlaceholder helper using pdfKit version 0.11.0. Everything is working fine using the 0.10.0 version.
@ZaunSupremoXV você conseguiu corrigir o problema? Tenho um problema semelhante ao chamar o auxiliar pdfkitAddPlaceholder usando o pdfKit versão 0.11.0. Tudo está funcionando bem usando a versão 0.10.0.
Yes, can you share your code?
Sure, very basic one:
const signer = require('node-signpdf').default; const pdfkitAddPlaceholder = require('node-signpdf/dist/helpers/pdfkitAddPlaceholder').default; const fs = require('fs'); const PDFDocument = require('pdfkit'); const pdf = new PDFDocument({ autoFirstPage: true, size: 'A4', layout: 'portrait', bufferPages: true, }); pdf.info.CreationDate = '';
// Add some content to the page pdf .fillColor('#333') .fontSize(25) .moveDown() .text('HELLO WORLD');
// 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', () => { console.log("pdfDoc.end"); let pdfBuffer = Buffer.concat(pdfChunks); let signedPDF = signer.sign(pdfBuffer, fs.readFileSync('pdf/cert/certificate.pfx')); fs.writeFileSync('pdf/document.pdf', signedPDF); });
// Externally (to PDFKit) add the signature placeholder. const refs = pdfkitAddPlaceholder({ pdf, pdfBuffer: Buffer.from([pdf]), reason: 'I am the author' }); // 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();
@vladh1 right, i will send you my code. I'm using it in Express JS, it works in Adonis Js too.
The example is configured for Express
In my example, the PDF is more complex, the fields are filled out using a form that the user fills out.
was it a help?
Thanks. I used similar approach using plainAddPlaceholder instead of pdfkitAddPlaceholder, but the issue is with the vey big pdfs (~ 8MB) where plainAddPlaceholder takes 5 min to add the placeholder. So, in my case that is not an option.
My code:
doc = new PDFDocument({ autoFirstPage: true, size: 'A4', margins: { top: 90, bottom: 80, left: 72, right: 72 }, layout: 'portrait', bufferPages: true, }); let fieldIds = [], form; const signatureLength = 8192, byteRangePlaceholder = '**', signature = doc.ref({ Type: 'Sig', Filter: 'Adobe.PPKLite', SubFilter: 'adbe.pkcs7.detached', ByteRange: [ 0, byteRangePlaceholder, byteRangePlaceholder, byteRangePlaceholder ], Contents: Buffer.from(String.fromCharCode(0).repeat(signatureLength)), Reason: new String("Blabla"), M: new Date(), ContactInfo: new String("bla@bla.pt"), Name: new String('Bla'), Location: new String('Bla') }), signatureName = 'Signature', widget = doc.ref({ Type: 'Annot', Subtype: 'Widget', FT: 'Sig', Rect: [0, 0, 0, 0], V: signature, T: new String(signatureName + (fieldIds.length + 1)), F: 4, P: doc.page.dictionary }); doc.page.dictionary.data.Annots = [widget];
Erro:
Cannot read property 'fonts' of undefined at PDFDocument.endAcroForm (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\pdfkit\js\pdfkit.js:4900:39) at PDFDocument.end (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\pdfkit\js\pdfkit.js:5506:10) at exports.sign_pdf (C:\Users\erick\Documents\Projetos\meu-ponto-server\routes\controllers\admin.js:712:9) at Layer.handle [as handle_request] (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\route.js:137:13) at isInternal (C:\Users\erick\Documents\Projetos\meu-ponto-server\helpers\auth.js:14:20) at Layer.handle [as handle_request] (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\route.js:137:13) at isAdmin (C:\Users\erick\Documents\Projetos\meu-ponto-server\helpers\auth.js:6:20) at Layer.handle [as handle_request] (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\route.js:137:13) at isLogged (C:\Users\erick\Documents\Projetos\meu-ponto-server\helpers\auth.js:43:20) at Layer.handle [as handle_request] (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\layer.js:95:5) at C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\index.js:174:3) at router (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (C:\Users\erick\Documents\Projetos\meu-ponto-server\node_modules\express\lib\router\layer.js:95:5) (node:14696) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)HELP