如何在 PHP 中将 PDF 文档转换为预览图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/467793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:50:55  来源:igfitidea点击:

How do I convert a PDF document to a preview image in PHP?

phpimagepdflamp

提问by Mathew Byrne

What libraries, extensions etc. would be required to render a portion of a PDF document to an image file?

将 PDF 文档的一部分呈现为图像文件需要哪些库、扩展等?

Most PHP PDF libraries that I have found center around creating PDF documents, but is there a simple way to render a document to an image format suitable for web use?

我发现的大多数 PHP PDF 库都以创建 PDF 文档为中心,但是有没有一种简单的方法可以将文档呈现为适合 Web 使用的图像格式?

Our environment is a LAMP stack.

我们的环境是一个 LAMP 堆栈。

回答by Paolo Bergantino

You need ImageMagickand GhostScript

你需要ImageMagickGhostScript

<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

The [0]means page 1.

[0]手段page 1

回答by Andrew

For those who don't have ImageMagick for whatever reason, GD functions will also work, in conjunction with GhostScript. Run the ghostscript command with exec()to convert a PDF to JPG, and manipulate the resulting file with imagecreatefromjpeg().

对于那些由于某种原因没有 ImageMagick 的人,GD 函数也可以与 GhostScript 结合使用。运行 ghostscript 命令exec()将 PDF 转换为 JPG,并使用imagecreatefromjpeg().

Run the ghostscript command:

运行 ghostscript 命令:

exec('gs -dSAFER -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sOutputFile=whatever.jpg input.pdf')

To manipulate, create a new placeholder image, $newimage = imagecreatetruecolor(...), and bring in the current image. $image = imagecreatefromjpeg('whatever.jpg'), and then you can use imagecopyresampled()to change the size, or any number of other built-in, non-imagemagickcommands

要进行操作,请创建一个新的占位符图像$newimage = imagecreatetruecolor(...),然后引入当前图像。$image = imagecreatefromjpeg('whatever.jpg'),然后您可以使用imagecopyresampled()更改大小,或任意数量的其他内置非imagemagick命令

回答by Jason

You can also get the page count using

您还可以使用

$im->getNumberImages();

Then you can can create thumbs of all the pages using a loop, eg.

然后您可以使用循环创建所有页面的缩略图,例如。

'file.pdf['.$x.']'

回答by Sebastian

Use the php extension Imagick. To control the desired size of the raster output image, use the setResolution function

使用 php 扩展Imagick。要控制光栅输出图像的所需大小,请使用setResolution 函数

<?php    
$im = new Imagick();
$im->setResolution(300, 300);     //set the resolution of the resulting jpg
$im->readImage('file.pdf[0]');    //[0] for the first page
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>

(Extension on Paolo Bergantino his answer and Luis Melgratti his comment. You need to set the resolution before loading the image.)

(扩展 Paolo Bergantino 他的回答和 Luis Melgratti 他的评论。您需要在加载图像之前设置分辨率。)

回答by jrjohnson

If you're loading the PDF from a blob this is how you get the first page instead of the last page:

如果您从 blob 加载 PDF,这就是您获取第一页而不是最后一页的方式:

$im->readimageblob($blob);
$im->setiteratorindex(0);

回答by Preet Sandhu

You can also try executing the 'convert' utility that comes with imagemagick.

您还可以尝试执行 imagemagick 附带的“转换”实用程序。

exec("convert pdf_doc.pdf image.jpg");
echo 'image-0.jpg';

回答by imal hasaranga perera

I'm the author of PDFlibwhich is a GhostScriptwrapper for php, advantage of using this library is, it is already tested and it does not require ImageMagic

我是PDFlib的作者,它是 php的GhostScript包装器,使用这个库的好处是,它已经过测试,不需要ImageMagic

Always GhostScriptcommands are faster than ImageMagicwhen it comes to pdf so you should either go for a GhostScript wrapper or pure GhostScript commands

GhostScript命令总是比ImageMagicpdf快,所以你应该选择 GhostScript 包装器或纯 GhostScript 命令

$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->convert();

回答by Duy Khanh

I install finished! It's worked!

我安装完了!成功了!

You may be do base install imagemagickon windows.

您可能会在 Windows 上进行基本安装 imagemagick

In php (local)use call exec(<command line>)ex:

php (local)使用呼叫exec(<command line>)前:

<?php
$pdf = "filename.pdf";
$info = pathinfo($pdf);
$file_name =  basename($pdf,'.'.$info['extension']);
echo $file_name;
$pdf = "filename.pdf[0]";
exec("convert $pdf convert-img/$file_name.jpg");    
?>

Besides, you may be use class imagickin PHP Imagick class

此外,您可能会class imagickPHP Imagick 类中使用

Thanks all helped me!

谢谢大家帮助我!

回答by jewelhuq

Think differently, You can use the following library to convert pdf to image using javascript

换个角度思考,您可以使用以下库使用 javascript 将 pdf 转换为图像

http://usefulangle.com/post/24/pdf-to-jpeg-png-with-pdfjs

http://usefulangle.com/post/24/pdf-to-jpeg-png-with-pdfjs

回答by user664995

Here is a simple class I've written and used on a couple of projects. It just wraps imagickand handles writing each page out to disk. If anyone is still looking for an easy way to do this, this linkmight be helpful.

这是我在几个项目中编写并使用的一个简单类。它只是包装imagick并处理将每个页面写入磁盘。如果有人仍在寻找一种简单的方法来做到这一点,这个链接可能会有所帮助。