vbuch / node-signpdf

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

Cannot read property 'split' of undefined #61

Closed arpitagrawal0000 closed 4 years ago

arpitagrawal0000 commented 4 years ago

can anyone help why I am getting this issue? here is my code:-

const signer = require('node-signpdf').default;
const  {plainAddPlaceholder} = require('node-signpdf/dist/helpers');

const action = async () => {
var pdfBuffer = fs.readFileSync('./test.pdf');
 pdfBuffer = plainAddPlaceholder({
    pdfBuffer,
    reason: 'I am the author',
    signatureLength: 1612,
  });
let p12Buffer = fs.readFileSync('./certificate.p12')
let pdf = signer.sign(pdfBuffer, p12Buffer);
fs.writeFileSync(__dirname + '/result.pdf', pdf);
}

action();

and i am getting this error.

(node:9110) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'split' of undefined at getXref (/home/hp/project/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js:55:34) at getFullXrefTable (/home/hp/project/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js:79:25) at readRefTable (/home/hp/project/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js:100:25) at readPdf (/home/hp/project/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readPdf.js:31:46) at plainAddPlaceholder (/home/hp/project/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/index.js:54:37) at action (/home/hp/project/index.js:92:14) at Object. (/home/hp/project/index.js:105:1) at Module._compile (internal/modules/cjs/loader.js:1157:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10) at Module.load (internal/modules/cjs/loader.js:1001:32) (node:9110) 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: 1) (node:9110) [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.

vbuch commented 4 years ago

Without a PDF that fails, I doubt it that someone would help.

dipedro commented 4 years ago

@arpitagrawal0000 you are using a async function. so you have use await before call the method sign( ).

zerobytes commented 4 years ago

Here goes a pdf that fails, for me. Same error, same case. pdf that [pdf-that-fails.pdf…]()

@dipedro i doubt that your comment is correct, as there is no promise return in none of the methods.

vbuch commented 4 years ago

Yup. plainAddPlaceholder is synchronous so that's not the issue.

@zerobytes bad link?

nfcaballaro commented 4 years ago

Hi! check if your document is linearized, please visit: https://www.pdftron.com/blog/pdf-format/what-is-pdf-linearization/ The bug that exists is when the split is made, the "trailer" section is not found. Then it fails.

/home/hp/project/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js const [objects, infos] = refTable.toString().split('trailer');

ankology commented 4 years ago

Same error here.. PDF that fails PDF 140mb Error: Cannot read property 'split' of undefined readRefTable.js#L48

nfcaballaro commented 4 years ago

@ankology if you edit the document, you can see in the first lines that your document is linearized

%PDF-1.5 %âãÏÓ 1028 0 obj <</Linearized 1/L 142559740/O 1031/E 414421/N 242/T 142558182/H [ 490 812]>> endobj

stale[bot] commented 4 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.

ghoshpushpendu commented 2 years ago

this issue occurs when you are using PDF-Lib to add or parse the PDF before the signing process.

{ useObjectStreams: false } to the save() method did the trick! in pdf-lib