【PHP】TCPDF-5 TCPDFでMulticellの行の高さを合わせる

TCPDFで表を作ろうとすると、文字数がオーバーした場合に、高さが自動調整されて、各列の高さがずれてしまいます。


$items = array(
['item_name' => '美味しいバナナジュース','unit_price' => 300,'amount' => 4, 'total' => 1200],
['item_name' => '甘くて美味しいちょっと酸味のある高級なパイナップルジュース果汁100%','unit_price' => 620,'amount' => 3, 'total' => 1860],	
);

$cellBorder = array(
'R' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
'L' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
'T' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
'B' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
);

$pdf->MultiCell(150, 0, '商品名', $cellBorder, 'L', false, 0, '', '', true, 0);
$pdf->MultiCell(80, 0, '単価', $cellBorder, 'L', false, 0, '', '', true, 0);
$pdf->MultiCell(80, 0, '数量', $cellBorder, 'L', false, 0, '', '', true, 0);
$pdf->MultiCell(0, 0, '合計', $cellBorder, 'L', false, 1, '', '', true, 0);

foreach ($items as $each) {
    $pdf->MultiCell(150, 0, $each['item_name'], $cellBorder, 'L', false, 0, '', '', true, 0);
    $pdf->MultiCell(80, 0, $each['unit_price'], $cellBorder, 'L', false, 0, '', '', true, 0);
    $pdf->MultiCell(80, 0, $each['amount'], $cellBorder, 'L', false, 0, '', '', true, 0);
    $pdf->MultiCell(0, 0, $each['total'], $cellBorder, 'L', false, 1, '', '', true, 0);
}


PDFサンプル(高さずれNG例)

以下のロジックで高さを計算し、調整することで行の高さを合わせることができます。
$pdfをクローンした$pdf2でレンダリングし、その高さを取得し行の高さを指定します。(Multicellの第二引数に指定する)


$items = array(
['item_name' => '美味しいバナナジュース','unit_price' => 300,'amount' => 4, 'total' => 1200],
['item_name' => '甘くて美味しいちょっと酸味のある高級なパイナップルジュース果汁100%','unit_price' => 620,'amount' => 3, 'total' => 1860],	
);

$cellBorder = array(
'R' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
'L' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
'T' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
'B' => array('width' => 0.1, 'cap' => 'square', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)),
);

// 高さ計算用
$pdf2 = clone $pdf;
  
$pdf->MultiCell(150, 0, '商品名', $cellBorder, 'L', false, 0, '', '', true, 0);
$pdf->MultiCell(80, 0, '単価', $cellBorder, 'L', false, 0, '', '', true, 0);
$pdf->MultiCell(80, 0, '数量', $cellBorder, 'L', false, 0, '', '', true, 0);
$pdf->MultiCell(0, 0, '合計', $cellBorder, 'L', false, 1, '', '', true, 0);

foreach ($items as $each) {
    // セル高さ計算
    $height0 = $pdf2->getY();
    $pdf2->MultiCell(150, 0, $each['item_name'], $cellBorder, 'L', false, 1, '', '', true, 0);
    $height1 = $pdf2->getY();
    $rowHeight = $height1 - $height0;
			
    $pdf->MultiCell(150, $rowHeight, $each['item_name'], $cellBorder, 'L', false, 0, '', '', true, 0);
    $pdf->MultiCell(80, $rowHeight, $each['unit_price'], $cellBorder, 'L', false, 0, '', '', true, 0);
    $pdf->MultiCell(80, $rowHeight, $each['amount'], $cellBorder, 'L', false, 0, '', '', true, 0);
    $pdf->MultiCell(0, $rowHeight, $each['total'], $cellBorder, 'L', false, 1, '', '', true, 0);
}


PDFサンプル(高さ調整済み)

TCPDF(Gitub)はこちら