Zend_Pdf 使用 drawImage() 图像未移动到左上角

Zend_Pdf using drawImage() Image not moved to top left corner

我正在使用 Zend PDF 生成 pdf 文件, 我用下面的方法在页面左上角创建了一个图像或徽标,

$image = Zend_Pdf_Image::imageWithPath('my_image.jpg'); 
$pdfPage->drawImage($image, 100, 100, 400, 300);

但是左下角显示的图像,每当我增加或减少浮动值时,只有图像大小会改变,不会移动到左上角。

谢谢!!

尝试直接使用 Zend_Pdf 渲染图像令人沮丧,主要是因为您必须为 drawImage() 函数提供您想要放置图像的区域的所有四个角的坐标,并且请记住,坐标系的原点在左下角,而不是左上角。如果你弄错了坐标,你最终会得到颠倒渲染的图像,错误的长宽比等。

我有一个在处理 Zend_Pdf 时使用的包装器。我将下面的代码从包装器中取出并尝试对其进行调整,以便它可以独立运行。我没有测试过这段代码,但希望它仍然可以作为解决问题的有用示例。

function image( $page, $filename, $x_mm, $y_mm, $w_mm = 0 )
{
    $paperHeight = 297; // For this example, we're using a paper size of A4 in portrait

    $size = getimagesize( $filename );
    $width = $size[0];
    $height = $size[1];

    if ( $w_mm == 0 )
    {
        $w_mm = pointsToMm( $width );
    }

    $h_mm = $height / $width * $w_mm;

    $x1 = mmToPoints( $x_mm );
    $x2 = mmToPoints( $x_mm + $w_mm );
    $y1 = mmToPoints( $paperHeight - $y_mm - $h_mm );
    $y2 = mmToPoints( $paperHeight - $y_mm );

    $page->drawImage( Zend_Pdf_Image::imageWithPath( $filename ), $x1, $y1, $x2, $y2 );

    return $h_mm;
}

function pointsToMm( $points )
{
    return $points / 72 * 25.4;
}

function mmToPoints( $mm )
{
    return $mm / 25.4 * 72;
}