php 为什么在php中使用FPDF的单元格中有左填充?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6481632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is there a left padding in a cell using FPDF in php?
提问by zeckdude
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.
我正在使用php 中的 FPDF( http://www.fpdf.org/) 类打印单元格。单元格应放置在左上角。
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:
这是使用 FPDF 输出的 PDF 的屏幕截图:
Why is there a left padding in a cell using FPDF in php and how can I remove the padding?
为什么在 php 中使用 FPDF 的单元格中有左填充,如何删除填充?
回答by Jasper Tandy
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:
FPDF 类中有一个名为 $cMargin 的属性,用于在文本在单元格中打印之前计算文本的 x 偏移,但似乎没有设置器。这是一个公共属性,因此在实例化 FPDF 类后,只需调用:
$pdf = new fpdf('P','mm','A4');
$pdf->cMargin = 0;
And your cells won't have that padding on the left any more.
并且您的单元格将不再有左侧的填充。
回答by Vince
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);
?>
回答by Jim
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.
作为一种解决方法,知道它似乎是 1mm 是有用的,而不管字体大小。右对齐文本在右边缘应用相同的填充。
回答by steve
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(..);
回答by Mahdi.Montgomery
Have you tried running SetMargins(0,0)
?
你试过跑步SetMargins(0,0)
吗?
SetMargins
设置边距
SetMargins(float left, float top [, float right])
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.
描述
定义左边距、上边距和右边距。默认情况下,它们等于 1 厘米。调用这个方法来改变它们。
回答by Dhea
Use SetMargins before AddPage
在 AddPage 之前使用 SetMargins
example:
例子:
$pdf=new PDF();
$pdf->SetMargins(23, 44, 11.7);
$pdf->AliasNbPages();
$pdf->AddPage();