nodejs' spawn and exec using pdf2swf

1k views Asked by At

I hope that there is someone that can help with this problem since I don't get it myself.

I compiled the newest version of pdf2swf and everything is working fine, except of when I want to use it as a spawned child_process in NodeJS.

When I use the same command as a exec, it works again. To provide some code example:

// convert a pdf to swf using exec (works)
var tmpSwfLocation = docsDir+'/Paper.pdf.swf';
var pdf2swf = cp.exec('pdf2swf '+tmpPDFLocation+' -o '+tmpSwfLocation+' -G -v -f -T 9 -t -j '+quality+' -s subpixels='+resolution+' -s storeallcharacters -s poly2bitmap', function(err, stdout, stderr){
  console.log(stdout);
  console.log(stderr);
  var end = Date.now();
  console.log('pdf2swf: SWF created in '+((end-start) /1000)+' seconds');
});

// convert a pdf to swf (does not work)
var tmpSwfLocation = docsDir+'/Paper.pdf.swf';
var pdf2swf = cp.spawn('pdf2swf', ['-G','-v','-f','-T 9', '-t', '-j '+quality,'-s subpixels='+resolution,'-s storeallcharacters', '-s poly2bitmap',tmpPDFLocation, '-o '+tmpSwfLocation],{setsid:true});
pdf2swf.stdout.on('data',function(output){
  console.log("pdf2swf:"+output)
});
pdf2swf.stderr.on('data',function(output){
  console.log("pdf2swf ERROR:"+output);
});
pdf2swf.stdout.on('end', function(output){
  var end = Date.now();
  console.log('pdf2swf: SWF created in '+((end-start) /1000)+' seconds');
});

The spawn process breaks in the very end, when the child_process is trying to write data into the file:

pdf2swf:NOTICE  Writing SWF file  /home/bitnami/flipdoc/ec2-instances/worker/pdf_test/test_directory/test-Testfile_31MB_26Pages.pdf_0/docs/Paper.pdf.swf
pdf2swf:FATAL   Could not create " /home/bitnami/flipdoc/ec2-instances/worker/pdf_test/test_directory/test-Testfile_31MB_26Pages.pdf_0/docs/Paper.pdf.swf". 

Using exec, the process finalizes successfully with

pdf2swf:NOTICE  Writing SWF file  /home/bitnami/flipdoc/ec2-instances/worker/pdf_test/test_directory/test-Testfile_31MB_26Pages.pdf_0/docs/Paper.pdf.swf

and a existent file.

Additional information: Before the whole process I execute a recursive chmod 777 on the whole "test_directory".

Greets

1

There are 1 answers

1
nnikpour On BEST ANSWER

Solved it myself... Never (in the means of don't even think about it...) use spaces in the arguments to the spawn process. if you have a param=value argument which can use spaces in between, just row them up in the array with every other argument.

the source code from above for spawn looks now like this:

var pdf2swf = cp.spawn('pdf2swf', [tmpPDFLocation, '-o',tmpSwfLocation,'-G','-vvv','-f','-T','9', '-t', '-j',quality,'-s','subpixels='+resolution,'-s','storeallcharacters', '-s','poly2bitmap']);