Converting HTML to PDF (not PDF to HTML) using PHP

I am a PHP developer and in one of my projects, I need to convert some HTML documents (about 30 to 50 pages) into PDF documents.

My search has turned up the following possible solutions. Among them are some PHP libraries and some command line applications. Each has its own advantages and disadvantages.

PHP libraries:

  • fpdf (need more effort to convert)
  • tcpdf (need more effort to convert)
  • html2fpdf http://html2fpdf.sourceforge.net
  • html2pdf http://html2pdf.fr/
  • dompdf http://code.google.com/p/dompdf/ (compared to other, works well)
  • For each library, I have problems like:

  • Takes a long time (more than five minutes to convert 30 HTML pages)
  • Requires too many resources (memory and time)

    (I set the following parameters in php.ini:

    max_execution_time = 600
    memory_limit = 250M

    but things still don't work.)

  • Needs HTML pages to be well-formatted (eg no missing close tags)

  • All of these work when I try to convert simple HTML docs (five or fewer pages with little CSS)

    Command line applications

    All command line apps work perfectly and very quickly compared to the above libraries, but only when I run them directly on console. When I try to use them in PHP with exec() or system() , they give me errors.

    The following are the command line applications and their errors when I run them in PHP:

  • html2pdf (http://www.tufat.com/s_html2ps_html2pdf.htm)

    html2pdf:11380): Gtk-WARNING **: cannot open display: :0.0
    No protocol specified

  • wkhtmltopdf

    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 (http://www.ultrashareware.com/html-to-pdf.htm)

  • So now I am looking for help. Can anyone answer:

    Which PHP library would work well in my case?

    Why do these errors occur in command line applications?


    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.


  • Try this:

  • tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples
  • fpdf.org
  • http://code.google.com/p/wkhtmltopdf/

  • 你有没有试过王子?

    链接地址: http://www.djcxy.com/p/88488.html

    上一篇: 将HTML文件转换为PDF

    下一篇: 使用PHP将HTML转换为PDF(不是PDF转换为HTML)