note 43476 added to ref.pdf
| From: | donatas at spurgius dot com | Date: | Tue, 22 Jun 2004 16:56:55 +0000 |
| Subject: | note 43476 added to ref.pdf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-71510@lists.php.net to get a copy of this message | ||
I've been looking for a way to extract plain text from PDF documents (needed to search for text
inside 'em). Not being able to find one I wrote the needed functions myself. here you go folks.
<?php
function pdf2string ($sourceFile)
{
$textArray = array ();
$objStart = 0;
$fp = fopen ($sourceFile, 'rb');
$content = fread ($fp, filesize ($sourceFile));
fclose ($fp);
$searchTagStart = chr(13).chr(10).'stream';
$searchTagStartLenght = strlen ($searchTagStart);
while ((($objStart = strpos ($content, $searchTagStart, $objStart)) && ($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)
{
$textArray [] = ExtractText ($data);
}
$objStart = $objStart < $objEnd ? $objEnd : $objStart + 1;
}
return $textArray;
}
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+43476
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43476&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43476&report=yes
Search -- http://master.php.net/manage/user-notes.php