Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top