A few years ago, generating PDF files via web server side scripts was a real nightmare. Although several libraries were available for the task, most of them were very difficult to use. Times have changed, and now days easiest approach is to convert HTML to PDF.

WKHTMLTOPDF

wkhtmltopdf and is an open source (LGPL) command line tool to render HTML into PDF using the QT Webkit rendering engine. It runs entirely “headless” and does not require a display or display service.

More info: http://wkhtmltopdf.org/

Note: Similar to wkhtmltopdf, there is wkhtmltoimage which converts HTML into various image format, also using Webkit.

PhantomJS

PhantomJS is much more then a screen capturing tool. As the authors state: “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.” Except for screen capturing, it can be used for headless website testing, page automation and network monitoring.

Way of getting PDF from your HTML using PhantomJS can be found here.