$image1 = "call.png";
$pdf->Cell(0,8,$pdf->Image($image1, $pdf->GetX()+2, $pdf->GetY()+2, 20.78),0,0,'',true);
Run Code Online (Sandbox Code Playgroud)
我尝试上面的代码在pdf文档的单元格中插入图像.我想将此图像对齐在单元格的右侧.
在FPDF中,分别有内部变量w和h宽度和高度.但是,这些以用户单位表示.默认情况下,这是毫米.图像总是以像素形式呈现,这可能会导致问题.也就是说,FPDF还可以跟踪wPt并hPt以一种非常容易与像素单元进行比较的方式为您提供尺寸.最后,还有k一个缩放单元,可用于将用户单元转换为像素.
我们将执行以下操作:
getimagesize$pdf->w$pdf->k向左填充从像素到用户单位转换$pdf->Cell$pdf->Image我们不会将图像放在单元格中.不要这样做.单元格不是为图像构建的,仅用于字符串.如果尝试此方法,则无法获得预期结果.相反,请选择上述方法.代码如下:
// Get image dimensions
$size = getimagesize('img.png');
if( $size===false )
die('Image does not exist.');
$wImg = $size[0];
$hImg = $size[1];
// Get PDF dimensions
$wPdf = $pdf->wPt;
$hPdf = $pdf->hPt;
// Calculate width necessary for the cell
$width = $wPdf - $wImg;
if( $width<0 )
{
error_log('Image is larger than page we\'re trying to print on.');
$width = 0;
}
// Convert pixel units to user units
$width /= $pdf->k;
$height /= $pdf->k;
// Print a boundary cell
$pdf->Cell($width,$height);
// Print image
$pdf->Image('img.png');
// Force a new line
$pdf->Ln();
Run Code Online (Sandbox Code Playgroud)
如果你计划使用它,我会建议将它放入一个函数或类似的东西.请注意,这仅适用于右对齐.您将使用不同的中心对齐计算来执行类似的方法.页面左侧和右侧的边界由FPDF定义,可以在构造函数中设置SetMargins