编程语言
首页 > 编程语言> > 在PHP中通过pdf文件定位文本

在PHP中通过pdf文件定位文本

作者:互联网

我正在开发一个电子证书网页.
我已经设法加载了证书模板并在其上书写(与会者名称,事件标题和事件日期).
但是在定位这三段信息时,无论它们多长时间,我都无法将它们定位在中心.他们总是从x点开始.

检查我的工作结果:

enter image description here

码:

<?php
require_once('fpdf.php');
require_once('fpdi.php');

$pdf =& new FPDI();
$pdf->addPage('L');
$pagecount = $pdf->setSourceFile('Certificate.pdf');
$tplIdx = $pdf->importPage(1); 
$pdf->useTemplate($tplIdx); 
$pdf->SetFont('Times','I',18);
$pdf->SetTextColor(0,0,0); 
$pdf->SetXY(120, 62); 
$pdf->Write(0, "Attendee Name"); 

$pdf->SetXY(120, 102); 
$pdf->Write(0, "Event Name"); 

$pdf->SetXY(120, 140); 
$pdf->Write(0, "Event Date"); 
$pdf->Output();
?>

文本有多长?

意思是不固定x点.但是,y点的结果非常好.

解决方法:

所以我以前做过的2种方法,最可能是最简单的方法:
由于您在该行上只有1个条目(与会者名称或事件名称等)
在最左边的位置开始一个单元,并使该单元运行页面的整个宽度,然后指定中心选项:

$pdf->SetXY(0,62);
//0 extends full width, $height needs to be set to cell height.
$pdf->Cell(0, $height, "Attendee Name", 0, 0, 'C');

另一个选择是计算中心,并相应地设置X:

//how wide is the page?
$midPtX = $pdf->GetPageWidth() / 2;
//now we need to know how long the write string is
$attendeeNameWidth = $pdf->GetStringWidth($attendeeName);
//now we need to divide that by two to calculate the shift
$shiftLeft = $attendeeNameWidth / 2;
//now calculate our new X value
$x = $midPtX - $shiftLeft;
//now apply your shift for the answer
$pdf->setXY($x, 62); 
$pdf->Write(0, "Attendee Name"); 

然后,您可以对其他每个元素重复上述操作.

标签:fpdf,php
来源: https://codeday.me/bug/20191111/2023162.html