Search code examples
pdf-generationpngfpdf

PNG Image not showing up in PDF


I properly encoded my PNG image. It is a Grayscale, 8 bit. The image is 5100(w) x 6600(h). MediaBox is 5100.0 6600.0.

Image:

3 0 obj
<<
/ProcSet [/PDF /ImageB]
/XObject <<
/Im8 8 0 R
>>
>>
endobj


8 0 obj
<<
/Type /XObject
/Subtype /Image
/BitsPerComponent 8
/ColorSpace /DeviceGray
/Height 6600
/Width 5100
/Filter /FlateDecode
/Length 590065
>>
stream
....zlib compressed stream

Image transformation:

9 0 obj
<</Length 54>>
stream
q
5100 0 0 6600 0 0 cm
/Im8 Do
Q
endstream
endobj

Page:

10 0 obj
<<
/Type /Page
/Parent 2 0 R
/CropBox [0.0 0.0 5100.0 6600.0]
/MediaBox [0.0 0.0 5100.0 6600.0]
/Resources 3 0 R
/Contents [  ]
>>
endobj

The image is not showing up. I verified pixel by pixel.

The stream consists of 33666600 (5100x6600 + 6600) bytes total. 6600 out of them indicate Predictor that was used. In my case it is /Predictor 13.

I write these 33666600 bytes, i.e. only compressed IDAT.

Perhaps coordinates? Compression? What am I missing here?


Solution

  • If you check ISO-32000-1 (the PDF standard), you'll discover that PNG isn't supported in PDF. You're using the /FlateDecode filter, which means you tell the PDF viewer that the image is stored inside the PDF file as a compressed bitmap. If you store a compressed PNG, the PDF viewer won't know how to render it. (update: Your most recent update indicates that you do store the image as a bitmap.)

    Also: in your page dictionary (object 10), you store the /Contents of the page as a PDF array, but that array is empty. It should be [ 9 0 R ]

    In your code sample, you also forgot to copy/paste the endstream keyword.