PhantomJS: pipe input

99封情书 提交于 2019-11-29 03:27:56

You can do what you're looking for very simply (it's just not really documented) directly in PhantomJS.

var page = require('webpage').create(),
    fs = require('fs');

page.viewportSize = { width: 600, height: 600 };
page.paperSize = { format: 'Letter', orientation: 'portrait', margin: '1cm' };

page.content = fs.read('/dev/stdin');

window.setTimeout(function() {
    page.render('/dev/stdout', { format: 'pdf' });
    phantom.exit();
}, 1);

(May need to increase the timeout if you have images that need loading, etc.)

HTML comes in stdin, PDF binary goes out stdout. You can test it like:

echo "<b>test</b>" | phantomjs makepdf.js > test.pdf && open test.pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!