note 49690 added to ref.pdf
| From: | mattb at bluewebstudios dot com | Date: | Fri, 04 Feb 2005 21:44:40 +0000 |
| Subject: | note 49690 added to ref.pdf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84544@lists.php.net to get a copy of this message | ||
I recently tested Donatas' code below for the extraction of text from PDF files. After running
into a few problems where PDF files were not being read at all, I've modified it somewhat. It
still isn't perfect, but should work great for searching. Thanks Donatas.
<?php
$test = pdf2string("<pathtoPDFfile>");
echo "$test";
# Returns a -1 if uncompression failed
function pdf2string($sourcefile)
{
$fp = fopen($sourcefile, 'rb');
$content = fread($fp, filesize($sourcefile));
fclose($fp);
# Locate all text hidden within the stream and endstream tags
$searchstart = 'stream';
$searchend = 'endstream';
$pdfdocument = "";
$pos = 0;
$pos2 = 0;
$startpos = 0;
# Iterate through each stream block
while( $pos !== false && $pos2 !== false )
{
# Grab beginning and end tag locations if they have not yet been parsed
$pos = strpos($content, $searchstart, $startpos);
$pos2 = strpos($content, $searchend, $startpos + 1);
if( $pos !== false && $pos2 !== false )
{
# Extract compressed text from between stream tags and uncompress
$textsection = substr($content, $pos + strlen($searchstart) + 2, $pos2 - $pos -
strlen($searchstart) - 1);
$data = @gzuncompress($textsection);
# Clean up text via a special function
$data = ExtractText($data);
# Increase our PDF pointer past the section we just read
$startpos = $pos2 + strlen($searchend) - 1;
if( $data === false ) { return -1; }
$pdfdocument = $pdfdocument . $data;
}
}
return $pdfdocument;
}
function ExtractText($postScriptData)
{
while( (($textStart = strpos($postScriptData, '(', $textStart)) && ($textEnd =
strpos($postScriptData, ')', $textStart + 1)) && substr($postScriptData, $textEnd
- 1) != '\\') )
{
$plainText .= substr($postScriptData, $textStart + 1, $textEnd - $textStart - 1);
if( substr($postScriptData, $textEnd + 1, 1) == ']' ) // This adds quite some
additional spaces between the words
{
$plainText .= ' ';
}
$textStart = $textStart < $textEnd ? $textEnd : $textStart + 1;
}
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+49690
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49690&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49690&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49690&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49690&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49690&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49690&report=yes
Search -- http://master.php.net/manage/user-notes.php