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 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.
Way of getting PDF from your HTML using PhantomJS can be found here.