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