I am using Zend PDF to generating the pdf file, I used below to create a image or logo in page top left,
$image = Zend_Pdf_Image::imageWithPath('my_image.jpg');
$pdfPage->drawImage($image, 100, 100, 400, 300);
But images shown in bottom left, when ever I increase or decrease the float value, only the image size will changes not move to top left corner.
Thanks!!
Trying to render images directly with Zend_Pdf
is frustrating, mainly because you have to provide the drawImage() function with the co-ordinates of all four corners of the area in which you want the image placed and remember that the co-ordinate system has its origin at the bottom left corner, not the top left corner. If you get the co-ordinates wrong you'll end up with images being rendered upside down, with the wrong aspect ratio, etc.
I have a wrapper that I use when dealing with Zend_Pdf
. I ripped the code below out of the wrapper and tried to adjust it so it would work stand-alone for you. I have not tested this code, but hopefully it still serves as a useful illustration of how to solve your problem.
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;
}