views:

2184

answers:

6

I am a php web developer and in my one project i need to convert some html document (of 30 to 50 pages) into pdf .

For this I got following some solutions, among them some are php libraries and some are command line applications.

But each has their own advantage and disadvantage.

php libraries: Followings are some php libraries i tried.

  1. fpdf (need more effort to convert)
  2. tcpdf (need more effort to convert)
  3. html2fpdfhtml2fpdf dot sourceforge dot net
  4. html2pdf html2pdf dot fr/
  5. dompdf code dot google dot com/p/dompdf/ (compared to other working good)

For each library have problem like:

  1. Takes long time for conversion process (more than 5min to conert 30 html pages)
  2. Need more resorce (memory and time)

    (on my machine i set following parameter in php.ini file: max_execution_time = 600 memory_limit = 250M

    but still not work.)

  3. need html pages in well formated (no missing html close tag otherwise gives error)

But when i try all these libraries to convert simple html of 5 or less pages with less css , all works.

Command line application

For command line application , i want to say that all works perfectly and very fast as compared to all above libraries only when i run it directly on console. but when i try to use them in php with exec() or system() function ,gives error.

Following are these command line application and there errors when I run them in php

html2pdf : http://www.tufat.com/s%5Fhtml2ps%5Fhtml2pdf.htm

gives error: html2pdf:11380): Gtk-WARNING **: cannot open display: :0.0

No protocol specified

wkhtmltopdf :

gives error

Loading page: 10%
Loading page: 33%
Loading page: 100%
Waiting for redirect
Outputting pages
QPainter::begin(): Returned false QPainter::begin(): Returned false QPainter::save: Painter not active QPainter::scale: Painter not active QPainter::setRenderHint: Painter must be active to set rendering hints QPainter::setBrush: Painter not active QPainter::pen: Painter not active QPainter::setPen: Painter not active

htmltopdf : www dot ultrashareware dot com/html-to-pdf.htm

So now I am looking for help.

Can any one answer:

Which php library work good in my case?

Why this errors occur in command line applications?

A: 

But what if You will use any online service and send Your HTML content over HTTP? Of course most of them are not free.

Rafal Ziolkowski
can you suggests any on-line service and their URL/link
santosh
http://www.freepdfconvert.com is free, as the name suggests. Automating its use may not be the easiest thing to do, on the other hand, but it can take either an uploaded file or a URL.
Julian
And this could take a while to create big set of PDF.
Rafal Ziolkowski
freepdfconvert.com has no support for flash files and javascript. And generated pdf is not exactly as the website looks. for example i tried pazintys.com
FDisk
A: 

One possibility: having the script automatically:

  1. Take the web page
  2. Open that page in a web browser
  3. Take a screencap of that page
  4. Turn it into a PDF

step 4 is easy - there are plenty of PHP/cmdline libraries that will let you put images onto a pdf or convert them (eg, fpdf.)

For steps 1-3... you might could try looking at the code from here: http://browsershots.org/. Not sure if it would be relevant - it seems like it requires a lot of setup. Maybe their architecture could work?

rascher
but, what about links or anchor tag in html pages?
santosh
That is a terrible solution. It will turn all the text into bitmap graphics. It will use screen css instead of print css. It will only show as much of the page as can fit in a screen capture. There are plenty of ways to do it better. Please don't do this!
rjmunro
A: 

A couple of questions and suggestions:

  • Do you really need it converted to PDF? Why? In some cases, it would be better to stick with HTML.
  • Is upgrading the hardware of the server that generates the PDF an option? I asked this because if all the libraries that you've tried is taking too long to create, then your only option might be upgrading the server.
  • You might want to solve the problem with the command line error. If it gives the fastest results, then find a work around it.
Randell
do you know any command line app for same apart from those mentioned in question?
santosh
For PHP, i've only used dompdf, and I only have to print an average of 3 pages per call. The only other PDF generator i've used is JasperReports, but I think it's only for Java. Maybe you could most the entire stack trace of the error you're getting from the command line.
Randell
post*
Randell
+1  A: 

Regarding wkhtmltopdf:

  • This thing works blazingly fast and it can also handle all kinds of HTML/CSS you throw at it, so when you need speed, you should seriosly consider it. We switch to it recently in our company and our PDF serving got enourmous speed-boost.

  • At least under Linux it needs XOrg libraries to be installed - servers usually don't have them, so that might be your problem.

Rene Saarsoo
A: 

Try this:

FDisk
@FDisk , I already tried that and I also mentioned same in my question
santosh
+1  A: 

Have you tried Prince?

meanstreakuk
hmmmmmm? but its expensive, cost USD 495 !
santosh