【问题标题】:TCPDF - Internal Server Error (500) on live server with large files onlyTCPDF - 仅具有大文件的实时服务器上的内部服务器错误 (500)
【发布时间】:2017-08-29 20:13:51
【问题描述】:

我在尝试渲染大文件时遇到了 TCPDF 问题:一个 66 页的 HTML 表,但是该问题仅在将脚本推送到实时服务器后才会发生。

我的localhost 将文件渲染完成,尽管速度很慢,但实时服务器最终返回 500 Internal Server Error。

据我所知,实际脚本没有任何问题,因为将 HTML 表的大小减少到 500 行会在几秒钟内返回 PDF。我的localhost 需要一分钟多的时间才能返回相同的 500 行,而需要 4 分钟才能返回完整的表格,所以我原以为实时服务器会做很短的工作,但它无法处理。

同样,PDF 的大容量内容是由一个脚本生成的,该脚本需要 30 多秒才能在我的 localhost 上呈现,而实时服务器会做很短的工作,并且可以在不到一秒的时间内返回结果,所以现场服务器的性能被证明是优越的(当然,正如您所期望的那样)。

我已经设置了,没有用:

ini_set("memory_limit", "-1");
ini_set('max_execution_time', 0);
set_time_limit(0);

服务器是共享主机,但我对它有很多控制权,如果我不能自己做,托管服务提供商愿意做出合理的改变。但是,我需要先查明原因,然后才能要求他们进行任何更改。

这并不理想,但即使文件需要很长时间才能完成,我也很乐意实施一种解决方法,以便文件在后台呈现,然后在完成后通过电子邮件向用户发送下载链接。但是,由于渲染中止,我什至不能这样做。

非常感谢任何帮助!

编辑:

在 cmets 中提醒之前我忘了提及:我在错误日志中什么也没得到。如果我从脚本中删除了一些关键的东西,例如 DB 连接器,那么该日志会按预期进行,但不会记录任何与 TCPDF 相关的内容。

附加信息:

我最初使用的是 Dompdf,但这无法在不失败的情况下渲染同一张表的 100 多行。这是在本地和实时服务器上进行的,这很遗憾,因为它很好地处理了 CSS 和 HTML,并且需要的内容定位工作要少得多。

【问题讨论】:

  • 你检查你的错误日志了吗?如果没有它,那将只是疯狂的猜测。
  • @Maerlyn 对不起,我忘了说:没有任何错误的痕迹!如果我从脚本中删除类似数据库连接器的东西,那么它当然会失败并出现错误,我可以看到这一点,但是在调用 TCPDF 类之后任何地方都没有。

标签: php html pdf tcpdf internal-server-error


【解决方案1】:

我遇到了同样的错误,并得出结论,问题是在为表格生成 HTML 并将其转换为 PDF 格式时形成的瓶颈。

首先,我将代码分成两个“部分”。我通过在外部运行查询并将结果保存到 JSON 文件来避免循环内生成数据行的复杂 MySql 查询。

然后读取我使用 multicell() 构建行和列的 JSON 文件

// Set MuliCell Defaults
$height = '5';
$border = '1';// 1/0 or L, T, R, B
$align = 'L';// L, C, R, J
$fill = '0'; //1/0
$Ln = '0';// 0=right, 1 = beginning of next line, 2=below
$floatX = '';
$floatY = '';
$resetH = 'true';


// Header
$pdf->SetFont('helvetica', 'n', 15);
$pdf->MultiCell('85', $height, 'SKU', $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('280', $height, 'Product', $border, $align, $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Price', $border, 'R', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Pk Size', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Min', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'Ord Lvl', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('68', $height, 'On Hnd', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('70', $height, 'Counted', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
$pdf->MultiCell('32', $height, 'Var', $border, 'C', $fill, $Ln, $floatX, $floatY, true);

// Set DataRows
$pdf->SetXY('13', '50');
$pdf->SetFont('helvetica', '', 10);
$pdf->SetTextColor(0,0,0);
$pdf->setCellPaddings(3, 3, 3, 3);

if (!empty($json))
    {
        $rows = '0'; // Counter for rows of processed data
        foreach($json as $item) //start loop for adding
            {
                $pdf->Ln(18);// Set a new line BEFORE each datarow
                $pdf->SetX('13');                       
                $pdf->MultiCell('85', $height, $item['sku_prd'], $border, $align, $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('280', $height, substr($item['name_prd'],0,60), $border, $align, $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('68', $height, $item['prefix_cur'].$item['sto_price'], $border, 'R', $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('68', $height, $item['sto_pack_size'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('68', $height, $item['sto_min'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('68', $height, $item['sto_reorder_level'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('68', $height, $item['sto_qty_on_hand'], $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('70', $height, '', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                $pdf->MultiCell('32', $height, '', $border, 'C', $fill, $Ln, $floatX, $floatY, true);   
                $rows++;// increment with 1 after each datarow

                if ($rows == 27) // Set max number of datarows per page
                    {
                        $pdf->AddPage();
                        $pdf->SetXY('13', '18');

                        // Header
                        $pdf->SetFont('helvetica', 'n', 15);
                        $pdf->MultiCell('85', $height, 'SKU', $border, $align, $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('280', $height, 'Product', $border, $align, $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('68', $height, 'Price', $border, 'R', $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('68', $height, 'Pk Size', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('68', $height, 'Min', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('68', $height, 'Ord Lvl', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('68', $height, 'On Hnd', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('70', $height, 'Counted', $border, 'C', $fill, $Ln, $floatX, $floatY, true);
                        $pdf->MultiCell('32', $height, 'Var', $border, 'C', $fill, $Ln, $floatX, $floatY, true);

                        // Reset font and rows to DataRows
                        $pdf->SetFont('helvetica', '', 10);
                        $pdf->SetXY('13', '25');
                        $rows = '0';// reset to "0" ready for the new page
                    }                                   
            }
    }

https://www.xms-systems.co.uk/article-262-tabular-data-with-tcpdf-and-internal-server-500-error.html

【讨论】:

  • 很久没有答案了,我忘了我问过这个!虽然有趣的解决方案。我考虑过使用 Multicell,但最终在坚持使用 html 的同时找到了另一种方法,这种方法有效并且还显着提高了速度。有机会我会发布我的解决方案。
  • 感谢分享,@JoeP。我们当然很欣赏这种“替代方法”......
  • @ner0 你有同样的问题吗?这个问题引起的兴趣太小,我什至忘记了它的存在。最终,如果没记错的话,我最终将表格逐行写入 TCPDF,并检查每一行之后的页面位置。如果它高于特定阈值,我插入一个新页面,编写新表头,并继续逐行写入并每次重复页面位置检查
  • @JoeP 感谢您的回复。看来您基本上按照 Fred 的建议实现了 MultiCell。主要区别似乎是 Fred 在您检查页面坐标以了解何时插入新页面时使用静态行数。
  • @ner0 没错,但我所做的一切都是在 HTML 中,所以我不得不在每个页面调用中重新声明 tbody 等
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-18
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2015-09-26
  • 2016-02-19
相关资源
最近更新 更多