note 56492 added to ref.pdf

From: Date: Mon, 05 Sep 2005 05:22:40 +0000
Subject: note 56492 added to ref.pdf
Groups: php.notes 
Request: Send a blank email to php-notes+get-94734@lists.php.net to get a copy of this message
Yet another addition to the PDF text extraction code last posted by jorromer. The code only seemed to work for PDF 1.2 (Acrobat 3.x) or below. This pdfExtractText function uses regular expressions to cover cases I have found in PDF 1.3 and 1.4 documents. The code also handles closing brackets in the text stream, which were ignored by the previous version. My regular expression skills are somewhat lacking, so improvements may possible by a more skilled programmer. I'm sure there are still cases that this function will not handle, but I haven't come across any yet... <?php function pdf2string($sourcefile) { $fp = fopen($sourcefile, 'rb'); $content = fread($fp, filesize($sourcefile)); fclose($fp); $searchstart = 'stream'; $searchend = 'endstream'; $pdfText = ''; $pos = 0; $pos2 = 0; $startpos = 0; while ($pos !== false && $pos2 !== false) { $pos = strpos($content, $searchstart, $startpos); $pos2 = strpos($content, $searchend, $startpos + 1); if ($pos !== false && $pos2 !== false){ if ($content[$pos] == 0x0d && $content[$pos + 1] == 0x0a) { $pos += 2; } else if ($content[$pos] == 0x0a) { $pos++; } if ($content[$pos2 - 2] == 0x0d && $content[$pos2 - 1] == 0x0a) { $pos2 -= 2; } else if ($content[$pos2 - 1] == 0x0a) { $pos2--; } $textsection = substr( $content, $pos + strlen($searchstart) + 2, $pos2 - $pos - strlen($searchstart) - 1 ); $data = @gzuncompress($textsection); $pdfText .= pdfExtractText($data); $startpos = $pos2 + strlen($searchend) - 1; } } return preg_replace('/(\s)+/', ' ', $pdfText); } function pdfExtractText($psData){ if (!is_string($psData)) { return ''; } $text = ''; // Handle brackets in the text stream that could be mistaken for // the end of a text field. I'm sure you can do this as part of the // regular expression, but my skills aren't good enough yet. $psData = str_replace('\)', '##ENDBRACKET##', $psData); $psData = str_replace('\]', '##ENDSBRACKET##', $psData); preg_match_all( '/(T[wdcm*])[\s]*(\[([^\]]*)\]|\(([^\)]*)\))[\s]*Tj/si', $postScriptData, $matches ); for ($i = 0; $i < sizeof($matches[0]); $i++) { if ($matches[3][$i] != '') { // Run another match over the contents. preg_match_all('/\(([^)]*)\)/si', $matches[3][$i], $subMatches); foreach ($subMatches[1] as $subMatch) { $text .= $subMatch; } } else if ($matches[4][$i] != '') { $text .= ($matches[1][$i] == 'Tc' ? ' ' : '') . $matches[4][$i]; } } // Translate special characters and put back brackets. $trans = array( '...' => '&hellip;', '\205' => '&hellip;', '\221' => chr(145), '\222' => chr(146), '\223' => chr(147), '\224' => chr(148), '\226' => '-', '\267' => '&bull;', '\(' => '(', '\[' => '[', '##ENDBRACKET##' => ')', '##ENDSBRACKET##' => ']', chr(133) => '-', chr(141) => chr(147), chr(142) => chr(148), chr(143) => chr(145), chr(144) => chr(146), ); $text = strtr($text, $trans); return $text; } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.pdf.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+56492 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56492&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56492&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56492&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56492&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56492&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56492&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+56492&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#94734) next »