Converting PDF to string [closed]

梦想的初衷 提交于 2019-11-28 10:10:20

You could use something like pdftotext which comes with the Xpdf package on linux. The popen command can then be used to pipe the output of pdftotext into a string:

$mystring = "";
$fd = popen("/usr/bin/pdftotext blah.pdf","r");
if ($fd) {
    while (($myline = fgets($fd)) !== false) {
        $mystring .= $myline;
    }
}
advanced_noob

Found this really nice class! Further, you can add functionality to fit your needs.

Probably these will help you to add functionality:

If it doesn't work, check if you can highlight/mark your text when opening in Adobe Reader (if you can't, the text in your file is probably saved as geometric curves), check also for the encoding.

Install APACHE-TIKA on your server. APACHE-TIKA support more then pdf files. Install guide: http://www.acquia.com/blog/use-apache-solr-search-files

and final code is easy:

$string = "";
$fd = popen("java -jar yourpathtotika/tika-app-1.3.jar -t yourpathtopdf/sample.pdf","r");
while (!feof($fd)) { 
$buffer = fgets($fd, 4096); 
$string .= $buffer;
}
echo $string;

You can use the PHP class that is available here :

http://www.pdftotext.eu

This is a public domain PDF text extractor entirely written in pure PHP, meaning that you do not need to rely on external commands. It provides a simple interface to retrieve text :

include ( 'PdfToText.phpclass' ) ;
$pdf = new PdfToText ( 'mysample.pdf' ) ;
echo "PDF contents are : " . $pdf -> Text . "\n" ;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!