10

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

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
lolalola
  • 3,773
  • 20
  • 60
  • 96
  • 1
    Use [`file_get_contents`](http://de3.php.net/manual/en/function.file-get-contents.php) if you need the raw binary data or update your question and tell us what you really want. – Linus Kleen Jan 24 '11 at 10:03
  • I need get clean text from pdf files. When i get text from pdf files I need insert this text in DB. – lolalola Jan 24 '11 at 11:09

3 Answers3

8

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;
    }
}
Matthew Smith
  • 6,165
  • 6
  • 34
  • 35
  • Download link for xpdf: https://www.xpdfreader.com/download.html and link to popen: http://php.net/manual/en/function.popen.php – kurdtpage Nov 09 '17 at 00:58
0

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

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