Using ImageMagick from a NodeJs application30 October 2016
Lately when I needed to convert files from PDF to an image file format I was not surprised to find ImageMagick the best option for the job. I had used it before from the command line and knew to be a great piece of software.
And naturally, when wanting to integrate that PDF to PNG conversion in my current NodeJs application, I searched npm for a module easing that integration. Faced with a lot of choices (that's what you get from 300'000 building blocks) I read the description of a handful, tried a couple and ended picking one to build my solution with.
But later, I hit a bug. The module I chose, the bug I hit, are not relevant. I had moved my code in a Docker container, the operating system was different, my code was not running as expected and my PDF files were not converted correctly anymore.
After looking a while for a solution without success. I made sure that ImageMagick in my container was working properly from the command line and decided to ditch the module I had chosen and implemented my own solution :
var spawn = require('child_process').spawn; // The arguments used to call convert from ImageMagick var args = ['pdf:-', '-density', '100', 'png:-']; var convert = spawn('convert', args); // Redirect any errors that may happen to the standard error convert.stderr.pipe(process.stderr); // Pipe in my pdf data pdfDoc.pipe(convert.stdin); convert.stdout.pipe(myStream);
Ah yes it'is not files that are converted but streams of data. Faster.
And my solution works without another required module with it's own dependencies.