确定FPDF模板中的文本位置

问题描述

我是fpdf的菜鸟,我想知道是否有一种方法可以预先确定在文档中放置文本的位置。

例如 我想将交易日期放在模板中的“日期”之后。

我现在正在使用的脚本是这样的

$nomeFile = date("YmdHis").".pdf";
$pathTmp = "path/to/template.pdf";
$pathTmp1 = "path/to/result.pdf";
$pdf = new Pdf($pathTmp);
$pdf = new TcpdfFpdi();
$pdf->AddPage();
$pdf->setSourceFile($pathTmp);
$template = $pdf->importPage(1);
$pdf->useTemplate($template);
$pdf->SetAutopageBreak(false);
$pdf->Text(78,70,'1');
$pdf->Text(38,78,'2');
$pdf->Text(45,87,'3');
$pdf->Text(47,113,'4');
$pdf->Text(65,145,'5');
$pdf->Output($pathTmp1,"F");

因此,基本上,放置随机值,看看哪一个离我的标签最近,但是这种方式太浪费时间了

解决方法

使用GetY函数查找您在行中的位置,然后使用SetY函数定位文本的下一行。

您可能会发现SetXY函数在需要时也可用于绝对定位。

这是一段代码,该代码创建具有适当偏移量的地址块,以使所有内容对齐。

$ourpdf->SetFont('Arial','B',11);
$to_offset = $ourpdf->GetStringWidth('To:') + 27;
$ourpdf->Cell(0,4,'To:',0);
$ourpdf->SetFont('Arial','',10);
$ourpdf->SetXY($to_offset,75);
$ourpdf->Cell(0,$coop_data['name'],0);
$ourpdf->SetXY($to_offset,79);
$ourpdf->Cell(0,$coop_data['contact'],1);
$ourpdf->SetXY($to_offset,83);
$ourpdf->Cell(0,$coop_data['address1'],1);