【问题标题】:Getting the results of pdftotext into a php variable, not a text file将 pdftotext 的结果放入 php 变量,而不是文本文件
【发布时间】:2009-09-14 15:09:13
【问题描述】:

pdftotext 获取 PDF 文件并将文本转换为 .txt 文件。

如何让 pdftotext 将结果发送到 PHP 变量而不是文本文件?

我假设我必须运行 exec('pdftotext /path/file.pdf'),但我该如何取回结果?

【问题讨论】:

  • 我的pdftotext 命令没有在exec() 函数中运行。我错过了图书馆中使用的任何东西吗?
  • @SumitMadan 为您在 exec() 中运行任何东西。如果没有,你的问题就在那里,如果其他人运行然后检查以确保 pdftotext 已安装...
  • ya 其他命令正在运行并且 pdftotext 已安装。

标签: php pdf-generation


【解决方案1】:
$result = shell_exec("pdftotext file.pdf -");

- 将指示 pdftotext 将结果返回到标准输出而不是文件。

【讨论】:

    【解决方案2】:

    你需要捕获stdout/stderr:

    function cmd_exec($cmd, &$stdout, &$stderr)
    {
        $outfile = tempnam(".", "cmd");
        $errfile = tempnam(".", "cmd");
        $descriptorspec = array(
            0 => array("pipe", "r"),
            1 => array("file", $outfile, "w"),
            2 => array("file", $errfile, "w")
        );
        $proc = proc_open($cmd, $descriptorspec, $pipes);
    
        if (!is_resource($proc)) return 255;
    
        fclose($pipes[0]);    //Don't really want to give any input
    
        $exit = proc_close($proc);
        $stdout = file($outfile);
        $stderr = file($errfile);
    
        unlink($outfile);
        unlink($errfile);
        return $exit;
    }
    

    【讨论】:

    • 对不起,我很抱歉,但是......我应该使用第二个和第三个参数是什么?现在我有 echo cmd_exec('/usr/local/bin/pdftotext /users/jmr/downloads/test.pdf -');它返回 1 但是当我正常运行相同的命令时,我会在屏幕上看到 PDF 文本
    • 使用你想为第二个和第三个参数捕获输入的变量。
    • @altCognito 能否请您给出如何调用上述函数并获取值的代码行?我这样使用它并且参数未定义错误出来了。 pr( $this->cmd_exec(" pdftotext /var/www/sample.pdf - "),&$var,&$varr );
    • 我改变了 $stdout = file($outfile);到 $stdout = impode(file($outfile));现在它的工作。我像这样使用它 $this->cmd_exec(' pdftotext /var/www/sample.pdf - ',&$out,&$in ); //谢啦;回声 $out;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 2016-01-07
    • 1970-01-01
    相关资源
    最近更新 更多