Make text wrap in a cell with FPDF?
Text Wrap:
The MultiCell
is used for print text with multiple lines. It has the same atributes of Cell
except for ln
and link
.
$pdf->MultiCell( 200, 40, $reportSubtitle, 1);
Line Height:
What multiCell does is to spread the given text into multiple cells, this means that the second parameter defines the height of each line (individual cell) and not the height of all cells (collectively).
MultiCell(float w, float h, string txt [, mixed border [, string align [, boolean fill]]])
You can read the full documentation here.
Wrap Text in Fpdf in Php
Use MultiCell()
instead Cell()
Change this:
$pdf->Cell(20,7,'Hi5(xtra)',1);
To:
$pdf->MultiCell( 20, 7, 'Hi5(xtra)', 1);
The MultiCell() is used for print text with multiple lines.
EDIT:
I can see that MultiCell()
, breaks the line so new cell will be placed below current position.
In such case you can calculate x
and y
co-ordinate and calculate new position and set position after outputting every cell.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$start_x=$pdf->GetX(); //initial x (start of column position)
$current_y = $pdf->GetY();
$current_x = $pdf->GetX();
$cell_width = 20; //define cell width
$cell_height=7; //define cell height
$pdf->SetFont('Arial','',16);
$pdf->MultiCell($cell_width,$cell_height,'Hi1',1); //print one cell value
$current_x+=$cell_width; //calculate position for next cell
$pdf->SetXY($current_x, $current_y); //set position for next cell to print
$pdf->MultiCell($cell_width,$cell_height,'Hi2',1); //printing next cell
$current_x+=$cell_width; //re-calculate position for next cell
$pdf->SetXY($current_x, $current_y); //set position for next cell
$pdf->MultiCell($cell_width,$cell_height,'Hi3',1);
$current_x+=$cell_width;
$pdf->Ln();
$current_x=$start_x; //set x to start_x (beginning of line)
$current_y+=$cell_height; //increase y by cell_height to print on next line
$pdf->SetXY($current_x, $current_y);
$pdf->MultiCell($cell_width,$cell_height,'Hi4',1);
$current_x+=$cell_width;
$pdf->SetXY($current_x, $current_y);
$pdf->MultiCell($cell_width,$cell_height,'Hi5(xtra)',1);
$current_x+=$cell_width;
$pdf->SetXY($current_x, $current_y);
$pdf->MultiCell($cell_width,$cell_height,'Hi5',1);
$current_x+=$cell_width;
$pdf->SetXY($current_x, $current_y);
$pdf->Output();
?>
Related Topics
How to Do File Upload Using Jquery Serialization
How to Remove a Directory That Is Not Empty
How to Fetch Record of Many Ids on Single Query
How to Pass Variable Between Two Web Pages With PHP Without Using Session
Getting Error in Console:Failed to Load Resource: Net::Err_Connection_Reset
How to Get All Month Record Count in Laravel
How to Get Previous Month from Selected Month
How to Add Auto Numbering With Each Fetching Row
How to Send Data Using Redirect With Laravel
Check If Class Property Is Not Null Before Calling Any Public Class Method
How to Save a Png Image Server-Side, from a Base64 Data Uri
Laravel Array to String Conversion
Merging Two Json by Adding Their Value
How to Remove a Key from an Object Not Array in Laravel
Codeigniter Cors Policy: No 'Access-Control-Allow-Origin' Error How to Resolve