I'm having trouble understanding and therefore also fixing following problem. I'm creating dynamic PDF using drawings, fonts and images using Zend framework (Zend_Pdf). The problem is with images. I'm embedding JPG, with no compression, but it is always corrupted in Adobe PDF Reader. I tried using different image DPI, I scaled and stored the image before embedding, or I kept the original size and only scaled it when embedding into PDF, but the results are the same. I don't really understand what's wrong or what else could I try, so I will be grateful for any help.
The image inserted into PDF (original is here):
When I open the pdf in any browser (Chrome, FF, IE) it looks OK, but in Adobe PDF reader I get this:
I marked the places that are corrupted. The test.pdf can be seen here. Can anybody help me understand the issue?
EDIT
After further debugging, this seems to be connected to image size and Zend_Pdf::drawImage()
method.
The expected size of image in PDF should be something like 168x120px based on the coordinates I provide, however the actual size inside PDF is much bigger. It's about 35% difference.
I believe that's why the image is corrupted, but I still don't understand what coordinates I should use in order to display the desired pixel size... Basically I'm using this pattern:
$page->drawImage($image, $x, ($y - 120), ($x + 168), $y);
Is it a wrong way of calculating the coordinates?
EDIT 2:
I'm adding also example of already scaled image being embedded into PDF using its actual size (168x120px), to show that the image size inside PDF is different, or at least seems to not have the expected size, though I'm using the code above. The scaled image is here and the final pdf is here. This scaled image already had the white border prior resize, so the result looks better.
EDIT 3:
To clarify more about the desired pixel size of image inside PDF. There is a layout I need to follow, due to print. The layout specification is in mm, and I have exact areas where the image(s) must be positioned and max width/height. I.e. max width is 100mm, which I calculated as 280px in 72ppi document, similarily with height (42mm = 119px). There is also requirement, that embedded images must be 300 DPI. And I think there might be the problem, but I still don't understand how should I recalculate the width/height in points for PDF? Also is it recommended to embed large image and draw it smaller, or use scaled image and draw in its actual size? I saw a little better results on screen with images using 72DPI, so is it possible I will need to create 2 PDF files, one only for print and one only for screen, or browser?
EDIT 4 (print):
I tested with print in 300 and 600 DPI. The PDF using full size image being scaled when drawing it in PDF (this one) is OK in both cases. The scaled.pdf is very bad in 300 DPI print (even some red color is visible at edges). It is a quite OK in 600 DPI print, but is extremely blurry, due to stretching. And the stretching is caused by wrong point size of the image when drawing, as I mentioned earlier.