3

I have tried something like

public function getPdf()
{
  $imagePath=C:\Users\Hp\Desktop\image.jpg;
  $image = Zend_Pdf_Image::imageWithPath($imagePath);
  $page->drawImage($image, 40,764,240, 820);
  $pdf->pages[] = $page;
  $pdf->save('new.pdf');
}

It generates PDF with image successfully. But it does not maintain quality of image. i.e height and width of the image get distorted and displays small image.

Can anyone help me in resolving these issues. I want that image displayed should be of full size on the page. Also please note that landscape or portrait images should fit in the page accordingly.

2 Answers 2

5

I suspect the other reason the image is coming out small is because you are drawing the image using pixels for the height and width of the drawing, but drawImage uses points which will make the image appear much smaller if you use the pixel measurements.

You can use the following code to convert the pixels to points before drawing the image:

$imgWidthPts  = $image->getPixelWidth() * 72 / 96;
$imgHeightPts = $image->getPixelHeight() * 72 / 96;
$x1           = 40;
$y1           = 764;

$page->drawImage($image, $x1, $y1, $x1 + $imgWidthPts, $y1 + $imgHeightPts);

Mihai's point about 300DPI is also valid so if you use a 72DPI image, it will appear a bit cloudy, but this should take care of drawing it at actual size.

0
2

For that to work you should consider the image quality, as an PDF's DPI is 300 or more, a JPEG used in websites or screen viewing has 72 DPI.

the PDF engine will always try to put all data in same size. That's why your image is distorsed and the quality is bad.

You can try to change the image resolution:

$image = substr_replace($image, pack("cnn", 1, 300, 300), 13, 5);
// replace the binary info with a new one, 300 DPI X resolution and 300 DPI Y resolution.
4
  • I have tried this but it throws error saying r Call to a member function drawImage() on a non-object
    – Nida
    Aug 25, 2012 at 9:03
  • I think you should recreate the image in a temp folder, not loading like that. Aug 25, 2012 at 9:08
  • I am not getting you. Can you please elaborate.
    – Nida
    Aug 25, 2012 at 9:20
  • read the contents from $image, apply my solution and save it to disk in a temp folder, then open it with ZEND Aug 25, 2012 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.