【问题标题】:Experiencing weird top margin when using fpdf Write()使用 fpdf Write() 时遇到奇怪的上边距
【发布时间】:2015-01-25 23:51:01
【问题描述】:

我正在尝试将文本写入 PDF,但页面顶部似乎有一个奇怪的边距。

这是我的以下代码:

require_once('fpdf.php');
require_once('fpdi/fpdi.php');

//Start the FPDI
$pdf = new FPDI('P', 'pt');

//Set the source PDF file
$source_file = $pdf->setSourceFile("template.pdf");

//Import the first page of the file
$tppl = $pdf->importPage(1);

$pdf->AddPage();

//get size of pdf page
$size = $pdf->getTemplateSize($tppl);

$pdf->useTemplate($tppl, null, null, $size['w'], $size['h'], true);
$pdf->SetMargins(0, 0, 0);
$pdf->SetTextColor(0, 0, 0); 

当我使用字体大小 pt 12 并编写文本时,我得到了:

$pdf->SetFont('Arial', '', 12);
$pdf->SetXY(0, 0);
$pdf->Write(0, "Hi");

当我 $pdf->SetXY(0, 7.5) 时,我明白了

上面看起来我可以很容易地在 Y 上加 7.5 分就可以了。

但是,如果我改变了字体大小,顶部和文本之间的距离会变大一些。

$pdf->SetFont('Arial', '', 8);

谁能帮我弄清楚如何中和它以至少使它成为这样,如果我将我的 XY 设置为一个数字,它将把它放在某个位置而不考虑字体大小?我尝试了不同的 pdf,但效果都一样。

编辑:

我做了$pdf->GetY(),我得到了28.35

【问题讨论】:

    标签: php pdf pdf-generation fpdf fpdi


    【解决方案1】:

    您只需将行高定义为零。因此,文本以 0 为中心垂直“居中”。

    常见的行高是:

    $pdf->Write($pdf->FontSize * 1.2, "Hi");
    

    【讨论】:

      【解决方案2】:

      我用Cell()代替Write()解决了这个问题。

      我认为主要问题是没有固定的宽度和高度。我所知道的是它现在完美工作了,所以遇到同样问题的任何人都应该试试这个。

      $pdf->Cell(WIDTH,HEIGHT,TEXT);
      

      我也做了以下,不确定这是否有帮助,但我的脚本中有它。

      $pdf->SetMargins(0, 0);
      $pdf->cMargin = 0;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-27
        • 2014-08-12
        • 1970-01-01
        • 1970-01-01
        • 2011-10-11
        • 2011-04-29
        • 2014-02-10
        • 1970-01-01
        相关资源
        最近更新 更多