Menu

How cool is Node.js?

An introduction to Node.js

Node.js is part runtime environment and part library for building network applications using server-side JavaScript. It uses Chrome's JavaScript runtime engine to execute JS code directly without the need for the browser sandbox.

Basically, that means you can write the same language (Javascript) on the Client and Server side. Neat huh?

PhantomJS

We're going to create a PDF from an HTML page with the command line using PhantomJS.

PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

Lets go ahead and install PhantomJS. This can be done with the following command in terminal.

npm install phantomjs

That will install PhantomJS and all it's necessery dependencies.

Since PhantomJS is using WebKit, a real layout and rendering engine, it can capture a web page as a screenshot. Because PhantomJS can render anything on the web page, it can be used to convert contents not only in HTML and CSS, but also SVG and Canvas.

The following script: github.js, demonstrates the simplest use of page capture. It loads the Github homepage and then saves it as a PDF.