首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php中使用pdftoppm将pdf转换为图像,而无需在磁盘上写入文件

在php中使用pdftoppm将pdf转换为图像,而无需在磁盘上写入文件
EN

Stack Overflow用户
提问于 2016-05-06 11:53:59
回答 1查看 1.3K关注 0票数 1

我需要转换pdf到png在php。由于质量原因,我们不想使用Imagemagick,而是更喜欢使用pdftoppm。

为了性能起见,我们更愿意使用的不是文件系统,而是内存。

pdftoppm正确地安装在Ubuntu和works上。

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

代码语言:javascript
复制
//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做同样的操作,它不起作用,我做错了什么?

代码语言:javascript
复制
//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

谢谢你的提示和建议--对不起,我的英语很差。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-09 19:27:14

好的,我自己修!

移除连字符。

代码语言:javascript
复制
$command = 'pdftoppm -png ';

谢谢大家的支持!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37071723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档