1

我需要在php中将pdf转换为png。由于质量原因,我们不想使用 Imagemagick,但更喜欢使用 pdftoppm。

为了性能,我们不喜欢使用文件系统,而是使用内存。

pdftoppm 已正确安装在 Ubuntu 上并且可以正常工作。

对于另一个项目(html -> pdf),我们使用以下代码:

//input is $html

$descriptorSpec =
[
    0 => ['pipe', 'r'],
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w']
];

$command = 'wkhtmltopdf --quiet  - -';

$process = proc_open($command, $descriptorSpec, $pipes);

fwrite($pipes[0], $html);
fclose($pipes[0]);
$pdf = stream_get_contents($pipes[1]);
$errors = stream_get_contents($pipes[2]);
if ($errors)
{
    $errors = ucfirst(strtr($errors, [
        'sh: wkhtmltopdf: ' => '',
        PHP_EOL => ''
    ]));
    throw new Exception($errors); 
}
fclose($pipes[1]);
$return_value = proc_close($process);

//output is $pdf

这很完美!

但是如果我使用这段代码对 pdftoppm 做同样的事情,它不起作用,我做错了什么?

//input is $pdf

$descriptorSpec =
[
    0 => ['pipe', 'r'],
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w']
];

$command = 'pdftoppm -png  - -';

$process = proc_open($command, $descriptorSpec, $pipes);

fwrite($pipes[0], $pdf);
fclose($pipes[0]);
$png = stream_get_contents($pipes[1]);
$errors = stream_get_contents($pipes[2]);
if ($errors)
{
    $errors = ucfirst(strtr($errors, [
        'sh: pdftoppm: ' => '',
        PHP_EOL => ''
    ]));
    throw new Exception($errors); 
}
fclose($pipes[1]);
$return_value = proc_close($process);

//output is $png

预先感谢您的提示和建议对不起我的英语不好..

4

1 回答 1

1

好的,自己修好了!

删除了连字符。

$command = 'pdftoppm -png ';

感谢所有的支持 !

于 2016-05-09T19:27:14.300 回答