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
    Write Simple, Elegant and Maintainable Media Queries with Sass

    I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

Incredible Demos

  • By
    MooTools Equal Heights Plugin:  Equalizer

    Keeping equal heights between elements within the same container can be hugely important for the sake of a pretty page. Unfortunately sometimes keeping columns the same height can't be done with CSS -- you need a little help from your JavaScript friends. Well...now you're...

  • By
    MooTools 1.2 Image Protector: dwProtector

    Image protection is a hot topic on the net these days, and why shouldn't it be? If you spent two hours designing an awesome graphic, would you want it ripped of in matter of seconds? Hell no! That's why I've created an image...

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!