Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a left padding in a cell using FPDF in php?

I am printing a cell using the FPDF(http://www.fpdf.org/) class in php. The cell should be placed into the top left corner.

Everything works great, except that a left padding is added inside the cell.

Here is my code:

$pdf = new FPDF('L', 'mm', array(50.8,88.9));
$pdf->addPage('L', array(50.8,88.9));
$pdf->SetDisplayMode(100,'default');
$pdf->SetTextColor(0,0,0);
$pdf->SetMargins(0,0,0);    
$pdf->SetAutoPageBreak(0);
$pdf->SetFont('Arial','',8.5);

$pdf->SetXY(0, 0); //sets the position for the name
$pdf->Cell(0,2.98740833, "Your Name", '1', 2, 'L', false); //Name

Here's a screenshot of the PDF that is outputting with FPDF:

Screenshot of PDF output with FPDF

Why is there a left padding in a cell using FPDF in php and how can I remove the padding?

like image 265
zeckdude Avatar asked Jun 26 '11 01:06

zeckdude


2 Answers

I know this is super old, but I had and fixed the same issue so maybe someone will find it useful.

There's a property in the FPDF class called $cMargin, which is used to calculate the x-offset of the text before it gets printed within the cell, but there doesn't appear to be a setter for it. It's a public property, so after you've instantiated your FPDF class, just call:

$pdf = new fpdf('P','mm','A4');
$pdf->cMargin = 0;

And your cells won't have that padding on the left any more.

like image 96
Jasper Tandy Avatar answered Sep 25 '22 03:09

Jasper Tandy


Small fix updateproof

<?php
require("fpdf.php");

class CustomFPDF extends FPDF{
    function SetCellMargin($margin){
        // Set cell margin
        $this->cMargin = $margin;
    }
}
?>

<?php
$pdf = new CustomFPDF();
$pdf->setCellMargin(0);
?>
like image 36
Vince Avatar answered Sep 24 '22 03:09

Vince