为什么在php中使用FPDF的单元格中有左边的填充?

zec*_*ude 7 php pdf pdf-generation fpdf

我正在使用php中的FPDF(http://www.fpdf.org/)类打印一个单元格.单元格应放在左上角.

一切都很好,除了在单元格内添加左边距.

这是我的代码:

$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
Run Code Online (Sandbox Code Playgroud)

以下是使用FPDF输出的PDF的屏幕截图:

使用FPDF输出PDF的屏幕截图

为什么在php中使用FPDF的单元格中有左边的填充?如何删除填充?

小智 16

我知道这是超级老,但我已经修复了同样的问题,所以也许有人会发现它很有用.

FPDF类中有一个名为$ cMargin的属性,它用于计算文本在单元格中打印之前的x偏移量,但似乎没有为它设置setter.它是一个公共属性,因此在您实例化FPDF类之后,只需调用:

$pdf = new fpdf('P','mm','A4');
$pdf->cMargin = 0;
Run Code Online (Sandbox Code Playgroud)

而你的细胞将不再有左边的填充物.


小智 6

小修复更新

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

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

<?php
$pdf = new CustomFPDF();
$pdf->setCellMargin(0);
?>
Run Code Online (Sandbox Code Playgroud)


Mah*_*ery 1

你尝试过跑步SetMargins(0,0)吗?

设置边距

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

描述

定义左边距、上边距和右边距。默认情况下,它们等于 1 厘米。调用此方法来更改它们。

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