2012-07-31 74 views
0

UPDATE:wkhtmltopdf暂停,需要人工干预

好像wkhtmltopdf不退出正确


我做的节点以下的问题:

console.log("before"); 
fs.writeFile(html_filename, html, function (err) { 
    if (err) {res.writeHead(400); res.end("" + err); return;} 

    console.log("wrote html fine; now converting"); 
    exec('wkhtmltopdf ' + html_filename + ' ' + pdf_filename, function (err, stdout, stderr) { 
    if (err) {res.writeHead(400); res.end("" + err); return;} 

    console.log("converted; now reading"); 
    fs.readFile(pdf_filename, function (err, data) { 
     if (err) {res.writeHead(400); res.end("" + err); return;} 

     console.log("read fine; now serving"); 
     res.writeHead(200, {"content-type" : "application/pdf"}); 
     res.end(data); 
    }); 
    }); 
}); 

工作正常,除了每次执行该操作时,节点程序都会挂起,当我在cmd + tab中看到“exec”进程时。当我选择这个过程时,节点程序继续。

任何想法为什么?

+0

当您运行“wkhtmltopdf”d时是否会发生相同的行为直接从命令行? – maerics 2012-07-31 19:53:49

+0

哇,实际上,是的 – Max 2012-07-31 19:56:30

+0

已编辑的问题,以反映这 – Max 2012-07-31 19:59:53

回答

0

嗯,这看起来像一个OSX错误...

3

作为替代wkhtmltopdf我会用Phantom.js:http://phantomjs.org/

您可以创建一个简单的脚本光栅化:

var page = require('webpage').create(), 
address, output, size; 

if (phantom.args.length < 2 || phantom.args.length > 3) { 
    console.log('Usage: rasterize.js URL filename'); 
    phantom.exit(); 
} else { 
    address = phantom.args[0]; 
    output = phantom.args[1]; 
    page.viewportSize = { width: 600, height: 600 }; 
    page.open(address, function (status) { 
     if (status !== 'success') { 
      console.log('Unable to load the address!'); 
     } else { 
      window.setTimeout(function() { 
       page.render(output); 
       phantom.exit(); 
      }, 200); 
     } 
    }); 
} 

然后调用:

phantomjs rasterize.js http://path/to/webpage output.pdf 
+0

谢谢,但由于我不pdf'ing静态HTML,但基于HTTP请求的动态,我不认为这种解决方案可以帮助我比wkhtmltopdf。 – Max 2012-08-03 00:05:15

+0

phantomjs可以与所有网址一起使用,包括基于http请求的动态网址。 phantomjs是一款纯粹的无头像webkit&javascript api,它在社区中比wkhtmltopdf更加活跃,并且有很多用途。但是,嘿,这取决于你.... – rogchap 2012-08-09 14:01:35