gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:45:57 26 4
gpt4 key购买 nike

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

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

pdftopm 已正确安装在 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

这很完美!

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

//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

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

最佳答案

好的,我自己修好了!

删除了连字符。

$command = 'pdftoppm -png ';

感谢大家的支持!

关于php - 在 php 中使用 pdftopm 将 pdf 转换为图像,而无需在磁盘上写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071723/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com