【问题标题】:how to extract texts from PDFs using xpdf?如何使用 xpdf 从 PDF 中提取文本?
【发布时间】:2012-02-14 23:58:08
【问题描述】:

我的文件夹中有许多 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>";
        }
    }
}

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

【问题讨论】:

  • 你有没有尝试过?放置良好的回声语句怎么样

标签: php xpdf


【解决方案1】:

尝试使用这个:

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

【讨论】:

    【解决方案2】:

    您不必创建临时 txt 文件

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

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

    【讨论】:

      【解决方案3】:

      线条

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

      应该是

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

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-05
        • 2023-04-02
        • 1970-01-01
        • 2016-04-22
        • 1970-01-01
        相关资源
        最近更新 更多