TCPDF - 如何使打印更快?非常非常慢,1320条记录花了40分钟
使用 Zend 框架和 TCPDF 打印 Test.pdf 需要 40 分钟。我现在不知道如何将这种不正常的时间缩短到正常的时间?
set_time_limit(0);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->setFontSubsetting(true);
$pdf->SetFont('dejavusans', '', 8, '', true);
$pdf->AddPage();
/* Database mysql gives the records and it is wrapped with <table> */
$html = "<table>1310 records.... with some simple <tr><td></td></tr></table>";
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$pdf->Output('Test.pdf', 'I');
exit;
后续:(调整性能)
1)php.ini: 内存限制 = 512M max_execution_time = 0
2) 编码 $pdf->setFontSubsetting(false); // true 到 false
3) 调试显示,花费整个时间
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border= 0、$ln=1、$fill=0、$reseth=true、$align=''、$autopadding=true);
Using Zend framework and TCPDF this is taking 40 minutes to print the Test.pdf. I having now no idea how to reduce this abnormal time to something normal?
set_time_limit(0);
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->setFontSubsetting(true);
$pdf->SetFont('dejavusans', '', 8, '', true);
$pdf->AddPage();
/* Database mysql gives the records and it is wrapped with <table> */
$html = "<table>1310 records.... with some simple <tr><td></td></tr></table>";
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
$pdf->Output('Test.pdf', 'I');
exit;
Follow up: (tune the performance)
1) php.ini:
memory_limit = 512M
max_execution_time = 0
2) Codeing
$pdf->setFontSubsetting(false); // true to false
3) Debug shows, following taking the whole time
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于没有其他答案,我强烈建议您仔细阅读 MPDF (GPL),因为它是比 TCPDF 快得多。我曾在一台服务器上进行过操作,使用 TCPDF 大约需要三分钟,而使用 MPDF 将其缩短到几秒。我只会假设我的 HTML 的某种格式 -> PDF 遇到了 TCPDF 中一些低效的功能。
无论如何,我提供了以下代码来帮助我转换 HTML -> PDF。
此代码提供了作为可下载文件输出的 PDF,MPDF 文档提供了许多其他示例来满足您的需求。
Since no other answers have been forthcoming, I would highly recommend a good look at MPDF (GPL) as it's much faster than TCPDF. I've had operations on one server that took approx three minutes using TCPDF being reduced to seconds with MPDF. I would only assume that some format of my HTML -> PDF was hitting some inefficient function in TCPDF.
Anyway I present the following code which helped me convert HTML -> PDF.
This code provides a PDF outputted as a downloadable file, the MPDF documentation gives lots of other examples to suit your needs.
我们使用
mpdf
主要是因为我可以“包含”它而无需安装。有一些小的调整需要添加到 php.ini,我认为是 mbstring。
通过缩小输入
WriteHTML
动词的 HTML,我能够将每分钟 75 页提高到每分钟 100 页。我们没有任何图形。然后,我使用
ghostscript
对每个接收 PDF 的人进行排序和分类(大约 15 页)。对于在一个目的地接收报告的 50 或 60 人来说,每 800-1000 页的猫处理过程大约需要 3 分钟。所有这些都在一个盒子里,大部分都放在那里没有其他任何东西。
We use
mpdf
mainly because I can just "include" it with no installation.There was some minor tweak that needed to be added to php.ini, mbstring I think.
I was able to get 75 pages a minute up to 100 pages per minute by shrinking the HTML I was feeding the
WriteHTML
verb. We do not have any graphics.Then I use
ghostscript
to sort and cat pdfs (15 or so pages) per person receiving them. That cat process takes about 3 minutes per 800-1000-pages for the 50 or 60 people receiving a report at one destination.All this on a box mostly sitting there for nothing else.
TCPDF 性能缓慢的原因之一可能是从外部 URL 插入的图像。 DNS 解析和文件下载需要时间并减慢 PDF 生成过程。
One of the causes of the TCPDF slow performance could be images inserted from external URLs. DNS resolution and file downloading take time and slow down the PDF generation process.