如何使用xpdf从PDF中提取文本?

时间:2012-02-14 23:58:08

标签: php xpdf

我在一个文件夹中有很多PDF。我想使用xpdf从这些PDF中提取文本。例如:

  • example1.pdf extract to example1.txt
  • example2.pdf extract to 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>";
        }
    }
}

我得到空白的结果。我的代码出了什么问题?

3 个答案:

答案 0 :(得分: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>";
    }
}

答案 1 :(得分:0)

您不必创建临时txt文件

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

如果不起作用,请检查服务器的错误日志。

答案 2 :(得分:0)

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

应该是

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

希望这有帮助