Render a webpage and get the image as a stream
Render a webpage and get the image as a stream.
npm install phantom-render-stream
It uses a pool of phantom processes so it doesn't need to spawn a new process for each website. New requests are added to the pool member with the shortest queue length.
var phantom = ;var fs = ;var render = ;// render a website url;// or as a transform streamfs
You can also pass some options:
var render =;
Or override the options for each render stream
We support the output formats that PhantomJS's render method supports. At the time of this writing these are:
Since the interface is just a stream you can pipe the web site anywhere! Try installing picture-tube and run the following example
var phantom = ;var pictureTube = ;var render = ;;
If you need your page to do something before phantom renders it you just need to immediately set
window.renderable to false. If that is set when the page is opened the module will wait for
window.renderable to be set to true and when this happens the render will occur.
Here is an example to illustrate it better.
You can add any special cookies at render time. For format, see http://phantomjs.org/api/webpage/method/add-cookie.html. Example:
var render =;;
That will use that cookie for that particular render job. You probably want to set the
expires property to something fairly short, as there may not be a guarantee that a pooled phantom process won't pick up the cookie for a particular render job, and you may want that session to only be valid for an individual job run.
Sometimes you need to inject polyfills, e.g. PhantomJS Date.parse is broken.
You can add paths to local files to polyfill broken / missing features of PhantomJS using the
opts.injectJs property. Example:
var phantom =;
Obviously, make sure the path './includes/my-date-polyfill.js' is resolvable from the project root, or pass in an absolute path. When the page is initialized, any scripts you listed there will be injected before any rendering happens.
For rendering, PhantomJS requires the
fontconfig library, which may be missing if you're using Ubuntu Server. To install on Ubuntu:
sudo apt-get install libfontconfig
Render stream emits "log" event with useful debug details coming from onError (JS error), onConsoleMessage, onResourceError, onResourceTimeout webpage hooks.
var render = ;;
Also, some additional debugging output may be enabled by running your app with a
DEBUG environment variable set as follows:
DEBUG=phantom-render-stream node ./your-script.js
If you are getting undefined error codes and responses when attempting to
render, it's likely a connection issue of some sort. If the URL uses SSL,
--ignore-ssl-errors=true to phantomFlags may help. You also try adding
--debug=true to the
wkhtmotopdfonly supports PDF output.