Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data extraction from /Filter /FlateDecode PDF stream in PHP

Tags:

php

pdf

decode

I can not decrypt the data from the stream like:

    56 0 obj      << /Length 1242 /Filter /FlateDecode >>     stream     x]êΩnƒ Ñ{ûbÀKq¬æ\âê¢....(whole binary is omitted)     endstream     endobj 

I tried isolate the binary content (x]êΩnƒ Ñ{ûbÀKq¬æ\âê¢....) in a file and in a binary string. Decoding function gzinflate($encripted_data) sends me error of decoding, and I think it happens because encoded content in not "deflated" or so.

In PDF Reference v 1.7, (six edition), on a page 67, I found the description of /FlateDecode filter as: ...Decompresses data encoded using the zlib/deflate compression method, reproducing the original text or binary data

I need real raw solution, aka php function or/and algorithm what to do with this "\FlateDecoded" stream.

Thank You!

like image 423
Ruben Kazumov Avatar asked Jul 31 '12 00:07

Ruben Kazumov


1 Answers

Since you didn't tell if you need to access one decompressed stream only or if you need all streams decompressed, I'll suggest you a simple commandline tool which does it in one go for the complete PDF: Jay Berkenbilt's qpdf.

Example commandline:

 qpdf --qdf --object-streams=disable in.pdf out.pdf 

out.pdf can then be inspected in a text editor (only embedded ICC profiles, images and fonts could still be binary).

qpdf will also automatically re-order the objects and display the PDF syntax in a normalized way (and telling you in a comment what the original object ID of the de-compressed object was).

Should you require to re-compress the file again (maybe after you edited it), just run this command:

 qpdf out-edited.pdf out-recompressed.pdf 

(You may see some warning message, telling that the utility was attempting to repair a damaged file....)

qpdf is multi-platform and available from Sourceforge.

like image 169
Kurt Pfeifle Avatar answered Sep 20 '22 05:09

Kurt Pfeifle