4

我正在使用 ImageMagik 尝试将 PDF 的内容转换为 JPG,但总是得到一个空的 jpg。我已经确保所有测试的烫发都是 777,所以我有点迷失如何继续。

这是我正在运行的脚本

<?php

    exec('convert testfile.pdf output.jpg', $output, $return_var);

?>
4

5 回答 5

6

试试这个。

<?php
    $pdf = 'testfile.pdf';
    $save = 'output.jpg';

    exec('convert "'.$pdf.'" -colorspace RGB -resize 800 "'.$save.'"', $output, $return_var);

?>
于 2012-11-28T08:17:46.933 回答
1

使用二进制文件的绝对路径,如下所示:

exec('/usr/bin/convert testfile.pdf output.jpg', $output, $return_var);

但是请确保您的convert二进制文件实际上已打开,/usr/bin您可以使用以下命令进行检查:

which convert

于 2012-11-28T08:17:14.517 回答
1
convert -normalize yourfile.pdf[0] yourdestination.jpg
于 2012-11-28T08:19:44.607 回答
0

ImageMagick 内部使用 GhostScript,通常 ImageMagick 的转换速度比 Ghoastscript 慢,所以如果您只对将 pdf 转换为图像感兴趣,那么 Ghostscriptgs命令会更快。下面是我几天前写的 Ghostscript 的示例包装器。

PDFLib-PHP

$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->setImageQuality(95);
$pdflib->setDPI(300);
$pdflib->setPageRange(1,$pdflib->getNumberOfPages());
$pdflib->convert();
于 2016-09-10T18:03:49.307 回答
0

在这里你有我的解决方案。直接在您的 php 代码中使用 Imagick。

将所有 PDF 页面转换为 JPG

 // create Imagick object
 $imagick = new Imagick();
 // Reads image from PDF
 $imagick->readImage('file.pdf');
 // Writes an image
 $imagick->writeImages('converted.jpg', false);

将特定的 PDF 页面转换为 JPG

 // create Imagick object
 $imagick = new Imagick();
 // Read image from PDF
 $imagick->readImage('test.pdf[0]');
 // Writes an image
 $imagick->writeImages('converted_page_one.jpg');

处理这个问题的另一种方法是使用spatie/pdf-to-image库。

干杯!

于 2017-09-11T13:05:17.843 回答