php - 命令行后脚本不继续

标签 php pdf command-line poppler

我的命令行脚本有问题..PHP 脚本永远不会继续..

曾尝试通过 putty 直接调用命令行,它输出了很多错误,但立即返回/完成。为什么它不返回到 PHP?

它适用于其他 PDF 文件,但不适用于这个

pdf

http://docdro.id/b0M5vfw

代码

$Cmd = new Command;
if($err = $Cmd->exec('/var/bin/poppler-0.51.0/utils/pdfimages -list /var/test.pdf')){
    echo "ERR: $err\n";
}
echo "continue\n";

class Command {
    private $descriptorspec;

    private $output = '';

    private $process;
    private $pipes = [];

    public function __construct(){
        $this->descriptorspec = [
            0 => ['pipe', 'r'], // stdin
            1 => ['pipe', 'w'], // stdout
            2 => ['pipe', 'w']  // stderr
        ];
    }

    public function output(): string{
        return $this->output;
    }

    public function close(){
        foreach($this->pipes as $pipe){
            if(is_resource($pipe)){
                fclose($pipe);
            }
        }

        proc_close($this->process);
    }

    public function exec(string $syntax){
        $this->process = proc_open($syntax, $this->descriptorspec, $this->pipes);
        fclose($this->pipes[0]);

        $this->output = stream_get_contents($this->pipes[1]);

        $stderr = stream_get_contents($this->pipes[2]);

        $this->close();

        return $stderr;
    }
}

错误

# /var/bin/poppler-0.51.0/utils/pdfimages -list /var/test.pdf
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    2154   303  rgb     3   8  jpeg   yes  [inline]     289   292    -    -
Syntax Error (50560): Illegal character '>'
Syntax Error (50560): Unknown operator '<10><07><82>;w<ad><a2><b4>2r<1f><10><07><8f>~j<c4>Hq<cf>Z<86>'
Syntax Error (50568): Unknown operator '<0f><b5>X<8f><ae><d0>:<d7>DU<91><cb>'v'
Syntax Error (50568): Illegal character ')'

........

Syntax Error (66698): Illegal character <04> in hex string
Syntax Error (66699): Illegal character <ff> in hex string
Syntax Error (66699): Illegal character <c1> in hex string
Syntax Error (66705): Unknown operator '<9b>'
Syntax Error (66714): Illegal character ')'
Syntax Error (66714): Unknown operator '<bc>q<ff>'
Syntax Error (66720): Unknown operator '<05>6<f8><c2><fa><d7><c3>?<f8>'
Syntax Error (66741): Unknown operator '<df><ec><99><e1>-'
Syntax Error (66743): Unknown operator ']'
Syntax Error (66762): Unknown operator '<cc>'
Syntax Error: Unterminated string
Syntax Error: End of file inside array
Syntax Error: End of file inside array
Syntax Error: Leftover args in content stream

最佳答案

PDF 有问题 - @dwarring 已经在评论中回避了这一点(在此处引用是为了感谢评论者)

@dwarring said "just quickly, I'm pretty sure that this PDF is dying because the content stream contains an inline image started by and 'BI' followed by random data and ended by 'EI'. The Adobe engineers were having an off-day when they designed these operators, the problem being that situations arise where the binary data randomly contains 'EI' and makes the PDF unparsable. Some tools may handle this better, but ideally the producer of this image should avoid the use of inline images."

不过,从 PHP 的角度来看,使用 try/catch block 而不是 if 语句,您应该保留对脚本的控制。

$Cmd = new Command;

try {
    $err = $Cmd->exec('/var/bin/poppler-0.51.0/utils/pdfimages - list/var/test.pdf')){
} catch (Exception $e) {
    var_log($e);
}

echo "continue\n";

关于php - 命令行后脚本不继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42467869/

相关文章:

mysql - 使用命令行加载.sql文件

php - 如何动态编写PHP对象属性名称?

javascript - Ajax 文件上传 Vue.js

linux - sudo 事件在哪里报告?

python - 在 python3 中通过 PDF 写入文本

javascript - 在 HTML 页面中的 PDF 文档上添加触摸目标

matlab - 如何在 MATLAB 匿名函数中执行多条语句?

php - 保存后 Laravel 模型 ID 为空(ID 递增)

php - Laravel 对数据库行运行更新仅返回已更改的字段?

javascript - 导出为 PDF 时如何在表格中保留垂直标题?