Take a screenshot of a HTML fragment or URL
This module is installed via npm:
$ npm install html2png
Render some HTML into a PNG Buffer Object
var html2png = require('html2png');
var screenshot = html2png({ width: 1280, height: 720, browser: 'phantomjs'});
screenshot.render('<b>Hello</b>', function (err, data) {
// If there is an error close the web browser first before calling the
// errback
if (err) return screenshot.error(err, cb);
// data will contain a screenshot of the HTML as a node.js Buffer
console.log(data);
//<Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 02 80 ...>
// Close the web browser (phantomjs or chrome)
screenshot.close(done);
});
The constructor is passed an options object:
width
,height
- the width and height of the browser. NB: This is not the maximum dimensions of each screenshot. So if the rendered page is higher thanheight
the screenshot returned will be the full rendered height of the page.browser
- The browser to use for rendering. By default this isphantomjs
and this module bundles together a static binary of phantomjs with phantomjs-bin. If you have Google Chrome installed andchromedriver
is in yourPATH
, then you can render with Chrome.
Renderes html
into a PNG node.js Buffer
and returns the data in the
callback.
Renders the URL url
into a PNG node.js Buffer
and returns the data in the
callback.
Closes the underlying webdriver web browser used for the rendering (eg. Phantomjs or Chrome). Highly recommended.
Helper method that will call #close
first, before calling the usual errback
with the error.