TCPDF 优化性能

发布于 2024-11-16 13:35:45 字数 1160 浏览 3 评论 0原文

我做了很多搜索和测试。我的任务是优化 HTML 的 PDF 生成。

我的代码是:

<?php
define('K_TCPDF_EXTERNAL_CONFIG', true); 

require('static_config.php');
require("hipero_pdf.class.php");

$pdf = new hipero_TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

$pdf->SetHeaderData(PDF_HEADER_IMAGE, PDF_HEADER_IMAGE_WIDTH);
$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->SetFont('dejavusans', '', 10);

$pdf->AddPage();

$pdf->SetTextColor(0, 0, 0);

$pdf->writeHTML(PDF_SYNEO_HTML, true, false, true, false, '');
$pdf->Output('../../Content/Offers/Generated/'.PDF_SYNEO_FILENAME.'.pdf');
?>

我的 HTML 有点乱,但测试表明这对性能几乎没有影响。

每和平代码花费不到 1 sek。待解析。只是这需要更长的时间:

$pdf->AddPage - 3 sek。

$pdf->writeHTML - 9 瑞典克朗。

$pdf->输出 - 5 秒。

这是在 IIS 服务器(共享主机)上。

请告诉我如何优化这个方法。我没有更多的想法:/。

I've done a lot of searching and testing. My task is to optimize PDF generation form HTML.

My code is:

<?php
define('K_TCPDF_EXTERNAL_CONFIG', true); 

require('static_config.php');
require("hipero_pdf.class.php");

$pdf = new hipero_TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);

$pdf->SetHeaderData(PDF_HEADER_IMAGE, PDF_HEADER_IMAGE_WIDTH);
$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->SetFont('dejavusans', '', 10);

$pdf->AddPage();

$pdf->SetTextColor(0, 0, 0);

$pdf->writeHTML(PDF_SYNEO_HTML, true, false, true, false, '');
$pdf->Output('../../Content/Offers/Generated/'.PDF_SYNEO_FILENAME.'.pdf');
?>

My HTML is a bit messy, but test shows this is have almost no impact on performance.

Every peace of code take less than 1 sek. to be parsed. Only this take longer:

$pdf->AddPage - 3 sek.

$pdf->writeHTML - 9 sek.

$pdf->Output - 5 sek.

This is on IIS server (shared hosting).

Please tell me how to optimize this methods. I have no more ideas :/.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

肩上的翅膀 2024-11-23 13:35:45

在 TCPDF 网站上,有一个页面建议尽可能避免使用 writeHTML。除此之外,它还建议避免使用大的 HTML 块。
http://www.tcpdf.org/performances.php 上有更多内容,他们解释了您可以关闭其他选项以使渲染速度更快。

At TCPDF website, there's a page which advises avoiding the use of writeHTML if possible. Other than that, it also advises avoiding use big HTML blocks.
There's more at http://www.tcpdf.org/performances.php where they explain you can turn off additional options to make rendering faster.

迷迭香的记忆 2024-11-23 13:35:45

我还遇到了 TCPDF 性能问题。我遵循 http://www.tcpdf.org/performances.php 引用的指南。我可以从中得到什么:

  • writeHTML() 代替 cell() 产生了很大的差异,但仅限于已经很短的时间。对于不超过 5 秒的片段,它所用的时间除以 5。但除此之外,并没有多大区别。
  • 相反,把大块切成小块对我来说非常有效。我尝试将大块切成小块,似乎最佳的块是每块 5000 个字符左右。它使我能够将 pdf 生成时间从 60 秒缩短到 20 秒,在我的服务器上缩短到 7 秒。

希望有帮助!

I also had an issue with TCPDF performance. I followed the guidelines cited at http://www.tcpdf.org/performances.php. What I could take from it :

  • writeHTML() made a big difference instead of cell(), but only for already short times. It divided by 5 the time taken for pieces that didn't take more than 5 seconds. But above that, it didn't make much of a difference.
  • on the contrary, cutting big pieces in small worked really well for me. I tried to cut big pieces into smaller ones, and it seemed that the optimal was around 5000 characters per piece. It enabled me to bring a pdf generation from 60 seconds to 20 seconds, and 7 seconds on my server.

Hope it helps !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文