node-signpdf: TypeError: Cannot read property 'split' of undefined
Hi! I have the error with this pdf file and others generated with a same library, please if you could give me a hand I would really appreciate it.
Here is the pdf file: descarga.pdf
The code used to sign:
const p12Buffer = fs.readFileSync('./assets/certificados/certificate.pfx');
let pdfBuffer = fs.readFileSync('./assets/pdfEntrada/descarga.pdf');
pdfBuffer = plainAddPlaceholder({
pdfBuffer,
reason: 'I have reviewed it.',
signatureLength: 1612,
});
pdfBuffer = await signer.sign(
pdfBuffer,
p12Buffer,
{passphrase: 'password'},
);
and i am getting this error.
(node:8496) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘split’ of undefined at getXref (C:\Users\danie\Proyectos\firmaDig_backend\helpers\plainAddPlaceholder\readRefTable.js:48:36) at getFullXrefTable (C:\Users\danie\Proyectos\firmaDig_backend\helpers\plainAddPlaceholder\readRefTable.js:77:27) at readRefTable (C:\Users\danie\Proyectos\firmaDig_backend\helpers\plainAddPlaceholder\readRefTable.js:99:27) at readPdf (C:\Users\danie\Proyectos\firmaDig_backend\helpers\plainAddPlaceholder\readPdf.js:22:22) at plainAddPlaceholder (C:\Users\danie\Proyectos\firmaDig_backend\helpers\plainAddPlaceholder\index.js:36:18) at firmarPDF2 (C:\Users\danie\Proyectos\firmaDig_backend\app.js:231:17) at procesoCompleto (C:\Users\danie\Proyectos\firmaDig_backend\app.js:55:11) at C:\Users\danie\Proyectos\firmaDig_backend\app.js:34:5 at Layer.handle [as handle_request] (C:\Users\danie\Proyectos\firmaDig_backend\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\danie\Proyectos\firmaDig_backend\node_modules\express\lib\router\route.js:137:13) (node:8496) 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(). (rejection id: 2) (node:8496) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 18 (3 by maintainers)
Commits related to this issue
- 🧪 Add a failing test to parse the resource from #79 — committed to vbuch/node-signpdf by vbuch 8 months ago
- 🧪 Add a failing test to parse the resource from #79 — committed to vbuch/node-signpdf by vbuch 8 months ago
sorry briefly my code, this code works on me!
i use pdf-lib : 1.14.1 node-signpdf : 1.3.2.
const { PDFDocument, } = require(“pdf-lib”);
const signer = require(“node-signpdf”).default; const fs = require(“fs”); const { plainAddPlaceholder } = require(“node-signpdf/dist/helpers”);
//add this your code var pdfDoc = await PDFDocument.load(bytes, { ignoreEncryption: true, });
//add this your code const pdfBytes = await pdfDoc.save({ useObjectStreams: false });
const p12Buffer = fs.readFileSync(path.join(__dirname, “…/utils/test.pfx”));
const pdfBuffer = fs.readFileSync(
${sourceFolder}/${test}); dinamicconst pdfWithPlaceholder = plainAddPlaceholder({ pdfBuffer, reason: “i can do it”, });
const signedPdf = signer.sign(pdfWithPlaceholder, p12Buffer, { passphrase: “test”, });
fs.writeFileSync(“test-id.pdf”, signedPdf);
I can confirm that @Arisdolanan solution works!
I was already using pdf-lib to add a “visual signature” on the document before signing it, and it was not working, but adding
{ useObjectStreams: false }to thesave()method did the trick!Thanks a lot!
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.
After further research, while it does seem to be a problem with the lib not being able to read newer PDFs, I found a simple work around for the time being:
Is able add the proper trailer to the end of the PDF. In my case the PDF was also protected, so I used:
Note: This was able to preserve the form fields!
I hope this helps someone else and saves them the 3 days it took me!
At the moment, fixing this issue is no longer a priority, but I am interested in learning more about PDFs so I may look into this at a later date.
Not sure how much higher is not supported. But, yes, streams, linearized, etc… features that I’ve seen in 1.5+ are not supported. PRs are always welcome.