使用PHP将HTML转换为PDF(不是PDF转换为HTML) [英] Converting HTML to PDF (not PDF to HTML) using PHP

查看:600
本文介绍了使用PHP将HTML转换为PDF(不是PDF转换为HTML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名PHP开发人员,在我的其中一个项目中,我需要将一些HTML文档(大约30到50页)转换为PDF文档。

我的搜索已经提出了以下可能的解决方案。其中有一些PHP库和一些命令行应用程序。每种类型都有自己的优点和缺点。

PHP库:
$ b


  1. fpdf(需要更多努力来转换)

  2. tcpdf(需要更多努力转换) ://html2fpdf.sourceforge.netrel =noreferrer> http://html2fpdf.sourceforge.net

  3. html2pdf http://html2pdf.fr/

  4. dompdf http://code.google.com/p/dompdf/ (与其他方式相比,效果很好)

对于每个图书馆,我都有类似的问题:


  1. 需要很长时间(超过五分钟转换30个HTML页面)

  2. 需要太多资源(内存和时间)

    php.ini:

    max_execution_time = 600

    memory_limit = 250M



    但是sti )


  3. 需要格式良好的HTML页面(例如,没有失踪的关闭标签)

所有这些工作,当我尝试转换简单的HTML文档(五或更少的CSS页面)



命令行应用程序

与上述库相比,所有命令行应用程序都可以非常快速地工作,但只有在控制台上直接运行它们时才是如此。当我尝试在PHP中使用 exec() system()时,它们给我错误。 p>

以下是我在PHP中运行它们时的命令行应用程序及其错误:


  1. html2pdf( http://www.tufat.com/s_html2ps_html2pdf.htm
    $ b


    html2pdf:11380):Gtk-WARNING **:无法打开显示:: 0.0
    没有指定协议


  2. wkhtmltopdf
    $ b


    :10%

    加载页面:33%

    加载页面:100%

    等待重定向

    输出页面

    QPainter :: begin():返回false

    QPainter :: begin():返回false

    QPainter :: save:画家不活跃

    QPainter :: scale:Painter无效

    QPainter :: setRenderHint:Painter必须处于活动状态才能设置渲染提示

    QPainter :: setBrush:画家不活跃

    QPainter :: pen:画家不活跃

    QPainter :: setPen:画家不活跃


  3. htmltopdf( http: //www.ultrashareware.com/html-to-pdf.htm


现在我我正在寻求帮助。任何人都可以回答:

在我的情况下,哪个PHP库可以正常工作? > 为什么在命令行应用程序中出现这些错误?

关于 wkhtmltopdf strong>:


  • 这个东西速度非常快,它也可以处理所有的HTML / CSS,当你需要速度时,你应该仔细考虑它。我们最近在公司切换到了它,并且我们的PDF服务获得了极大的速度提升。

  • 至少在Linux下它需要安装XOrg库 - 服务器通常没有它们,所以这可能是你的问题。

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:

  1. fpdf (need more effort to convert)
  2. tcpdf (need more effort to convert)
  3. html2fpdf http://html2fpdf.sourceforge.net
  4. html2pdf http://html2pdf.fr/
  5. dompdf http://code.google.com/p/dompdf/ (compared to other, works well)

For each library, I have problems like:

  1. Takes a long time (more than five minutes to convert 30 HTML pages)
  2. 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.)

  3. Needs HTML pages to be well-formatted (e.g. 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:

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

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

  2. 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

  3. 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.

这篇关于使用PHP将HTML转换为PDF(不是PDF转换为HTML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆