Skip to content
PHP Logo

How can line breaks be added to text within a table cell when generating a PDF using a PHP PDF generator?

When generating a PDF using a PHP PDF generator, line breaks can be added to text within a table cell by using the "\n" character to create a new line. This character can be inserted into the text string wherever a line break is desired. Additionally, the cell height can be adjusted to accommodate the line breaks and ensure that the text is displayed correctly within the cell.

php
// Create a new PDF document
$pdf = new FPDF();

// Add a new page
$pdf->AddPage();

// Set font
$pdf->SetFont('Arial','',12);

// Add a table with cell containing text with line breaks
$pdf->Cell(50,10,"Line 1\nLine 2\nLine 3",1,1);

// Output the PDF
$pdf->Output();