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开发人员,在我的一个项目中,我需要将一些HTML文档(大约30到50页)转换为PDF文档。
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库和一些命令行应用程序。它们各有利弊。
PHP libraries:
PHP库:
- fpdf (need more effort to convert)
- fpdf(需要更多的转换)
- tcpdf (need more effort to convert)
- tcpdf(需要更多的转换)
- html2fpdf http://html2fpdf.sourceforge.net
- html2fpdf http://html2fpdf.sourceforge.net
- html2pdf http://html2pdf.fr/
- html2pdf http://html2pdf.fr/
- dompdf http://code.google.com/p/dompdf/ (compared to other, works well)
- dompdf http://code.google.com/p/dompdf/(与其他软件相比,效果良好)
For each library, I have problems like:
对于每个图书馆,我都有如下问题:
- Takes a long time (more than five minutes to convert 30 HTML pages)
- 需要很长时间(转换30个HTML页面需要5分钟以上)
-
Requires too many resources (memory and time)
需要太多的资源(内存和时间)
(I set the following parameters in php.ini:
(我在ph .ini中设置了以下参数:
max_execution_time = 600
memory_limit = 250M
max_execution_time = 600 memory_limit = 250M
but things still don't work.)
但事情还是行不通。
-
Needs HTML pages to be well-formatted (e.g. no missing close tags)
需要HTML页面具有良好的格式(例如,没有缺失的关闭标签)
All of these work when I try to convert simple HTML docs (five or fewer pages with little CSS)
当我尝试转换简单的HTML文档(5个或更少的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.
与上面的库相比,所有的命令行应用程序都运行得非常好、非常快,但只有在我直接在控制台运行它们的时候。当我尝试在PHP中使用exec()或system()时,它们会给我错误。
The following are the command line applications and their errors when I run them in PHP:
下面是我在PHP中运行的命令行应用程序及其错误:
-
html2pdf (http://www.tufat.com/s_html2ps_html2pdf.htm)
html2pdf(http://www.tufat.com/s_html2ps_html2pdf.htm)
html2pdf:11380): Gtk-WARNING **: cannot open display: :0.0
No protocol specifiedGtk-WARNING *:无法打开显示:::::0.0没有指定协议
-
wkhtmltopdf
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下载页面:10%加载页面:33%加载页面:100%等待重定向输出页面:开始():返回假QPainter:返回假QPainter:返回假QPainter:保存油漆工不活动QPainter:油漆工不活动QPainter:油漆工
-
htmltopdf (http://www.ultrashareware.com/html-to-pdf.htm)
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?
在我的例子中,哪个PHP库会工作得很好?
Why do these errors occur in command line applications?
为什么这些错误发生在命令行应用程序中?
8 个解决方案
#1
7
Regarding wkhtmltopdf:
关于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.
它的工作速度非常快,它还可以处理你扔给它的各种HTML/CSS,所以当你需要速度时,你应该认真考虑它。我们最近在我们公司改用它,我们的PDF服务得到了大量的加速。
-
At least under Linux it needs XOrg libraries to be installed - servers usually don't have them, so that might be your problem.
至少在Linux下,它需要安装XOrg库——服务器通常没有这些库,所以这可能是您的问题。
#2
2
Try this:
试试这个:
- tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples
- tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples
- fpdf.org
- fpdf.org
- http://code.google.com/p/wkhtmltopdf/
- http://code.google.com/p/wkhtmltopdf/
#4
0
But what if You will use any online service and send Your HTML content over HTTP? Of course most of them are not free.
但是,如果您使用任何在线服务并通过HTTP发送HTML内容,该怎么办呢?当然,大多数都不是免费的。
#5
0
One possibility: having the script automatically:
一种可能性是:自动拥有剧本:
- Take the web page
- 把网页
- Open that page in a web browser
- 在web浏览器中打开该页面
- Take a screencap of that page
- 对那一页做一个屏幕截图。
- Turn it into a PDF
- 把它变成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.)
第4步很简单——有很多PHP/cmdline库可以让您将图像放到pdf中或转换它们(例如,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?
步骤1 - 3…您可以尝试从这里查看代码:http://browsershots.org/。不确定它是否相关——它似乎需要很多的设置。也许他们的架构可以工作?
#6
0
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.
- 你真的需要把它转换成PDF吗?为什么?在某些情况下,最好坚持使用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.
- 升级生成PDF的服务器的硬件是一个选项吗?我这么问是因为,如果您尝试过的所有库创建起来都花费了太长的时间,那么您唯一的选择可能就是升级服务器。
- You might want to solve the problem with the command line error. If it gives the fastest results, then find a work around it.
- 您可能想用命令行错误来解决这个问题。如果它能给出最快的结果,那就找一个围绕它的工作。
#7
0
Try HTMLDOC commandline tool project https://www.msweet.org/projects.php?Z1
试试HTMLDOC commandline工具项目https://www.msweet.org/projects.php?Z1
#8
0
There are many solution to convert HTML to PDF, I can suggest you the one by https://grabz.it.
有很多方法可以将HTML转换成PDF,我可以建议你使用https://grabz.its。
The have a flexible PHP API which can be used by cronjobs or directly from PHP web page.
它有一个灵活的PHP API,可以由cronjobs使用,也可以直接从PHP web页面使用。
If you want to try it, at first you should get an app key + secret for authorization and the development free SDK
如果您想尝试它,首先您应该获得一个应用程序密钥+秘密授权和开发免费SDK
Here is an example of a basic implementation.
下面是一个基本实现的示例。
//First init
include("GrabzItClient.class.php");
// Create the GrabzItClient class
// Replace "APPLICATION KEY", "APPLICATION SECRET" values for your account!
$grabzIt = new GrabzItClient("Application Key", "Application Secret");
// To take a PDF screenshot
$grabzIt->URLToPDF("http://www.google.com");
// To save in case public callback handler is available
$grabzIt->Save("http://www.example.com/handler.php");
// OR To save in case public callback handler is not available,
// it's a synchonous method can be usedthe will force your application to wait
// while the screenshot is created
$filepath = "images/result.jpg";
$grabzIt->SaveTo($filepath);
It's possible to get other kinds of screenshots such as image screenshot and etc.
可以获得其他类型的屏幕截图,如图像截图等。
#1
7
Regarding wkhtmltopdf:
关于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.
它的工作速度非常快,它还可以处理你扔给它的各种HTML/CSS,所以当你需要速度时,你应该认真考虑它。我们最近在我们公司改用它,我们的PDF服务得到了大量的加速。
-
At least under Linux it needs XOrg libraries to be installed - servers usually don't have them, so that might be your problem.
至少在Linux下,它需要安装XOrg库——服务器通常没有这些库,所以这可能是您的问题。
#2
2
Try this:
试试这个:
- tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples
- tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf_examples
- fpdf.org
- fpdf.org
- http://code.google.com/p/wkhtmltopdf/
- http://code.google.com/p/wkhtmltopdf/
#3
#4
0
But what if You will use any online service and send Your HTML content over HTTP? Of course most of them are not free.
但是,如果您使用任何在线服务并通过HTTP发送HTML内容,该怎么办呢?当然,大多数都不是免费的。
#5
0
One possibility: having the script automatically:
一种可能性是:自动拥有剧本:
- Take the web page
- 把网页
- Open that page in a web browser
- 在web浏览器中打开该页面
- Take a screencap of that page
- 对那一页做一个屏幕截图。
- Turn it into a PDF
- 把它变成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.)
第4步很简单——有很多PHP/cmdline库可以让您将图像放到pdf中或转换它们(例如,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?
步骤1 - 3…您可以尝试从这里查看代码:http://browsershots.org/。不确定它是否相关——它似乎需要很多的设置。也许他们的架构可以工作?
#6
0
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.
- 你真的需要把它转换成PDF吗?为什么?在某些情况下,最好坚持使用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.
- 升级生成PDF的服务器的硬件是一个选项吗?我这么问是因为,如果您尝试过的所有库创建起来都花费了太长的时间,那么您唯一的选择可能就是升级服务器。
- You might want to solve the problem with the command line error. If it gives the fastest results, then find a work around it.
- 您可能想用命令行错误来解决这个问题。如果它能给出最快的结果,那就找一个围绕它的工作。
#7
0
Try HTMLDOC commandline tool project https://www.msweet.org/projects.php?Z1
试试HTMLDOC commandline工具项目https://www.msweet.org/projects.php?Z1
#8
0
There are many solution to convert HTML to PDF, I can suggest you the one by https://grabz.it.
有很多方法可以将HTML转换成PDF,我可以建议你使用https://grabz.its。
The have a flexible PHP API which can be used by cronjobs or directly from PHP web page.
它有一个灵活的PHP API,可以由cronjobs使用,也可以直接从PHP web页面使用。
If you want to try it, at first you should get an app key + secret for authorization and the development free SDK
如果您想尝试它,首先您应该获得一个应用程序密钥+秘密授权和开发免费SDK
Here is an example of a basic implementation.
下面是一个基本实现的示例。
//First init
include("GrabzItClient.class.php");
// Create the GrabzItClient class
// Replace "APPLICATION KEY", "APPLICATION SECRET" values for your account!
$grabzIt = new GrabzItClient("Application Key", "Application Secret");
// To take a PDF screenshot
$grabzIt->URLToPDF("http://www.google.com");
// To save in case public callback handler is available
$grabzIt->Save("http://www.example.com/handler.php");
// OR To save in case public callback handler is not available,
// it's a synchonous method can be usedthe will force your application to wait
// while the screenshot is created
$filepath = "images/result.jpg";
$grabzIt->SaveTo($filepath);
It's possible to get other kinds of screenshots such as image screenshot and etc.
可以获得其他类型的屏幕截图,如图像截图等。