2012-02-14 88 views
1

我在一個文件夾中有很多PDF文件。我想使用xpdf從這些PDF中提取文本。例如:如何使用xpdf從PDF中提取文本?

  • example1.pdf提取物example1.txt
  • example2.pdf提取物example2.txt
  • 等。

這裏是我的代碼:

<?php 

$path = 'C:/AppServ/www/pdfs/'; 
$dir = opendir($path); 
$f = readdir($dir); 

while ($f = readdir($dir)) { 
    if (eregi("\.pdf",$f)){ 
     $content = shell_exec('C:/AppServ/www/pdfs/pdftotext '.$f.' '); 
     $read = strtok ($f,"."); 
     $testfile = "$read.txt"; 
     $file = fopen($testfile,"r"); 
     if (filesize($testfile)==0){} 
     else{ 
      $text = fread($file,filesize($testfile)); 
     fclose($file); 
     echo "</br>"; echo "</br>"; 
     } 
    } 
} 

我得到空白結果。我的代碼有什麼問題?

+0

你嘗試過什麼?如何放置良好的回聲陳述 – 2012-02-15 00:15:57

回答

2

嘗試:

$dir  = opendir($path); 
$filename = array(); 

while ($filename = readdir($dir)) { 
if (eregi("\.pdf",$filename)){ 
    $content = shell_exec('C:/AppServ/www/pdfs/pdftotext '.$filename.' '); 
    $read  = strtok ($filename,"."); 
    $testfile = "$read.txt"; 
    $file  = fopen($testfile,"r"); 
    if (filesize($testfile)==0){} 
    else{ 
     $text = fread($file,filesize($testfile)); 
     fclose($file); 
     echo "</br>"; echo "</br>"; 
    } 
} 
0

您不必創建一個臨時txt文件

$command = '/AppServ/www/pdfs/pdftotext ' . $filename . ' -'; 
$a = exec($command, $text, $retval); 
echo $text; 

,如果它不工作,檢查服務器的錯誤日誌。使用這種

0

線條

echo "</br>"; 
echo "</br>"; 

應該

echo "</br>"; 
echo $text."</br>"; 

希望這有助於