8

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?

zeckdude
  • 15,877
  • 43
  • 139
  • 187
  • 1
    You really should checkout [TCPDF](http://www.tcpdf.org/). I don't believe FPDF is being maintained anymore. – Jared Farrish Jun 26 '11 at 04:33
  • I took your advice and created the same page with TCPDF and I am now getting a top and left padding within the cell. I have posted the code and screenshots here: http://stackoverflow.com/questions/6483326/why-is-there-a-left-and-top-padding-in-a-cell-using-tcpdf-in-php – zeckdude Jun 26 '11 at 10:12
  • My apologies, I wasn't saying it would necessarily fix the issue, just that TCPDF is a more modern and maintained package than FPDF. I used to use FPDF, but have found TCPDF to be more useful. – Jared Farrish Jun 26 '11 at 14:19

6 Answers6

16

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.

Jasper Tandy
  • 335
  • 2
  • 8
8

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);
?>
Vince
  • 81
  • 1
  • 1
  • A small aside, the cell margin has no effect if the cell is set to Center alignment – kurdtpage Nov 15 '16 at 22:21
  • This is the only correct way to fix this issue with the latest version of FPDF, as cMargin is now a protected property. Thank you for the great fix @Vince! – schmoove Sep 21 '22 at 18:02
2

I can't work out how to remove the padding.

As a workaround, it is useful to know that it seems to be 1mm, regardless of font size. The same padding is applied at the right edge with right aligned text.

Jim
  • 1,053
  • 9
  • 14
2

i've ran into the same problem. Only the 1st line has this unwanted margin, so my workaround was this:

  $pdf->Ln(); //workaround for 1st line
  $pdf->Cell(..);
steve
  • 615
  • 6
  • 14
1

Use SetMargins before AddPage

example:

$pdf=new PDF();
$pdf->SetMargins(23, 44, 11.7);
$pdf->AliasNbPages();
$pdf->AddPage();
Dhea
  • 11
  • 2
1

Have you tried running SetMargins(0,0)?

SetMargins

SetMargins(float left, float top [, float right])

Description

Defines the left, top and right margins. By default, they equal 1 cm. Call this method to change them.

http://www.fpdf.org/en/doc/setmargins.htm

Mahdi.Montgomery
  • 2,024
  • 4
  • 17
  • 21
  • Thank you for the tip, but I am actually using that function already. I had not listed it here because I didn't think it was important. I have now updated all the functions I am using so that you have a better idea. – zeckdude Jun 26 '11 at 04:32
  • 1
    This function is for setting the margin for the entire page, not each cell. – kurdtpage Nov 15 '16 at 22:19