使用 For 循环将字符串插入 PDF

Insert string to PDF with For loop

我是初级 PHP 开发人员。 对于客户,我需要将一些字符串放入 pdf 中。我正在使用 FPDI,我喜欢它。 我有一个现有的 PDF 模板,我需要将字符串的每个字符插入到一个小图形框中(参见图片)。

每个字符之间必须有 2 毫米(8px 大约)。

每个字符串可以有不同的长度,所以我想这样做:

$name = 'namenamename';
$stringcount = strlen($name)-1;
$countspace = $stringcount*2;
//121 = coordinate of first box
for ($x=121; $x <= $x+$countspace; $x = $x+2) {
  for ($i=0; $i <= $stringcount; $i++) {        
    $pdf->SetXY($x, 37);
    $pdf->Write(0,$name[$i]);
  }
}

那是行不通的。这是错误:

Maximum execution time of 30 seconds

你能帮我用正确的方法和对新手的很好的解释吗? :)

也许不是很好的解决方案,但您可以使用这行代码修改执行时间

set_time_limit ( $秒数 );

无论如何试一试,但我认为这可能是循环逻辑中的一个错误。

你能准确地说出你需要前两个字符的坐标吗,第一个是 121 + something 还是 121?

试试这个代码:



    $name = 'namenamename';
$string_length = strlen($name);

$coordinate = 121; //Give to the variable coordinate the beginning value, in this case 121
for ($i=0; $i < $string_length; $i++){ //make only one loop for the string length so the loop ends when there is no more characters


    $char = substr($name,$i,1); // this is "the tricky part", with substr you can grab each character with its position in the string
    $pdf -> SetXY($coordinate, 37); // here you put the coordinate for the character
    $pdf -> Write(0, $char); // write it
    $coordinate += 2; // and increment it by two, since the character are two spaces away from each other
}

希望对您有所帮助..