CodeIgniter3中FPDF的Cell与MultiCell发票排版问题求助
Solution for FPDF Invoice Text Wrapping and Row Height Alignment
To fix text overflow in the "Full Name" and "Course Name" columns while keeping all row heights consistent, we calculate the required height for these wrapping columns, then use MultiCell for them while aligning other cells to the same row height. Here's the modified code:
Modified Table Content Loop
// 表格内容 $pdf->Cell(10,8,'',0,1); $pdf->SetFont('Arial','',8)->SetTextColor(0, 0, 0)->SetDrawColor(181, 181, 181); // 定义列宽方便引用 $colWidths = [23, 40, 45, 22, 33, 8, 24]; $lineHeight = 8; // 与原Cell高度保持一致 foreach ($detail as $row) { $id_nomor_student = $row['id_nomor_student']; $course = $this->db->query("SELECT * FROM tb_student_universitas JOIN tb_universitas ON tb_student_universitas.universitas_tujuan = tb_universitas.code_uni WHERE id_nomor_student='$id_nomor_student' ")->result_array(); $fullName = $row['nama_student']; $courseName = $course[0]['universitas']; // 计算需要换行的列所需的行数 $linesFullName = ceil($pdf->GetStringWidth($fullName) / $colWidths[1]); $linesCourseName = ceil($pdf->GetStringWidth($courseName) / $colWidths[2]); $maxLines = max($linesFullName, $linesCourseName); $rowHeight = $lineHeight * $maxLines; // 记录当前行的起始位置 $startX = $pdf->GetX(); $startY = $pdf->GetY(); // Passport No 列 $pdf->Cell($colWidths[0], $rowHeight, $row['nomor_passpor_student'], 1, 0, 'C'); // Full Name 列(使用MultiCell实现换行) $pdf->SetXY($startX + $colWidths[0], $startY); $pdf->MultiCell($colWidths[1], $lineHeight, $fullName, 1, 'C'); // Course Name 列(使用MultiCell实现换行) $pdf->SetXY($startX + $colWidths[0] + $colWidths[1], $startY); $pdf->MultiCell($colWidths[2], $lineHeight, $courseName, 1, 'C'); // Intake 列 $pdf->SetXY($startX + $colWidths[0] + $colWidths[1] + $colWidths[2], $startY); $pdf->Cell($colWidths[3], $rowHeight, date('F Y', strtotime($row['intake'])), 1, 0, 'C'); // Tuition Fee / Commission 列 $pdf->SetXY($startX + $colWidths[0] + $colWidths[1] + $colWidths[2] + $colWidths[3], $startY); $pdf->Cell($colWidths[4], $rowHeight, $row['tuition_fee_commission'], 1, 0, 'C'); // % 列 $pdf->SetXY($startX + $colWidths[0] + $colWidths[1] + $colWidths[2] + $colWidths[3] + $colWidths[4], $startY); $pdf->Cell($colWidths[5], $rowHeight, $row['persen'], 1, 0, 'C'); // Total Commission 列 $pdf->SetXY($startX + array_sum(array_slice($colWidths, 0, 6)), $startY); $pdf->Cell($colWidths[6], $rowHeight, $row['total_commission'], 1, 1, 'L'); // 将光标定位到当前行末尾,准备下一行 $pdf->SetY($startY + $rowHeight); }
Key Changes Explained:
- Row Height Calculation: Use
GetStringWidthto determine how many lines the text in wrapping columns will occupy, then set the row height to match the tallest column in the row. - MultiCell for Wrapping:
MultiCellautomatically wraps text while maintaining cell borders, solving the overflow issue. - Cursor Alignment: After each
MultiCell(which shifts the cursor down), useSetXYto reset the cursor position to the start of the next column, ensuring all cells in the row align correctly. - Consistent Height: All cells in the row use the calculated
rowHeight, so shorter columns stretch to match the height of wrapping columns.
Performance Note:
The database query inside the foreach loop runs once per row, which is inefficient for large datasets. Consider joining this query with your initial $detail query to fetch all necessary data in a single call.
内容的提问来源于stack exchange,提问作者Edwin Nick
相关产品推荐
相关产品推荐

