wubzz / pdf-merge

Merge multiple PDF Files into a single PDF document

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unhandled rejection Error: spawn ENAMETOOLONG

pwong2 opened this issue · comments

@wubzz I tried to merge 500-1000 pdfs and then I encountered this issue. 400 pdfs and below still works.

Unhandled rejection Error: spawn ENAMETOOLONG
    at exports._errnoException (util.js:907:11)
    at ChildProcess.spawn (internal/child_process.js:298:11)
    at exports.spawn (child_process.js:362:9)
    at exports.execFile (child_process.js:151:15)
    at tryCatcher (D:\Clone Repositories\Website\node_modules\bluebird\js\release\util.js:16:23)
    at ret (eval at <anonymous> (D:\Clone Repositories\Website\node_modules\bluebird\js\release\promisify.js:184:12), <anonymous>:15:23)
    at D:\Clone Repositories\Website\node_modules\pdf-merge\index.js:55:5
    at Promise._execute (D:\Clone Repositories\Website\node_modules\bluebird\js\release\debuggability.js:300:9)
    at Promise._resolveFromExecutor (D:\Clone Repositories\Website\node_modules\bluebird\js\release\promise.js:483:18)
    at new Promise (D:\Clone Repositories\Website\node_modules\bluebird\js\release\promise.js:79:10)
    at module.exports.childPromise.then.then (D:\Clone Repositories\Website\node_modules\pdf-merge\index.js:18:38)
    at IncomingMessage.<anonymous> (D:\Clone Repositories\Website\pdfconverter\pdfconverter.js:61:21)
    at emitNone (events.js:67:13)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:921:12)
    at nextTickCallbackWith2Args (node.js:442:9)

my code is this:

const PDFMerge = require('pdf-merge');
const path = 'D:/Clone Repositories/Website/pdfconverter/temp';
 const pdfFiles = fs.readdirSync(path).map((file) => [path, file].join('/'));

PDFMerge(pdfFiles, { output: 'D:\\Clone Repositories\\Website\\pdfconverter\\batch\\batch1.pdf', libPath: 'C:\\Program Files (x86)\\PDFtk\\bin\\pdftk.exe' })
   .then(() => {
           res.writeHead(200);
           res.end();
});
commented

@pwong2 This is caused by string limitation in Windows. Not something the library will handle for you.

See https://support.microsoft.com/sv-se/help/830473/command-prompt-cmd--exe-command-line-string-limitation for details.

A workaround is to merge like 100 pdfs at a time, and then merge the merged files at the end.. So 500 pdfs would first be 100 / 100 / 100 / 100 / 100 = 5 PDF files -> Merge these 5 = 1 file.

thanks @wubzz! appreciate all your help. I tried with that and it worked. There will be just more effort than merging all files at once.