Jump to content

pdf2text fix


atiq_uppsala

Recommended Posts

Hi,

I got some code from Internet which extracts text. It reads some pdf files and extract their text (not 100% correct) but it doesn't extract text from others at all. Can some one help to find out the reason ?

Here is the code:

<?php

// new pdf extract

$text = pdf2txt("input.pdf");
echo $text;
// Function    : pdf2txt()
// Arguments   : $filename - Filename of the PDF you want to extract
// Description : Reads a pdf file, extracts data streams, and manages
//               their translation to plain text - returning the plain
//               text at the end
// Author      : Jonathan Beckett, 2005-05-02
function pdf2txt($filename){

$data = getFileData($filename);

// grab objects and then grab their contents (chunks)
$a_obj = getDataArray($data,"obj","endobj");
foreach($a_obj as $obj){

	$a_filter = getDataArray($obj,"<<",">>");
	if (is_array($a_filter)){
		$j++;
		$a_chunks[$j]["filter"] = $a_filter[0];

		$a_data = getDataArray($obj,"stream\r\n","endstream");
		if (is_array($a_data)){
			$a_chunks[$j]["data"] = substr($a_data[0],strlen("stream\r\n"),strlen($a_data[0])-strlen("stream\r\n")-strlen("endstream"));
		}
	}
}

// decode the chunks
foreach($a_chunks as $chunk){

	// look at each chunk and decide how to decode it - by looking at the contents of the filter
	$a_filter = split("/",$chunk["filter"]);

	if ($chunk["data"]!=""){
		// look at the filter to find out which encoding has been used			
		if (substr($chunk["filter"],"FlateDecode")!==false){
			$data =@ gzuncompress($chunk["data"]);
			if (trim($data)!=""){
				$result_data .= ps2txt($data);
			} else {

				//$result_data .= "x";
			}
		}
	}
}

return $result_data;

}


// Function    : ps2txt()
// Arguments   : $ps_data - postscript data you want to convert to plain text
// Description : Does a very basic parse of postscript data to
//               return the plain text
// Author      : Jonathan Beckett, 2005-05-02
function ps2txt($ps_data){
$result = "";
$a_data = getDataArray($ps_data,"[","]");
if (is_array($a_data)){
	foreach ($a_data as $ps_text){
		$a_text = getDataArray($ps_text,"(",")");
		if (is_array($a_text)){
			foreach ($a_text as $text){
				$result .= substr($text,1,strlen($text)-2);

			}
		}
	}
} else {
	// the data may just be in raw format (outside of [] tags)
	$a_text = getDataArray($ps_data,"(",")");
	if (is_array($a_text)){
		foreach ($a_text as $text){
			$result .= substr($text,1,strlen($text)-2);

		}
	}
}
return $result;
}


// Function    : getFileData()
// Arguments   : $filename - filename you want to load
// Description : Reads data from a file into a variable
//               and passes that data back
// Author      : Jonathan Beckett, 2005-05-02
function getFileData($filename){
$handle = fopen($filename,"rb");
$data = fread($handle, filesize($filename));
fclose($handle);

return $data;
}


// Function    : getDataArray()
// Arguments   : $data       - data you want to chop up
//               $start_word - delimiting characters at start of each chunk
//               $end_word   - delimiting characters at end of each chunk
// Description : Loop through an array of data and put all chunks
//               between start_word and end_word in an array
// Author      : Jonathan Beckett, 2005-05-02
function getDataArray($data,$start_word,$end_word){

$start = 0;
$end = 0;
unset($a_result);

while ($start!==false && $end!==false){
	$start = strpos($data,$start_word,$end);
	if ($start!==false){
		$end = strpos($data,$end_word,$start);
		if ($end!==false){
			// data is between start and end
			$a_result[] = substr($data,$start,$end-$start+strlen($end_word));
		}
	}
}
return $a_result;
}

?>

Link to comment
Share on other sites

Have you tried contacting the author of the script you are using? He may have updated it since 2005. It's going to be pretty difficult for anyone to try and determine the problem when we have no idea what documents it works for and which ones it doesn't.

 

One guess would be that the script can only extract from PDFs saved in certain versions. The script was created back in 2005. It is very possible that the format for new versions of PDFs has changes such that the script does not work on the newer versions.

 

UPDATE: According to this page, the current version is 3.2, last updated on 7-17-2007: http://webscripts.softpedia.com/script/Development-Scripts-js/PDF2TXT-30100.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.