Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.68 KB | None | 0 0
  1. <?php
  2.  
  3. function decodeAsciiHex($input) {
  4.     $output = "";
  5.  
  6.     $isOdd = true;
  7.     $isComment = false;
  8.  
  9.     for($i = 0, $codeHigh = -1; $i < strlen($input) && $input[$i] != '>'; $i++) {
  10.         $c = $input[$i];
  11.  
  12.         if($isComment) {
  13.             if ($c == '\r' || $c == '\n')
  14.                 $isComment = false;
  15.             continue;
  16.         }
  17.  
  18.         switch($c) {
  19.             case '\0': case '\t': case '\r': case '\f': case '\n': case ' ': break;
  20.             case '%':
  21.                 $isComment = true;
  22.             break;
  23.  
  24.             default:
  25.                 $code = hexdec($c);
  26.                 if($code === 0 && $c != '0')
  27.                     return "";
  28.  
  29.                 if($isOdd)
  30.                     $codeHigh = $code;
  31.                 else
  32.                     $output .= chr($codeHigh * 16 + $code);
  33.  
  34.                 $isOdd = !$isOdd;
  35.             break;
  36.         }
  37.     }
  38.  
  39.     if($input[$i] != '>')
  40.         return "";
  41.  
  42.     if($isOdd)
  43.         $output .= chr($codeHigh * 16);
  44.  
  45.     return $output;
  46. }
  47. function decodeAscii85($input) {
  48.     $output = "";
  49.  
  50.     $isComment = false;
  51.     $ords = array();
  52.    
  53.     for($i = 0, $state = 0; $i < strlen($input) && $input[$i] != '~'; $i++) {
  54.         $c = $input[$i];
  55.  
  56.         if($isComment) {
  57.             if ($c == '\r' || $c == '\n')
  58.                 $isComment = false;
  59.             continue;
  60.         }
  61.  
  62.         if ($c == '\0' || $c == '\t' || $c == '\r' || $c == '\f' || $c == '\n' || $c == ' ')
  63.             continue;
  64.         if ($c == '%') {
  65.             $isComment = true;
  66.             continue;
  67.         }
  68.         if ($c == 'z' && $state === 0) {
  69.             $output .= str_repeat(chr(0), 4);
  70.             continue;
  71.         }
  72.         if ($c < '!' || $c > 'u')
  73.             return "";
  74.  
  75.         $code = ord($input[$i]) & 0xff;
  76.         $ords[$state++] = $code - ord('!');
  77.  
  78.         if ($state == 5) {
  79.             $state = 0;
  80.             for ($sum = 0, $j = 0; $j < 5; $j++)
  81.                 $sum = $sum * 85 + $ords[$j];
  82.             for ($j = 3; $j >= 0; $j--)
  83.                 $output .= chr($sum >> ($j * 8));
  84.         }
  85.     }
  86.     if ($state === 1)
  87.         return "";
  88.     elseif ($state > 1) {
  89.         for ($i = 0, $sum = 0; $i < $state; $i++)
  90.             $sum += ($ords[$i] + ($i == $state - 1)) * pow(85, 4 - $i);
  91.         for ($i = 0; $i < $state - 1; $i++)
  92.             $ouput .= chr($sum >> ((3 - $i) * 8));
  93.     }
  94.  
  95.     return $output;
  96. }
  97. function decodeFlate($input) {
  98.     return @gzuncompress($input);
  99. }
  100.  
  101. function getObjectOptions($object) {
  102.     $options = array();
  103.     if (preg_match("#<<(.*)>>#ismU", $object, $options)) {
  104.         $options = explode("/", $options[1]);
  105.         @array_shift($options);
  106.  
  107.         $o = array();
  108.         for ($j = 0; $j < @count($options); $j++) {
  109.             $options[$j] = preg_replace("#\s+#", " ", trim($options[$j]));
  110.             if (strpos($options[$j], " ") !== false) {
  111.                 $parts = explode(" ", $options[$j]);
  112.                 $o[$parts[0]] = $parts[1];
  113.             } else
  114.                 $o[$options[$j]] = true;
  115.         }
  116.         $options = $o;
  117.         unset($o);
  118.     }
  119.  
  120.     return $options;
  121. }
  122. function getDecodedStream($stream, $options) {
  123.     $data = "";
  124.     if (empty($options["Filter"]))
  125.         $data = $stream;
  126.     else {
  127.         $length = !empty($options["Length"]) ? $options["Length"] : strlen($stream);
  128.         $_stream = substr($stream, 0, $length);
  129.  
  130.         foreach ($options as $key => $value) {
  131.             if ($key == "ASCIIHexDecode")
  132.                 $_stream = decodeAsciiHex($_stream);
  133.             if ($key == "ASCII85Decode")
  134.                 $_stream = decodeAscii85($_stream);
  135.             if ($key == "FlateDecode")
  136.                 $_stream = decodeFlate($_stream);
  137.         }
  138.         $data = $_stream;
  139.     }
  140.     return $data;
  141. }
  142. function getDirtyTexts(&$texts, $textContainers) {
  143.     for ($j = 0; $j < count($textContainers); $j++) {
  144.         if (preg_match_all("#\[(.*)\]\s*TJ#ismU", $textContainers[$j], $parts))
  145.             $texts = array_merge($texts, @$parts[1]);
  146.         elseif(preg_match_all("#Td\s*(\(.*\))\s*Tj#ismU", $textContainers[$j], $parts))
  147.             $texts = array_merge($texts, @$parts[1]);
  148.     }
  149. }
  150. function getCharTransformations(&$transformations, $stream) {
  151.     preg_match_all("#([0-9]+)\s+beginbfchar(.*)endbfchar#ismU", $stream, $chars, PREG_SET_ORDER);
  152.     preg_match_all("#([0-9]+)\s+beginbfrange(.*)endbfrange#ismU", $stream, $ranges, PREG_SET_ORDER);
  153.  
  154.     for ($j = 0; $j < count($chars); $j++) {
  155.         $count = $chars[$j][1];
  156.         $current = explode("\n", trim($chars[$j][2]));
  157.         for ($k = 0; $k < $count && $k < count($current); $k++) {
  158.             if (preg_match("#<([0-9a-f]{2,4})>\s+<([0-9a-f]{4,512})>#is", trim($current[$k]), $map))
  159.                 $transformations[str_pad($map[1], 4, "0")] = $map[2];
  160.         }
  161.     }
  162.     for ($j = 0; $j < count($ranges); $j++) {
  163.         $count = $ranges[$j][1];
  164.         $current = explode("\n", trim($ranges[$j][2]));
  165.         for ($k = 0; $k < $count && $k < count($current); $k++) {
  166.             if (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+<([0-9a-f]{4})>#is", trim($current[$k]), $map)) {
  167.                 $from = hexdec($map[1]);
  168.                 $to = hexdec($map[2]);
  169.                 $_from = hexdec($map[3]);
  170.  
  171.                 for ($m = $from, $n = 0; $m <= $to; $m++, $n++)
  172.                     $transformations[sprintf("%04X", $m)] = sprintf("%04X", $_from + $n);
  173.             } elseif (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+\[(.*)\]#ismU", trim($current[$k]), $map)) {
  174.                 $from = hexdec($map[1]);
  175.                 $to = hexdec($map[2]);
  176.                 $parts = preg_split("#\s+#", trim($map[3]));
  177.                
  178.                 for ($m = $from, $n = 0; $m <= $to && $n < count($parts); $m++, $n++)
  179.                     $transformations[sprintf("%04X", $m)] = sprintf("%04X", hexdec($parts[$n]));
  180.             }
  181.         }
  182.     }
  183. }
  184. function getTextUsingTransformations($texts, $transformations) {
  185.     $document = "";
  186.     for ($i = 0; $i < count($texts); $i++) {
  187.         $isHex = false;
  188.         $isPlain = false;
  189.         $hex = "";
  190.         $plain = "";
  191.         for ($j = 0; $j < strlen($texts[$i]); $j++) {
  192.             $c = $texts[$i][$j];
  193.             switch($c) {
  194.                 case "<":
  195.                     $hex = "";
  196.                     $isHex = true;
  197.                 break;
  198.                 case ">":
  199.                     $hexs = str_split($hex, 4);
  200.                     for ($k = 0; $k < count($hexs); $k++) {
  201.                         $chex = str_pad($hexs[$k], 4, "0");
  202.                         if (isset($transformations[$chex]))
  203.                             $chex = $transformations[$chex];
  204.                         $document .= html_entity_decode("&#x".$chex.";");
  205.                     }
  206.                     $isHex = false;
  207.                 break;
  208.                 case "(":
  209.                     $plain = "";
  210.                     $isPlain = true;
  211.                 break;
  212.                 case ")":
  213.                     $document .= $plain;
  214.                     $isPlain = false;
  215.                 break;
  216.                 case "\\":
  217.                     $c2 = $texts[$i][$j + 1];
  218.                     if (in_array($c2, array("\\", "(", ")"))) $plain .= $c2;
  219.                     elseif ($c2 == "n") $plain .= '\n';
  220.                     elseif ($c2 == "r") $plain .= '\r';
  221.                     elseif ($c2 == "t") $plain .= '\t';
  222.                     elseif ($c2 == "b") $plain .= '\b';
  223.                     elseif ($c2 == "f") $plain .= '\f';
  224.                     elseif ($c2 >= '0' && $c2 <= '9') {
  225.                         $oct = preg_replace("#[^0-9]#", "", substr($texts[$i], $j + 1, 3));
  226.                         $j += strlen($oct) - 1;
  227.                         $plain .= html_entity_decode("&#".octdec($oct).";");
  228.                     }
  229.                     $j++;
  230.                 break;
  231.  
  232.                 default:
  233.                     if ($isHex)
  234.                         $hex .= $c;
  235.                     if ($isPlain)
  236.                         $plain .= $c;
  237.                 break;
  238.             }
  239.         }
  240.         $document .= "\n";
  241.     }
  242.     return $document;
  243. }
  244.  
  245. function pdf2text($filename) {
  246.     $infile = @file_get_contents($filename, FILE_BINARY);
  247.     if (empty($infile))
  248.         return "";
  249.  
  250.     $transformations = array();
  251.     $texts = array();
  252.  
  253.     preg_match_all("#obj(.*)endobj#ismU", $infile, $objects);
  254.     $objects = @$objects[1];
  255. //var_dump($objects);
  256.     for ($i = 0; $i < count($objects); $i++) {
  257.         $currentObject = $objects[$i];
  258.  
  259.         if (preg_match("#stream(.*)endstream#ismU", $currentObject, $stream)) {
  260.             $stream = ltrim($stream[1]);
  261.             $options = getObjectOptions($currentObject);
  262. //var_dump($options, getDecodedStream($stream, $options));
  263.         if(isset($options['Image'])) {
  264. //var_dump($options);
  265.             $image = @imagecreatefromstring(getDecodedStream($stream, $options));
  266. if($image) {
  267. //var_dump($options);
  268. //var_dump(getDecodedStream($stream, $options));
  269.     imagejpeg($image, 'img'.$i.'.jpg');
  270. } else {var_dump($options);}
  271.         }
  272.             if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])))
  273.                 continue;
  274.  
  275.             $data = getDecodedStream($stream, $options);
  276.             if (strlen($data)) {
  277.                 if (preg_match_all("#BT(.*)ET#ismU", $data, $textContainers)) {
  278.                     $textContainers = @$textContainers[1];
  279.                     getDirtyTexts($texts, $textContainers);
  280.                 } else
  281.                     getCharTransformations($transformations, $data);
  282.             }
  283.         }
  284.     }
  285.  
  286.     return getTextUsingTransformations($texts, $transformations);
  287. }
  288.  
  289. echo pdf2text(<pfad zu pdf>);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement