Advertisement
tugapt

class.pdf2text.php

Apr 5th, 2014
46,235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.62 KB | None | 0 0
  1. <?php
  2. /*
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7.  
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  15.  
  16. This code is an improved version of what can be found at:
  17. http://www.webcheatsheet.com/php/reading_clean_text_from_pdf.php
  18.  
  19. Related information:
  20. http://www.adobe.com/devnet/acrobat/pdfs/PDF32000_2008.pdf
  21.  
  22. AUTHOR:
  23. - Webcheatsheet.com (Original code)
  24. - Joeri Stegeman (joeri210 [at] yahoo [dot] com) (Class conversion and fixes/adjustments)
  25.  
  26. DESCRIPTION:
  27. This is a class to convert PDF files into ASCII text or so called PDF text extraction.
  28. It will ignore anything that is not addressed as text within the PDF and any layout.
  29. Currently supported filters are: ASCIIHexDecode, ASCII85Decode, FlateDecode
  30.  
  31. HISTORY:
  32. v1.01 2014-02-03: Fixed some word concatenation issues
  33. v1.00 2010-04-17: Initial release of PDF2Text
  34.  
  35. PURPOSE(S):
  36. Most likely for people that want their PDF to be searchable.
  37.  
  38. SYNTAX:
  39. include('class.pdf2text.php');
  40. $a = new PDF2Text();
  41. $a->setFilename('test.pdf');
  42. $a->decodePDF();
  43. echo $a->output();
  44.  
  45. ALTERNATIVES:
  46. Other excellent options to search within a PDF:
  47. - Apache PDFbox (http://pdfbox.apache.org/). An open source Java solution
  48. - pdflib TET (http://www.pdflib.com/products/tet/)
  49. - Online converter: http://snowtide.com/PDFTextStream
  50. */
  51.  
  52. class PDF2Text {
  53.     // Some settings
  54.     var $multibyte = 4; // Use setUnicode(TRUE|FALSE)
  55.     var $convertquotes = ENT_QUOTES; // ENT_COMPAT (double-quotes), ENT_QUOTES (Both), ENT_NOQUOTES (None)
  56.     var $showprogress = true; // TRUE if you have problems with time-out
  57.  
  58.     // Variables
  59.     var $filename = '';
  60.     var $decodedtext = '';
  61.  
  62.     function setFilename($filename) {
  63.         // Reset
  64.         $this->decodedtext = '';
  65.         $this->filename = $filename;
  66.     }
  67.  
  68.     function output($echo = false) {
  69.         if($echo) echo $this->decodedtext;
  70.         else return $this->decodedtext;
  71.     }
  72.  
  73.     function setUnicode($input) {
  74.         // 4 for unicode. But 2 should work in most cases just fine
  75.         if($input == true) $this->multibyte = 4;
  76.         else $this->multibyte = 2;
  77.     }
  78.  
  79.     function decodePDF() {
  80.         // Read the data from pdf file
  81.         $infile = @file_get_contents($this->filename, FILE_BINARY);
  82.         if (empty($infile))
  83.             return "";
  84.  
  85.         // Get all text data.
  86.         $transformations = array();
  87.         $texts = array();
  88.  
  89.         // Get the list of all objects.
  90.         preg_match_all("#obj[\n|\r](.*)endobj[\n|\r]#ismU", $infile . "endobj\r", $objects);
  91.         $objects = @$objects[1];
  92.  
  93.         // Select objects with streams.
  94.         for ($i = 0; $i < count($objects); $i++) {
  95.             $currentObject = $objects[$i];
  96.  
  97.             // Prevent time-out
  98.             @set_time_limit ();
  99.             if($this->showprogress) {
  100. //              echo ". ";
  101.                 flush(); ob_flush();
  102.             }
  103.  
  104.             // Check if an object includes data stream.
  105.             if (preg_match("#stream[\n|\r](.*)endstream[\n|\r]#ismU", $currentObject . "endstream\r", $stream )) {
  106.                 $stream = ltrim($stream[1]);
  107.                 // Check object parameters and look for text data.
  108.                 $options = $this->getObjectOptions($currentObject);
  109.  
  110.                 if (!(empty($options["Length1"]) && empty($options["Type"]) && empty($options["Subtype"])) )
  111. //              if ( $options["Image"] && $options["Subtype"] )
  112. //              if (!(empty($options["Length1"]) &&  empty($options["Subtype"])) )
  113.                     continue;
  114.  
  115.                 // Hack, length doesnt always seem to be correct
  116.                 unset($options["Length"]);
  117.  
  118.                 // So, we have text data. Decode it.
  119.                 $data = $this->getDecodedStream($stream, $options);
  120.  
  121.                 if (strlen($data)) {
  122.                     if (preg_match_all("#BT[\n|\r](.*)ET[\n|\r]#ismU", $data . "ET\r", $textContainers)) {
  123.                         $textContainers = @$textContainers[1];
  124.                         $this->getDirtyTexts($texts, $textContainers);
  125.                     } else
  126.                         $this->getCharTransformations($transformations, $data);
  127.                 }
  128.             }
  129.         }
  130.  
  131.         // Analyze text blocks taking into account character transformations and return results.
  132.         $this->decodedtext = $this->getTextUsingTransformations($texts, $transformations);
  133.     }
  134.  
  135.  
  136.     function decodeAsciiHex($input) {
  137.         $output = "";
  138.  
  139.         $isOdd = true;
  140.         $isComment = false;
  141.  
  142.         for($i = 0, $codeHigh = -1; $i < strlen($input) && $input[$i] != '>'; $i++) {
  143.             $c = $input[$i];
  144.  
  145.             if($isComment) {
  146.                 if ($c == '\r' || $c == '\n')
  147.                     $isComment = false;
  148.                 continue;
  149.             }
  150.  
  151.             switch($c) {
  152.                 case '\0': case '\t': case '\r': case '\f': case '\n': case ' ': break;
  153.                 case '%':
  154.                     $isComment = true;
  155.                 break;
  156.  
  157.                 default:
  158.                     $code = hexdec($c);
  159.                     if($code === 0 && $c != '0')
  160.                         return "";
  161.  
  162.                     if($isOdd)
  163.                         $codeHigh = $code;
  164.                     else
  165.                         $output .= chr($codeHigh * 16 + $code);
  166.  
  167.                     $isOdd = !$isOdd;
  168.                 break;
  169.             }
  170.         }
  171.  
  172.         if($input[$i] != '>')
  173.             return "";
  174.  
  175.         if($isOdd)
  176.             $output .= chr($codeHigh * 16);
  177.  
  178.         return $output;
  179.     }
  180.  
  181.     function decodeAscii85($input) {
  182.         $output = "";
  183.  
  184.         $isComment = false;
  185.         $ords = array();
  186.  
  187.         for($i = 0, $state = 0; $i < strlen($input) && $input[$i] != '~'; $i++) {
  188.             $c = $input[$i];
  189.  
  190.             if($isComment) {
  191.                 if ($c == '\r' || $c == '\n')
  192.                     $isComment = false;
  193.                 continue;
  194.             }
  195.  
  196.             if ($c == '\0' || $c == '\t' || $c == '\r' || $c == '\f' || $c == '\n' || $c == ' ')
  197.                 continue;
  198.             if ($c == '%') {
  199.                 $isComment = true;
  200.                 continue;
  201.             }
  202.             if ($c == 'z' && $state === 0) {
  203.                 $output .= str_repeat(chr(0), 4);
  204.                 continue;
  205.             }
  206.             if ($c < '!' || $c > 'u')
  207.                 return "";
  208.  
  209.             $code = ord($input[$i]) & 0xff;
  210.             $ords[$state++] = $code - ord('!');
  211.  
  212.             if ($state == 5) {
  213.                 $state = 0;
  214.                 for ($sum = 0, $j = 0; $j < 5; $j++)
  215.                     $sum = $sum * 85 + $ords[$j];
  216.                 for ($j = 3; $j >= 0; $j--)
  217.                     $output .= chr($sum >> ($j * 8));
  218.             }
  219.         }
  220.         if ($state === 1)
  221.             return "";
  222.         elseif ($state > 1) {
  223.             for ($i = 0, $sum = 0; $i < $state; $i++)
  224.                 $sum += ($ords[$i] + ($i == $state - 1)) * pow(85, 4 - $i);
  225.             for ($i = 0; $i < $state - 1; $i++) {
  226.                 try {
  227.                     if(false == ($o = chr($sum >> ((3 - $i) * 8)))) {
  228.                         throw new Exception('Error');
  229.                     }
  230.                     $output .= $o;
  231.                 } catch (Exception $e) { /*Dont do anything*/ }
  232.             }
  233.         }
  234.  
  235.         return $output;
  236.     }
  237.  
  238.     function decodeFlate($data) {
  239.         return @gzuncompress($data);
  240.     }
  241.  
  242.     function getObjectOptions($object) {
  243.         $options = array();
  244.  
  245.         if (preg_match("#<<(.*)>>#ismU", $object, $options)) {
  246.             $options = explode("/", $options[1]);
  247.             @array_shift($options);
  248.  
  249.             $o = array();
  250.             for ($j = 0; $j < @count($options); $j++) {
  251.                 $options[$j] = preg_replace("#\s+#", " ", trim($options[$j]));
  252.                 if (strpos($options[$j], " ") !== false) {
  253.                     $parts = explode(" ", $options[$j]);
  254.                     $o[$parts[0]] = $parts[1];
  255.                 } else
  256.                     $o[$options[$j]] = true;
  257.             }
  258.             $options = $o;
  259.             unset($o);
  260.         }
  261.  
  262.         return $options;
  263.     }
  264.  
  265.     function getDecodedStream($stream, $options) {
  266.         $data = "";
  267.         if (empty($options["Filter"]))
  268.             $data = $stream;
  269.         else {
  270.             $length = !empty($options["Length"]) ? $options["Length"] : strlen($stream);
  271.             $_stream = substr($stream, 0, $length);
  272.  
  273.             foreach ($options as $key => $value) {
  274.                 if ($key == "ASCIIHexDecode")
  275.                     $_stream = $this->decodeAsciiHex($_stream);
  276.                 elseif ($key == "ASCII85Decode")
  277.                     $_stream = $this->decodeAscii85($_stream);
  278.                 elseif ($key == "FlateDecode")
  279.                     $_stream = $this->decodeFlate($_stream);
  280.                 elseif ($key == "Crypt") { // TO DO
  281.                 }
  282.             }
  283.             $data = $_stream;
  284.         }
  285.         return $data;
  286.     }
  287.  
  288.     function getDirtyTexts(&$texts, $textContainers) {
  289.         for ($j = 0; $j < count($textContainers); $j++) {
  290.             if (preg_match_all("#\[(.*)\]\s*TJ[\n|\r]#ismU", $textContainers[$j], $parts))
  291.                 $texts = array_merge($texts, array(@implode('', $parts[1])));
  292.             elseif (preg_match_all("#T[d|w|m|f]\s*(\(.*\))\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
  293.                 $texts = array_merge($texts, array(@implode('', $parts[1])));
  294.             elseif (preg_match_all("#T[d|w|m|f]\s*(\[.*\])\s*Tj[\n|\r]#ismU", $textContainers[$j], $parts))
  295.                 $texts = array_merge($texts, array(@implode('', $parts[1])));
  296.         }
  297.  
  298.     }
  299.  
  300.     function getCharTransformations(&$transformations, $stream) {
  301.         preg_match_all("#([0-9]+)\s+beginbfchar(.*)endbfchar#ismU", $stream, $chars, PREG_SET_ORDER);
  302.         preg_match_all("#([0-9]+)\s+beginbfrange(.*)endbfrange#ismU", $stream, $ranges, PREG_SET_ORDER);
  303.  
  304.         for ($j = 0; $j < count($chars); $j++) {
  305.             $count = $chars[$j][1];
  306.             $current = explode("\n", trim($chars[$j][2]));
  307.             for ($k = 0; $k < $count && $k < count($current); $k++) {
  308.                 if (preg_match("#<([0-9a-f]{2,4})>\s+<([0-9a-f]{4,512})>#is", trim($current[$k]), $map))
  309.                     $transformations[str_pad($map[1], 4, "0")] = $map[2];
  310.             }
  311.         }
  312.         for ($j = 0; $j < count($ranges); $j++) {
  313.             $count = $ranges[$j][1];
  314.             $current = explode("\n", trim($ranges[$j][2]));
  315.             for ($k = 0; $k < $count && $k < count($current); $k++) {
  316.                 if (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+<([0-9a-f]{4})>#is", trim($current[$k]), $map)) {
  317.                     $from = hexdec($map[1]);
  318.                     $to = hexdec($map[2]);
  319.                     $_from = hexdec($map[3]);
  320.  
  321.                     for ($m = $from, $n = 0; $m <= $to; $m++, $n++)
  322.                         $transformations[sprintf("%04X", $m)] = sprintf("%04X", $_from + $n);
  323.                 } elseif (preg_match("#<([0-9a-f]{4})>\s+<([0-9a-f]{4})>\s+\[(.*)\]#ismU", trim($current[$k]), $map)) {
  324.                     $from = hexdec($map[1]);
  325.                     $to = hexdec($map[2]);
  326.                     $parts = preg_split("#\s+#", trim($map[3]));
  327.  
  328.                     for ($m = $from, $n = 0; $m <= $to && $n < count($parts); $m++, $n++)
  329.                         $transformations[sprintf("%04X", $m)] = sprintf("%04X", hexdec($parts[$n]));
  330.                 }
  331.             }
  332.         }
  333.     }
  334.     function getTextUsingTransformations($texts, $transformations) {
  335.         $document = "";
  336.         for ($i = 0; $i < count($texts); $i++) {
  337.             $isHex = false;
  338.             $isPlain = false;
  339.  
  340.             $hex = "";
  341.             $plain = "";
  342.             for ($j = 0; $j < strlen($texts[$i]); $j++) {
  343.                 $c = $texts[$i][$j];
  344.                 switch($c) {
  345.                     case "<":
  346.                         $hex = "";
  347.                         $isHex = true;
  348.                         $isPlain = false;
  349.                     break;
  350.                     case ">":
  351.                         $hexs = str_split($hex, $this->multibyte); // 2 or 4 (UTF8 or ISO)
  352.                         for ($k = 0; $k < count($hexs); $k++) {
  353.  
  354.                             $chex = str_pad($hexs[$k], 4, "0"); // Add tailing zero
  355.                             if (isset($transformations[$chex]))
  356.                                 $chex = $transformations[$chex];
  357.                             $document .= html_entity_decode("&#x".$chex.";");
  358.                         }
  359.                         $isHex = false;
  360.                     break;
  361.                     case "(":
  362.                         $plain = "";
  363.                         $isPlain = true;
  364.                         $isHex = false;
  365.                     break;
  366.                     case ")":
  367.                         $document .= $plain;
  368.                         $isPlain = false;
  369.                     break;
  370.                     case "\\":
  371.                         $c2 = $texts[$i][$j + 1];
  372.                         if (in_array($c2, array("\\", "(", ")"))) $plain .= $c2;
  373.                         elseif ($c2 == "n") $plain .= '\n';
  374.                         elseif ($c2 == "r") $plain .= '\r';
  375.                         elseif ($c2 == "t") $plain .= '\t';
  376.                         elseif ($c2 == "b") $plain .= '\b';
  377.                         elseif ($c2 == "f") $plain .= '\f';
  378.                         elseif ($c2 >= '0' && $c2 <= '9') {
  379.                             $oct = preg_replace("#[^0-9]#", "", substr($texts[$i], $j + 1, 3));
  380.                             $j += strlen($oct) - 1;
  381.                             $plain .= html_entity_decode("&#".octdec($oct).";", $this->convertquotes);
  382.                         }
  383.                         $j++;
  384.                     break;
  385.  
  386.                     default:
  387.                         if ($isHex)
  388.                             $hex .= $c;
  389.                         elseif ($isPlain)
  390.                             $plain .= $c;
  391.                     break;
  392.                 }
  393.             }
  394.             $document .= "\n";
  395.         }
  396.  
  397.         return $document;
  398.     }
  399. }
  400. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement