24

I have a URL (for e.g. http://www.example.com/OtterBox-77-24444-Commuter-Series-Optimus/dp/B00A21KPEI/ref=pd_sim_cps_4) and want to take a screenshot of it and preview it on my web page. Meaning, the user clicks on the preview button and PhantomJS needs to preview the web page as PNG/JPEG

I'm ok with using any other open source too.

2 Answers 2

34

I am going to assume you have installed Phantomjs and have created an alias in your .bashrc or have updated your system path to call the Phantomjs binaries. If not, you need to peruse a few beginner tutorials: http://net.tutsplus.com/tutorials/javascript-ajax/testing-javascript-with-phantomjs/

Once you have that set up, you will need to write a simple javascript file that you will call in the terminal (or shell, if you are using Windows). I will provide a simple, example script below.

var WebPage = require('webpage');
page = WebPage.create();
page.open('http://google.com');
page.onLoadFinished = function() {
   page.render('googleScreenShot' + '.png');
   phantom.exit();}

Then, save your js file. Open up your terminal or shell and run the following

phantomjs yourFile.js

That's it. Check the directory where you called the js file and you should have a png file with a screen shot of your web page.

This is very simple and there are a lot of caveats to f-ing with phantomjs, but this is about as basic as I can make it. If you need other recipes for phantomjs, try looking at these example scripts: https://github.com/ariya/phantomjs/wiki/Examples

3
  • 1
    If anyone sees this... How to use this method to save screenshot of a html page that I created locally?
    – F.S.
    Aug 9, 2016 at 17:57
  • 1
    @Chris if you're running a local server, you should be able to navigate to http://localhost or wherever you're serving it from. It'll work exactly the same, as browsers generally don't care where a given server is. If you aren't running a local server, try navigating to the absolute path of the file using the file protocol, e.g. file:///path/to/file.html
    – acobster
    Jan 3, 2017 at 23:53
  • @acobster I've moved away from that project in my internship for a while. But thanks for your effort, especially in this new year season!
    – F.S.
    Jan 5, 2017 at 0:42
14

The above answer is fine for basic usage but PhantomJS doesn't know if all AJAX requests have been loaded or not. I made url-to-image to help with this problem.

  1. npm install url-to-image
  2. Write screenshot.js

    var screenshot = require('url-to-image');
    screenshot('http://google.com', 'google.png').done(function() {
        console.log('http://google.com screenshot saved to google.png');
    });
    
  3. Run script node screenshot.js
4
  • Hi @Kimmo can i set height,width or useragent in this? Jun 18, 2014 at 6:56
  • @MohitArora yeah, you can set width and height. The documentation was pretty bad and didn't mention options at all. I added detailed usage example to github.com/kimmobrunfeldt/url-to-image#api
    – Kimmo
    Jun 19, 2014 at 16:47
  • 2
    This is awesome. npm i -g url-to-image; urltoimage http://localhost:3000 ~/Desktop/foo.jpg Finally full page images. For the love of god please package this as a Chrome Extension that actually works! Feb 9, 2016 at 19:42
  • Worked great. I combined with PosteRazor to slice the long, thin, PNG generated with this tool into a multipage PDF, which I could then print. A few details of that: in PosteRazor: Input the image in Step 1 (e.g. "foo.png"), click through to Step 3 and adjust the setting to use "0" overlap for both height and width (the overlapping position won't matter); click through to Step 4, and adjust the setting for Size in pages -> Width to "1". Nov 2, 2016 at 20:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.