我遇到了同样的错误,并得出结论,问题是在为表格生成 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