2012-07-31 76 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