note 45876 added to ref.pdf

From: Date: Wed, 22 Sep 2004 10:02:39 +0000
Subject: note 45876 added to ref.pdf
Groups: php.notes 
Request: Send a blank email to php-notes+get-77145@lists.php.net to get a copy of this message
Donata's pdf2string function did not work very well, as there are also .pdf documents, that do not have a \n or \r in front of every "stream". And because the data parsing also had some bugs with brackets and spaces, I worked on the script he provided and improved the output array, so it now has the correct spaces, brackets and special characters: <?php function strpos2 ($content,$str,$start,$exclude) { // this function allows us to look for the position of every "stream" while filtering those "endstreams" out $pos = strpos ($content, $str, $start); while(strtolower(substr($content,$pos-(strlen($exclude) - strlen($str)),strlen($exclude))) == $exclude) { $pos++; $pos = strpos ($content, $str, $pos); } return $pos; } function pdf2string ($sourceFile) { $textArray = array (); $objStart = 0; $fp = fopen ($sourceFile, 'rb'); $content = fread ($fp, filesize ($sourceFile)); fclose ($fp); $searchTagStart = 'stream'; $searchTagStartLenght = strlen ($searchTagStart); while ((($objStart = strpos2 ($content, $searchTagStart, $objStart, "endstream")) && ($objEnd = strpos ($content, 'endstream', $objStart+1)))) { $data = substr ($content, $objStart + $searchTagStartLenght + 2, $objEnd - ($objStart + $searchTagStartLenght) - 2); $data = @gzuncompress ($data); if ($data !== FALSE && strpos ($data, 'BT') !== FALSE && strpos ($data, 'ET') !== FALSE) { if(trim(substr ($data, -4)) == "EMC") $textArray [] = ExtractText ($data); // so we will only get pages that contain text - needs to be checked with different .pdf documents } $objStart = $objStart < $objEnd ? $objEnd : $objStart + 1; } return $textArray; } function ExtractText ($postScriptData) { $postScriptData = str_replace("\(","&#40;",$postScriptData); $postScriptData = str_replace("\)","&#41;",$postScriptData); $postScriptData = str_replace("( )","(<br>)",$postScriptData); while ((($textStart = strpos ($postScriptData, '(', $textStart)) && ($textEnd = strpos ($postScriptData, ')', $textStart + 1)) && substr ($postScriptData, $textEnd - 1) != '\\')) { $plainText .= substr ($postScriptData, $textStart + 1, $textEnd - $textStart - 1); $textStart = $textStart < $textEnd ? $textEnd : $textStart + 1; } //Sonderzeichen - special chars $szeichen = array("344"=>"&auml;","374"=>"&uuml;", "200"=>"&euro;","337"=>"&szlig;", "366"=>"&ouml;","247"=>"&sect;", "204"=>"&bdquo;","223"=>"&ldquo;", "224"=>"&rdquo;","221"=>"&lsquo;", "222"=>"&rsquo;"); // add your own here foreach($szeichen as $z1=>$z2) $plainText = str_replace("\\".$z1,$z2,$plainText); return stripslashes ($plainText); } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.pdf.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+45876 Delete -- http://master.php.net/manage/user-notes.php?action=delete+45876&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+45876&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#77145) next »