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
    Serving Fonts from CDN

    For maximum performance, we all know we must put our assets on CDN (another domain).  Along with those assets are custom web fonts.  Unfortunately custom web fonts via CDN (or any cross-domain font request) don't work in Firefox or Internet Explorer (correctly so, by spec) though...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Xbox Live Gamer API

    My sharpshooter status aside, I've always been surprised upset that Microsoft has never provided an API for the vast amount of information about users, the games they play, and statistics within the games. Namely, I'd like to publicly shame every n00b I've baptized with my...

  • By
    Create GitHub-Style Buttons with CSS and jQuery, MooTools, or Dojo JavaScript

    I'm what you would consider a bit of a GitHub fanboy. We all know that GitHub is the perfect place to store repositories of open source code, but I think my love of GitHub goes beyond that. GitHub seems to understand that most...

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!