Converting PDF to string [closed]

橙三吉。 提交于 2019-11-27 03:34:45

问题


How read PDF file and put content into string? Using PHP language.


回答1:


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;
    }
}



回答2:


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

  • PDF2Text - Pastebin

Probably these will help you to add functionality:

  • http://www.adobe.com/devnet/pdf/pdf_reference.html

  • http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf

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.




回答3:


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;



回答4:


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" ;


来源:https://stackoverflow.com/questions/4780697/converting-pdf-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!