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

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

问题描述

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

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.

我的搜索出现了以下可能的解决方案.其中包括一些 PHP 库和一些命令行应用程序.每个都有自己的优点和缺点.

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 库:

  1. fpdf(需要更多的转换)
  2. tcpdf(需要更多的转换)
  3. html2fpdf http://html2fpdf.sourceforge.net
  4. html2pdf http://html2pdf.fr/
  5. dompdf http://code.google.com/p/dompdf/(比较其他,效果很好)
  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. 需要很长时间(转换 30 个 HTML 页面需要超过五分钟)
  2. 需要太多资源(内存和时间)

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

(我在php.ini中设置了以下参数:

(I set the following parameters in php.ini:

max_execution_time = 600
memory_limit = 250M

但还是不行.)

需要 HTML 页面的格式正确(例如,没有缺少关闭标签)

Needs HTML pages to be well-formatted (e.g. no missing close tags)

当我尝试转换简单的 HTML 文档(五个或更少的页面,CSS 很少)时,所有这些都有效

命令行应用程序

与上述库相比,所有命令行应用程序都可以完美且快速地运行,但仅当我直接在控制台上运行它们时.当我尝试在 PHP 中通过 exec()system() 使用它们时,它们会给我错误.

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.

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

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 **: 无法打开显示: :0.0
未指定协议

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

  • wkhtmltopdf

  • wkhtmltopdf

    加载页面:10%
    加载页面:33%
    加载页面:100%
    等待重定向
    输出页面
    QPainter::begin(): 返回假
    QPainter::begin(): 返回假
    QPainter::save:画家未激活
    QPainter::scale:画家未激活
    QPainter::setRenderHint:Painter 必须处于活动状态才能设置渲染提示
    QPainter::setBrush:画家未激活
    QPainter::pen:画家未激活
    QPainter::setPen:画家未激活

    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:

    哪个 PHP 库适合我的情况?

    为什么在命令行应用程序中会出现这些错误?

    推荐答案

    关于wkhtmltopdf:

    • 这个东西运行得非常快,它也可以处理你扔给它的各种 HTML/CSS,所以当你需要速度时,你应该认真考虑它.我们最近在公司改用了它,我们的 PDF 服务得到了极大的提升.

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

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

    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天全站免登陆