PhantomJS: Convert Any Web Pages to PDF or PNG on Server Side

While developing a new feature for our vSearch product, I found the Javascript’s capability to screenshot a page is quite limited. As normal, the explanation is that it’s designed as such due to security reasons. No one wants a Javascript secretly capture a screenshot of what’s currently viewed. I personally don’t think it’s a good explanation why it’s not implemented as easily as it should have been. Similar, I think riskier, features like exchange clipboard content has been implemented.

Anyway, there is a backup plan which is to use PhantomJS tool. You can simply think it as a browswer, but it has no GUI. Sounds contradictory? It can be used for GUI testing, exporting Web content to different format, etc.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

The phantomjs can be easily installed and run on various platforms. I just did one on CentOS and find it’s pretty easy and fast.

Here are the steps to get it installed. If you use a different platform (32 bits or Windows, Mac), you can find their links from the download page.

# cd /opt
# wget
# tar xvf phantomjs-2.1.1-linux-x86_64.tar.bz2
# ln -s phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/local/bin
# yum install freetype fontconfig

Now, let’s write a simple Javascript code as follows:

#vi saveHtml2pdf.js
var WebPage = require('webpage');
page = WebPage.create();'');
page.onLoadFinished = function() {
  page.render('doublecloud_org' + '.png');

The Javascript above is quite simple. It basically creates a new Web page and then open to a URL to this site. You can always change it to any site of your choice. The onLoadFinished is a callback function that is called when the page is done with page loading. The function instructs the WebPage object to render it to png file and exit the phantom engine. If you want to save it to PDF, you can simply change the “.png” to “.pdf”.

When you are ready, you can run the following command and get the png file of my blog site in the same folder.

# phantomjs saveHtml2pdf.js

This is just the very basic of what phantomjs can do. You can check out many more use cases where phantomjs can help.

This entry was posted in Applications & Tools and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Posted November 14, 2016 at 12:01 am | Permalink

    [DoubleCloud] PhantomJS: Convert Any Web Pages to PDF or PNG on Server Side

  2. Posted November 14, 2016 at 1:27 am | Permalink

    PhantomJS: Convert Any Web Pages to PDF or PNG on Server Side #Applications_amp_Tools #JavaScript

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.