【问题标题】:Convert PDF to JPG image with PHP使用 PHP 将 PDF 转换为 JPG 图像
【发布时间】:2012-11-16 00:57:38
【问题描述】:

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

这是我正在运行的脚本

<?php

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

?>

【问题讨论】:

  • 您可能需要指定要转换的完整路径。您的输入和输出是否与运行转换的目录位于同一目录中?检查 $output 变量是否有错误消息? Imagemagick 是否包含委托 Ghostscript 和 libjpeg?

标签: php pdf imagemagick jpeg


【解决方案1】:

试试这个。

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

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

?>

【讨论】:

  • 就我最近的经验来看,ghostScript 比 convert 命令更快,试试我写的这个完整的库,它的速度超级快github.com/imalhasaranga/PDFBox
  • $output, $return_var 的值是什么
【解决方案2】:

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

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

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

which convert

【讨论】:

    【解决方案3】:
    convert -normalize yourfile.pdf[0] yourdestination.jpg
    

    【讨论】:

      【解决方案4】:

      ImageMagick 内部使用 GhostScript 并且通常 ImageMagick 的转换速度比 Ghoastscript 慢,所以如果您只对将 pdf 转换为图像感兴趣,那么 Ghostscript gs 命令更快。 下面是我几天前写的 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();
      

      【讨论】:

      • 不,那不是 PDFBox。 PDFBox 的正确 URL 是 pdfbox.apache.org
      • 你的问题是什么? PDFBox 用于 java,this 用于 php,问题是如何在 php no in java 中做到这一点
      【解决方案5】:

      这里有我的解决方案。直接在你的 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 库。

      干杯!

      【讨论】:

      • 试图将 pdf 转换为图像并出现错误,启用了 Imagick 扩展,请帮助我致命错误:未捕获的 ImagickException:UnableToOpenBlob 'sample.pdf':没有这样的文件或目录@error/blob.c/ E:\XAMPP\htdocs\pdf\convert\index.php:6 中的 OpenBlob/3315 堆栈跟踪:#0 E:\XAMPP\htdocs\pdf\convert\index.php(6): Imagick->readimage('sample .pdf') #1 {main} 在第 6 行的 E:\XAMPP\htdocs\pdf\convert\index.php 中抛出
      • 再次致命错误:未捕获的 ImagickException:PDFDelegateFailed `系统找不到指定的文件。 ' @error/pdf.c/ReadPDFImage/794 在 E:\XAMPP\htdocs\pdf\convert\index.php:8 堆栈跟踪:#0 E:\XAMPP\htdocs\pdf\convert\index.php(8) : Imagick->readimage('E:/XAMPP/htdocs...') #1 {main} 在第 8 行的 E:\XAMPP\htdocs\pdf\convert\index.php 中抛出
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-27
      • 2020-08-09
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多