I was wondering if we could change the file type from png, jpeg to pdf using only coding. So in order to do that, I first have to figure out what exactly makes a pdf file different from other files.
I was wondering if we could change the file type from png, jpeg to pdf using only coding. So in order to do that, I first have to figure out what exactly makes a pdf file different from other files.
You can write an image.PDF file as plain text by adding a header and trailer, so here is a simple mono image as seen by the simple f and 0 NOTE a color image would be more varied. so each black pixel is 000000 and each white one is FFFFFF
%PDF-1.7
%µ¶
1 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<<
/Type /Pages
/Count 1
/Kids [ 5 0 R ]
>>
endobj
3 0 obj
<<
/Length 2683981
/Type /XObject
/Subtype /Image
/Width 744
/Height 592
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/Filter /ASCIIHexDecode
>>
stream
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
....
Dozens of lines later
ffffffffffffffffffffffffffffffffff000000ffffffffffffffffffffffff
ffffffffffffffffffffffffffffff000000000000000000000000ffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000
....
Thousands of lines later
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
>
endstream
endobj
4 0 obj
<<
/Length 43
>>
stream
q
557.93026 0 0 443.9445 0 0 cm
/Img3 Do
Q
endstream
endobj
5 0 obj
<<
/Type /Page
/MediaBox [ 0 0 557.93026 443.9445 ]
/Rotate 0
/Resources <<
/XObject <<
/Img3 3 0 R
>>
>>
/Contents 4 0 R
/Parent 2 0 R
>>
endobj
xref
0 6
0000000000 65536 f
0000000016 00000 n
0000000070 00000 n
0000000136 00000 n
0002684310 00000 n
0002684406 00000 n
trailer
<<
/Size 6
/Info <<
/Producer (SumatraPDF 3.4.14236)
>>
/Root 1 0 R
>>
startxref
2684589
%%EOF
This would be very bloated for publication so a source crisp.PNG or fuzzy.peg is more normally imported and zip style "DeFlated" ("FlateDecode" re-encrypted as binary)
The source PDF generated by SumatraPDF was only 23082 bytes as shown below, but for this question it was deliberately expanded to show the internal workings however the image can be compressed back down to binary by any PDF library back to
3 0 obj
<</DecodeParms<<>>/Type/XObject/Subtype/Image/Width 744/Height 592/BitsPerComponent 8/ColorSpace/DeviceRGB/Length 23082/Filter/FlateDecode>>
stream
xœíÙ–Ûº®Eóÿ?íûpƨëm‰àBÃNšó)±ÑAX®T>..............
NOTE an image does NOT have a PNG identity NOR DPI once placed in a PDF (PDFs are NOT reversible)