Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a webpage to pdf document using php

Tags:

php

pdf

I can see there are a few options for doing this. My question is does any one know of a way that I can simply pass a php web page into a pdf creator and it outputs exactly the same as if it was viewed via a browser? Taking into consideration CSS, images and php functions etc.

Thanks

like image 360
aHunter Avatar asked Feb 28 '11 12:02

aHunter


2 Answers

Think wkhtmltopdf is the answer you are looking for as it uses WebKit to render the webpage just like it would look in a browser and then converts it to a PDF. This saves you writing a load of PHP code to organise the layout of your PDF.

Details from their site:

Simple shell utility to convert html to pdf using the webkit rendering engine, and qt.

Searching the web, I have found several command line tools that allow you to convert a HTML-document to a PDF-document, however they all seem to use their own, and rather incomplete rendering engine, resulting in poor quality. Recently QT 4.4 was released with a WebKit widget (WebKit is the engine of Apples Safari, which is a fork of the KDE KHtml), and making a good tool became very easy.

  • Convert web pages into PDF documents using webkit
  • Adding headers and footers (static version only)
  • TOC generation (static version only)
  • Batch mode conversions
  • (Linux) No longer requires an XServer to be running (however the X11 client libs must be installed)

For code and more information on wkhtmltopdf integration with PHP see https://github.com/aur1mas/Wkhtmltopdf

like image 67
Treffynnon Avatar answered Sep 19 '22 20:09

Treffynnon


This is a two-part question really.

  • Making a screen shot of a web site into an image has many good answers on SO

  • To generate a PDF, use e.g. the fpdf library.

like image 39
Pekka Avatar answered Sep 21 '22 20:09

Pekka