13

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

aHunter
  • 3,350
  • 10
  • 37
  • 45

4 Answers4

9

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

Treffynnon
  • 20,865
  • 5
  • 62
  • 96
2

This is a two-part question really.

Community
  • 1
  • 1
Pekka
  • 431,103
  • 135
  • 960
  • 1,075
  • 2
    Or a third option is to use a rendering engine and output the result as a PDF like wkhtmlpdf which uses WebKit to render the page. This saves you writing a load of PHP code to organise the layout of your PDF. http://code.google.com/p/wkhtmltopdf/ – Treffynnon Feb 28 '11 at 12:57
1

The fact that it's a PHP web page makes no difference. In order to create a PDF that looks like the web page should look, you need to render the page - this is what a web browser does when you visit it.

You'll need to use a web browser engine such as Gecko to produce the page layout, then export that as an image and stick it in a PDF. This can be done programatically.

Borealid
  • 91,119
  • 8
  • 102
  • 121
0

There are php modules/classes for PDF generation, and there are ways of using CSS to better prepare a page for printing (to pdf).

PHP cannot 'see' the page as the browser sees it.

Adam Hopkinson
  • 27,556
  • 7
  • 64
  • 93
  • I think this is the point exactly I have an application site that includes pages of code using function to print aspects, images, advanced CSS etc. I do not want to spend months going through it all to make it PDF compatible or have to duplicate pages of code specifically for the purpose of PDF. – aHunter Feb 28 '11 at 12:59