Home > Enterprise >  Convert png/jpeg to pdf 1.0 using ASCII85Encode and DCTEncode
Convert png/jpeg to pdf 1.0 using ASCII85Encode and DCTEncode

Time:04-28

I have been asked to modify a php script (written ages ago and running on a server) that outputs complex pdf 1.0 files, so that png and jpeg images could be added to the layout.

Since I don't have neither the time nor the budget to re-write that script with a pdf API, I thought I'd try to use Imagick to convert those images into pdf, and then insert the code in the pdf 1.0 files. The code is pretty straightforward :

$images = array('myfile.png');
$pdf = new Imagick($images);
$pdf->setImageFormat('pdf');
$pdf->writeImages('myfile.pdf', true);

But the problem is that Imagick produces pdf 1.4 which makes it impossible to insert in the pdf 1.0.

Ideally, the pdf 1.0 code for a jpeg should be as follows :

10 0 obj
<<
/Type /XObject
/Subtype /Image
/Name /I1 
/Width 41 
/Height 32 
/BitsPerComponent 8 
/ColorSpace /DeviceRGB
/Length 11 0 R
/Filter [ /ASCII85Decode /DCTDecode ]
/DecodeParms [ null null ]
>>
stream
s4IA>!"M;*Ddm8XA,lT0!!3,S!/(=^#mq"I#n7=Q%MTB`&/6#u'GM3"-RKoP
 !`0d0J n0/M].F4Z>5W3&*K\5s[k-9he>U6qpBM9he>V9he=_%M''a&JQQ-
( (jp/2T1X9gM'>9he>V9he>V9he>V9he>V9he>V9he>V9he>V9he>V9he>V
9he>V9he>Vs1eUH#QPtI.0BS_!!3`5!tbS6_uZS4!!*6(!<E3%!<<*"z
!rr?'"9eu7#RLhG!<<-(!<E3%!<E0#z!!*'$!sAc3#7(VC$P3:="9AT 
"9J`2"pbG9%:K8;!YGM; VHL55uD&(,&r/h"r*2nYsKZ\'iJKs1r43a6O p#
#ZK.?@rjLCiM,kJK-j!M< JG7UNAC1(t)FDAb*0\_p`bgo0t*lUkQ1@`73l?
V7":mjn2YdG(u<[[`6n\p,>KCB6T,tVmj^ukP$Aa86BPMLmY-NaOo_O#oP0P
8QfbQM4(?Rak>qS$5tBT8m5tUMOLQVb1c.W&HDk6!<NB,!sA`2#R1M<B)qu6
&Ha12&d1Kt@<!J)"YtXk'VVcu,Jd86d3S3jiGsO56W4_0F#50I(@e-)K*N_\
#f9!XP>n:nA49KVFCjJ&Z\66FFlW'_Pba#?Q,M25oVJt7e`HI)Ap/opVRFLq
k4U/]7os>ILR4pJa4KMK(aq@7=D2r8R&IO9f]`):)(@R;=_W/<RAma=g$/;>
)Cdd?>&&A@R]<sAg?SP7h#IQX"97'T$j-M1!YGMH!'^JNqTi0JV2B,5/ 1D&
qY&K_UhL'P,TcprL_clM-<<$9%D*\4h-7C1%"o=_,sVFVoNUaa*h di_3NU"
k;_r28P"/>FaZaaS@\$l[TWuQD%PouR$5i@aWmOL8La\eoR[)TFTj'NH!aTe
AZj,=2nPqt*do^$hu mArd5r=d8#&f\R]0_p_c&NG>`4q?rq,4:FE\m52Uqh
4!#,@s3g/P5gS_5hq%As9&pS@O"I;X:G2YI4]AMl76M#^f!I<-!!E9]!AuC'
VuP6<Jj9rj!@9kjDc -LB" [[X5(pBaihl BF!r_ge%#Xj\Di3%[A,;qE6u%
fIb'NU&P*d4&Y u:G^pUG!a>cs4$9R?W?m12q$l3%[Pc';nn=4Di7ES1jFID
8p5n-4U*N5)0ki36cG<m2`8L_9hJBLrrE)Lms:m,VlA\bH_4e_g`59W[dO=i
:Q\$`;LD_cHqG>Q4`.4-E7P9q"XQ`9s46gqPfB/,VRG)T)d=9!=9&Ha&lUWO
rrE)P~>
endstream
endobj

So I thought I could encode straight the png/jpeg file using ASCII85Encode and DCTEncode to produce the code to encapsulate in pdf 1.0, but my php skills aren't good enough... So before I start some research, I'd like to know if it's the way to go. And I will appreciate any advice on that matter.

CodePudding user response:

As mentioned there is no need to use encoded streams the streams can be imported in many ways such as here using MS Notepad text. KenS may recognise the MuPDF signature used for some manipulation/conversion.

  • Obj 4 is the scaled size of 5x5.
  • Obj 6 is the 75 image pixels without encoding as compressed binary.
  • Obj 7 is the related mask (not always needed).

The critical point is the decimal positions especially length are good enough to be valid if slightly off as some readers may not accept invalid.

%PDF-1.0
%µ¶

1 0 obj
<</Type/Catalog/Pages 2 0 R>>
endobj

2 0 obj
<</Type/Pages/Count 1/Kids[3 0 R]>>
endobj

3 0 obj
<</Type/Page/MediaBox[0 0 3.75 3.75]/Rotate 0/Resources<</XObject<</Img3 6 0 R>>>>/Contents 4 0 R/Parent 2 0 R>>
endobj

4 0 obj
<</Length 34>>
stream
q
3.75 0 0 3.75 0 0 cm
/Img3 Do
Q

endstream
endobj

6 0 obj
<</Length 153/Type/XObject/Subtype/Image/Width 5/Height 5/BitsPerComponent 8/SMask 7 0 R/ColorSpace/DeviceRGB/Filter/ASCIIHexDecode>>
stream
ff2020ffffff20a020ffffff2020ffffffffffffffffffffffffffffffffff20
ffffffff404040ffffff20ffffffffffffffffffffffffffffffffff2020ffff
ffffffff20ffffff202020>
endstream
endobj

7 0 obj
<</Length 11/Type/XObject/Subtype/Image/Width 5/Height 5/BitsPerComponent 1/ColorSpace/DeviceGray/Filter/ASCIIHexDecode>>
stream
ffffffffff>
endstream
endobj

xref
0 8
0000000004 65536 f 
0000000016 00000 n 
0000000062 00000 n 
0000000114 00000 n 
0000000000 00002 f 
0000000243 00000 n 
0000000326 00000 n 
0000000647 00000 n 

trailer
<</Size 8/Info<</Producer(Me)>>/Root 1 0 R>>
startxref
814
%%EOF

enter image description here

CodePudding user response:

Thank you both for your help. I managed to insert png/jpeg images in the pdf as follows :

// converts png to jpg and removes transparency
$image = new Imagick('img.png');
$image->setImageBackgroundColor('white');
$image = $image->flattenImages();
$image->writeImage('img.jpg');

// converts jpg to bmp
$image = array('img.jpg');
$image = new Imagick($image);
$image->setImageFormat('bmp');
$image->writeImages('img.bmp', true);

// converts bmp to pdf
$image = array('img.bmp');
$image = new Imagick($image);
$image->setImageFormat('pdf');
$image->writeImages('img.pdf', true);

Then I get pdf code of my image in the format "/Filter [ /ASCII85Decode ]" that I can select and insert into the existing pdf 1.0.

Best,

  • Related