32

I am trying to convert SVG to PNG with node js. My code is here:

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'image/png'});
  var convert = child_proc.spawn("convert", ["svg:", "png:-"]),
      values = (url.parse(req.url, true).query['values'] || ".5,.5")
        .split(",")
        .map(function(v){return parseFloat(v)});

  convert.stdout.on('data', function (data) {
    res.write(data);
  });
  convert.on('exit', function(code) {
    res.end();
  });

  jsdom.env({features:{QuerySelector:true}, html:htmlStub, scripts:scripts, done:function(errors, window) {
    var svgsrc = window.insertPie("#pie", w, h, values).innerHTML;
    //jsdom's domToHTML will lowercase element names
    svgsrc = svgsrc.replace(/radialgradient/g,'radialGradient');
    convert.stdin.write(svgsrc);
    convert.stdin.end();
  }});
}).listen(8888);

While executing I got this error (in MAC)

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:980:11)
    at Process.ChildProcess._handle.onexit (child_process.js:771:34)

I have specified the path for nodejs. But i dont know why it fails. Any idea about this issue?

4

3 回答 3

30

它可能会失败,因为它找不到convert应用程序。路径是否convert存在于您的环境 PATH 中?你可以convert从你的终端运行吗?

于 2013-09-17T10:53:24.633 回答
11

我收到了错误

Uncaught Error: spawn myExeCommand ENOENT

一旦我向 spawn() 添加了“选项”,它就起作用了。

let options = {shell: true};
let theProcess = child_process.spawn(myExeCommand, null, options);
于 2019-08-07T17:40:31.120 回答
1

我在 Linux 上运行时遇到了同样的问题。我做了npm install unoconv并且认为这将负责安装转换应用程序,但只有当我安装它时,我才能让它在 Node.js 中运行 sudo apt-get install unoconv

于 2015-01-16T11:16:50.693 回答