好的,我有一个正在用 PHP 编写的 FPDF 文档,在此页面中,我使用 set x 和 Y 完美定位了一个徽标,并且效果很好。
我现在想要做的是在标题旁边添加一个图像,现在我可以再次用 x 和 y 定位它。问题是页面中的信息是动态的,因此设置 x 和 y 将意味着标题可能会移动但图像不会。
目前我有如下设置的图像和单元格,但标题总是位于图像下方的一行,我找不到它们坐在同一行上。
$pdf->Image('images/school.png');
$pdf->Cell(10,10,"Education",0,1,'L');
Run Code Online (Sandbox Code Playgroud)
不幸的是,FPDF 不知道如何在图像旁边浮动文本。但是,通常存在解决方法。下面的方法写一个浮动图像。请注意,您必须指定图像的高度。重写它应该很容易,但也可以让您指定宽度或不指定宽度。
class FloatPDF extends FPDF
{
public function floatingImage($imgPath, $height) {
list($w, $h) = getimagesize($imgPath);
$ratio = $w / $h;
$imgWidth = $height * $ratio;
$this->Image($imgPath, $this->GetX(), $this->GetY());
$this->x += $imgWidth;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个演示:
$pdf = new FloatPDF();
$imgPath = "/logo.png";
$pdf->SetFont(self::FONT, 'B', 20);
$height = 10;
$pdf->floatingImage($imgPath, $height);
$pdf->Write($height, " This is a text ");
$pdf->floatingImage($imgPath, $height);
$pdf->Write($height, " with floating images. ");
$pdf->floatingImage($imgPath, $height);
$pdf->Output('demo.pdf', 'D');
Run Code Online (Sandbox Code Playgroud)
这是演示的样子:

哦,顺便说一句,你也很难过,在调用$pdf->Image(). 一种简单的解决方法是将$y参数设置$pdf->Image()为$pdf->getY(). 如果$y未设置该参数,FPDF 会尝试提供帮助并默认进行换行。