Search code examples
pdf-generationjpeg

Generate PDF File With Image


I am trying to generate a PDF file manually, line by line. I want to put an image into it but it doesn't work out to just put in the image stream, there also is a compressed stream at the beginning of the file, which seems to be necessary.

Sample PDF File with image:

%PDF-1.4
%âãÏÓ
1 0 obj
<</Type/Page/MediaBox[0 0 2048 1365]/Resources<</XObject 6 0 R>>/Contents 2  0 R/ArtBox[0 0 2048 1365]/CropBox[0 0 2048 1365]/Parent 8 0 R>>
endobj
2 0 obj

<</Filter/FlateDecode/Length 53>>
stream

THE STREAM THAT I AM TALKING ABOUT

endstream
endobj 

3 0 obj
<</D[1 0 R/FitH 1370.000]/S/GoTo>>
endobj
5 0 obj
<</Type/XObject/Subtype/Image/ColorSpace/DeviceRGB/Filter/DCTDecode/Width 2048/Height 1365/Length 623547/BitsPerComponent 8>>
stream

...

JPEG DATA HERE

...

endstream
endobj

...


So, what is that stream and how to create it? thanks


Solution

  • Look at object 1:

    1 0 obj
    <</Type/Page/MediaBox[0 0 2048 1365]/Resources<</XObject 6 0 R>>/Contents 2  0 R/ArtBox[0 0 2048 1365]/CropBox[0 0 2048 1365]/Parent 8 0 R>>
    endobj
    

    It represents a page (/Type/Page) and references object 2 as its contents (/Contents 2 0 R). So your mystery stream contains the description of what is drawn on the page modeled in object 1!

    Content streams are not necessarily compressed but due to the verbosity of the PDF content description instructions they usually can be compressed quite well.

    In such a stream there can be quite a variety of instructions for setting states (colors, transformations, transparency, ...) and drawing text/bitmaps/vector graphics as determined by those settings.

    In your case the contents stream should at least contain something like

    2048 0 0 1365 0 0 cm
    /Im0 Do
    

    Im0 being the name you associate with your image resource in object 6.


    That all being said, instead of using pure trial and error, you should simply read the PDF specification ISO 32000-1.