2

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):

the image inserted into PDF

When I open the pdf in any browser (Chrome, FF, IE) it looks OK, but in Adobe PDF reader I get this:

screenshot from PDF reader

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.

lp1051
  • 491
  • 7
  • 19
  • 2
    I think these are just artifacts of smoothing algorithm of Adobe tools. – Bobrovsky Apr 24 '14 at 18:05
  • 2
    I see the same in Apple's Preview (admittedly, not the most robust PDF viewer). To be fair: after extracting the JPEG and viewing it at a reasonable size -- anything smaller than 100% -- Preview also shows artifacts. Perhaps it helps the renderer if you include a 1 or 2 white pixel border around it. – Jongware Apr 24 '14 at 21:18
  • Yes, thanks for the hint. I forgot to mention that adding 5px white border around the image fixed the problems in the top of S and A, however top and bottom of Y are still not good. Also I could reproduce problems in IE and Firefox when zooming, so I believe there is really problem in the size of embedded image, which is not proportional (doesn't respect aspect ratio) – lp1051 Apr 24 '14 at 21:29
  • No, the embedded image is *exactly* the same size; I extracted the JPEG for my test straight out of your PDF. Remember that a PDF cannot show a raster image at "100% on your screen"; it's *always* going to resample it, to fit 'required dimensions' inside the PDF back to 'scaled for *your* screen' (where "your" is a highly relative notion; my "screen at 100%" is different from yours). – Jongware Apr 24 '14 at 21:34
  • Now I'm not sure how do you mean it. When I try to extract the image I will get full-size 945x669, however if I zoom to 100% take a snapshot and measure the image size, it is not 168x120px. It is not really visible in the first example, but when using already scaled image, it gets stretched in PDF and also producing bad results. I will provide the example with already scale image ASAP. – lp1051 Apr 24 '14 at 21:39
  • *None* of the internal measurements inside a PDF are in 'pixels'; PDF is a "real world" data format. Your zooming to 100% scales to what *your viewer application* thinks is "100%" on your screen. If you hold a ruler to your screen to measure what should be a 5" line in any PDF, you may find it's off by any small (for good viewers) or larger (for lazy viewers) amount. – Jongware Apr 24 '14 at 21:59
  • Yes, but the coordinates are based on points. From Zend docs: *PDF uses the same geometry as PostScript. It starts from bottom-left corner of page and by default is measured in points (1/72 of an inch).* so I thought those points are in first place responsible for size of the image in PDF. If I don't use the image size, but instead use image size *0.66, I get much better results. So there must be something about the coordinates and original pixel size of the image I'm embedding – lp1051 Apr 24 '14 at 22:05
  • *There is a layout I need to follow, due to print.* - Do the artifacts also appear in print? Furthermore there seem to be gray pixels created by anti-aliasing. Have you tried without? – mkl Apr 25 '14 at 10:25
  • I updated question with the print results. But where do you find the gray pixels? In the full-size image or in the scaled one? Also I'm not sure how to prevent anti-aliasing or when? Do you mean when resizing the image or when drawing it into PDF? – lp1051 Apr 25 '14 at 11:48
  • 1
    You might also check the Preferences of your Adobe Acrobat/Reader. There is a setting where you can set the screen resolution. It is nowadays most likely something around 108 ppi. For an experiment, change it to 72 ppi, and look at the results. This would also explain why you get the better results by scaling your image by 0.66. A good compromise would be scaling the image (in an image processor) so that it would get a resolution of around 108 ppi at 100% view. – Max Wyss Apr 26 '14 at 22:22
  • Yes, thanks!! You're right Acrobat has its own PPI for display, it was 110ppi for me. Changing the default to 72ppi shows correct or expected size of image now. So my premise it was about image DPI or size is wrong. I can still experience artifacts in Acrobat and some browsers on some zoom level. Chrome is the only one that manages to display any PDF in any zoom level for me. So it seems @Bobrovsky was right. I guess forgetting JPG and supporting some vector format is the way to go, if one wants to both preview and print PDF with good results... – lp1051 Apr 28 '14 at 08:48

1 Answers1

0

If the word "say" is composed by characters, it depends on how the reader (brower reader, acrobat, foxit, etc..) render fonts.

Try to incorporate fonts into pdf to see if could helps.

zeppaman
  • 844
  • 8
  • 23
  • 1
    OP said it's an image and even provided a link to it. The issue seems unrelated to the image actually showing *text*. – Jongware Apr 24 '14 at 21:24
  • You are right, so I agree with @Bobrowsky, probally these are just artifacts, maybe related with resizing algotith of Zend. – zeppaman Apr 24 '14 at 21:31
  • Yes exactly, it is as @Jongware said. I'm showing example with image using only letters, but it's more for simplicity. The images can contain even some non-textual graphics, which I cannot really draw myself either, due to nature of the application. – lp1051 Apr 24 '14 at 21:32