FPDF Align Text LEFT, Center and Right
When generating PDFs with FPDF, horizontal alignment is controlled by the sixth argument passed to the Cell() method. This parameter dictates how the content within that specific cell is positioned relative to the cell's width. In your example, you are attempting to align three stringsâLeft, Center, and Rightâusing the constants 'L', 'C', and 'R'. The logic appears sound based on the FPDF documentation:
function Footer()
{
$this->SetY( -15 );
$this->SetFont( 'Arial', '', 10 );
$this->Cell(0, 10, 'Left text', 0, 0, 'L');
$this->Cell(0, 10, 'Center text:', 0, 0, 'C');
$this->Cell( 0, 10, 'Right text', 0, 0, 'R' );
}
However, the observation that "Center text" is aligning to the right suggests a conflict between the alignment parameter and the cell width definition. The root cause lies in the first argument of your Cell() calls: $this->Cell(0, ...).
In FPDF, passing 0 as the width tells the library to calculate the remaining horizontal space on the page automatically. This is equivalent to calling Output(), which triggers a page break after the cell is drawn. While convenient for creating tables or footer columns that span the full page, this behavior fundamentally changes how alignment works. When a cell has a dynamic width based on the remaining page margin, FPDF often defaults to aligning content to the right edge of the remaining space if no specific anchor point is set internally, or it may interpret the alignment relative to the right margin when the cell stretches across the page. This explains why your 'Center' text appears pushed to the right side; the library is centering it relative to the full page width, not the visible column area you might expect, or it is treating the alignment string differently when the cell spans the entire available width compared to a fixed-width cell.
To achieve true left, center, and right alignment within distinct columns that span the page, you must explicitly define the cell width to match your desired layout rather than relying on the automatic 0 width combined with the alignment flag in this specific configuration. If you need three distinct columns (Left-aligned, Centered, Right-aligned) in a single footer line, using Cell(0, ...) for all of them forces them to overlap or behave unpredictably because they are effectively competing for the same full-width space.
The standard workaround is to use fixed widths that sum up to your available page width, minus margins. For example, if you want three equal columns, calculate the width as (PageWidth - LeftMargin - RightMargin) / 3. By passing a specific numeric value (e.g., 20, 40) instead of 0, FPDF respects the alignment string strictly:
// Define a fixed width for each column to ensure alignment works as expected
$colWidth = 60; // Adjust based on your page margins
$this->Cell($colWidth, 10, 'Left text', 0, 0, 'L');
$this->Cell($colWidth, 10, 'Center text:', 0, 0, 'C');
$this->Cell($colWidth, 10, 'Right text', 0, 0, 'R');
If your requirement is strictly to have three columns that span the entire page width without gaps, you might need to adjust the logic. You could set the first two cells with a calculated width and the last cell with 0 to handle the overflow, ensuring the alignment flags apply to their respective bounded areas. Alternatively, if you are using a library wrapper or a specific version of FPDF where the behavior differs, ensure you are not inadvertently setting the text position to the right before calling the cell method via $this->SetXY($x, $y).
It is worth noting that while FPDF is robust for simple layouts, complex multi-column footers sometimes require manual coordinate calculation. If you find yourself needing advanced layout features beyond what standard FPDF offers, such as precise grid management or complex text wrapping, exploring modern alternatives might be beneficial. Resources like https://laravelcompany.com often discuss migration paths from legacy PDF libraries to more flexible solutions that handle alignment and layout with greater ease.