PhantomJS: pipe input PhantomJS: pipe input linux linux

PhantomJS: pipe input


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