Create WebPage Screenshots with Node.js and PhantomJS

By  on  

Automation on the web has gotten incredibly accessible and advanced, much in part to utilities like PhantomJS.  PhatomJS allows you to do headless WebKit render testing, network monitoring, page automation, and much more.  One of the simple tasks I like using PhatomJS for is screenshot creation.  Sure I could use another service or another utility but PhantomJS is so flexible and easy to use that there's no need to look elsewhere!

Creating a Screenshot

Assuming you've downloaded PhatomJS, create a JavaScript file (screenshot.js for example) with the following contents:

var page = require('webpage').create();
page.open('https://davidwalsh.name/', function() {
  page.render('davidwalshblog.png');
  phantom.exit();
});

With that script created, open your command line tool and execute the following:

phantomjs screenshot.js

That's all it takes to create a screenshot of a website!

Setting Viewport Size

PhantomJS makes setting the viewport size easy as well, just one setting to change:

var page = require('webpage').create();
page.viewportSize = { width: 1920, height: 1080 };
page.open('https://davidwalsh.name/', function() {
  page.render('davidwalshblog1920.png');
  phantom.exit();
});

Modifying setting sizes is nice so that you can quickly automate different media query sizes based on your site's specifications!

Every few weeks I'll take screenshots of my site to ensure I've not made any design changes that have broken any of my designs.  I'll also use this strategy for creating screenshots of redesign ideas.  I love how easy automation of these types of tasks has gotten these days!

Recent Features

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    Chris Coyier&#8217;s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    MooTools FontChecker Plugin

    There's a very interesting piece of code on Google Code called FontAvailable which does a jQuery-based JavaScript check on a string to check whether or not your system has a specific font based upon its output width. I've ported this functionality to MooTools. The MooTools...

  • By
    MooTools Wall Plugin

    One of the more impressive MooTools plugins to hit the Forge recently was The Wall by Marco Dell'Anna.  The Wall creates an endless grid of elements which can be grabbed and dragged, fading in elements as they are encountered.  Let me show...

Discussion

  1. MaxArt

    It’s worth noting that Phantom uses WebKit as its render engine. Slimer uses Gecko instead, while a headless version of Chrome – directly from Google – is also next to be released.

    Alas, nothing decent for Trident or Edge.

  2. Great script for generating screenshots from responsive websites and web applications to see if everything is designed well. Thank you for this blog! :)

  3. If you’d like some more options and easy integration with other code I recommend checking Manet https://github.com/vbauer/manet

  4. DaveR

    Last time I tried node + phantom I ran into a lot of problems around webfont rendering ranging from webfonts not be rendered at all to webfonts partially rendering (with different results each time I ran it) depending on which version of phantom source I built from. At some point I gave up on it and just switched over to browserstack using some of their automated testing functionality.

    I’m hoping to revisit phantomjs once the webfont support is more reliable.

    Anyone else run into webfont related issues with phantom?

  5. Great tutorial, pity taking an accurate retina screenshot isn’t easy

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!