note 53628 added to ref.pdf
| From: | jorromer at uchile dot cl--Krash | Date: | Tue, 07 Jun 2005 17:51:23 +0000 |
| Subject: | note 53628 added to ref.pdf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90078@lists.php.net to get a copy of this message | ||
I recently use mattb code below for the extraction of text from PDF files. I modify this code for
only extract text fields.
Hope i can help some one
Here is the Function
<?php
$text = pdf2string("file.pdf");
echo $text;
function pdf2string($sourcefile){
$fp = fopen($sourcefile, 'rb');
$content = fread($fp, filesize($sourcefile));
fclose($fp);
$searchstart = 'stream';
$searchend = 'endstream';
$pdfdocument = '';
$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);
$data = ExtractText2($data);
$startpos = $pos2 + strlen($searchend) - 1;
if ($data === false){
return -1;}
$pdfdocument .= $data;}}
return $pdfdocument;}
function ExtractText2($postScriptData){
$sw = true;
$textStart = 0;
$len = strlen($postScriptData);
while ($sw){
$ini = strpos($postScriptData, '(', $textStart);
$end = strpos($postScriptData, ')', $textStart+1);
if (($ini>0) && ($end>$ini)){
$valtext = strpos($postScriptData,'Tj',$end+1);
if ($valtext == $end + 2)
$text .= substr($postScriptData,$ini+1,$end - $ini - 1);}
$textStart = $end + 1;
if ($len<=$textStart) $sw=false;
if (($ini == 0) && ($end == 0)) $sw=false;}
$trans = array("\\341" => "a","\\351" =>
"e","\\355" => "i","\\363" =>
"o","\\223" => "","\\224" => "");
$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+53628
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53628&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53628&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53628&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53628&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53628&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53628&report=yes
Search -- http://master.php.net/manage/user-notes.php