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.
我们的环境是一个灯罩。
10 个解决方案
#1
203
You need ImageMagick
and GhostScript
你需要ImageMagick和GhostScript
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The [0]
means page 1
.
[0]表示第1页。
#2
30
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一起工作。使用exec()运行ghostscript命令将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-imagemagick
commands
要操作,创建一个新的占位符映像,$newimage = imagecreatetruecolor(…),并引入当前映像。$image = imagecreatefromjpeg('whatever.jpg'),然后您可以使用imagecopyresample()来更改大小,或任何其他内置的非imagemagick命令。
#3
28
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.']'
#4
14
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.)
他的回答和路易斯·梅加蒂的评论。您需要在载入图像之前设置分辨率。
#5
11
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);
#6
8
You can also try executing the 'convert' utility that comes with imagemagick.
您还可以尝试执行imagemagick附带的“convert”实用程序。
exec("convert pdf_doc.pdf image.jpg");
echo 'image-0.jpg';
#7
3
I install finished! It's worked!
我安装完成!这是工作!
You may be do base install imagemagick on windows.
你可以在windows上安装imagemagick。
In php (local)
use call exec(<command line>)
ex:
在php(本地)中使用call exec( <命令行> )ex:
<?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 imagick
in PHP Imagick class
此外,还可以在PHP imagick类中使用类imagick
Thanks all helped me!
谢谢所有帮助我!
#8
3
I'm the author of PDFlib which is a GhostScript wrapper for php, advantage of using this library is, it is already tested and it does not require ImageMagic
我是PDFlib的作者,它是php的代写脚本包装器,使用这个库的好处是,它已经经过了测试,并且不需要ImageMagic
Always GhostScript
commands are faster than ImageMagic
when it comes to pdf so you should either go for a GhostScript wrapper or pure GhostScript commands
当涉及到pdf时,GhostScript命令总是比ImageMagic要快,所以您应该使用GhostScript包装或者纯GhostScript命令
$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->convert();
#9
1
Here is a simple class I've written and used on a couple of projects. It just wraps imagick
and handles writing each page out to disk. If anyone is still looking for an easy way to do this, this link might be helpful.
下面是我在几个项目中编写和使用的一个简单类。它只是包装imagick并处理将每个页面写到磁盘上。如果有人还在寻找一种简单的方法来实现这一点,这个链接可能会有所帮助。
#10
0
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
#1
203
You need ImageMagick
and GhostScript
你需要ImageMagick和GhostScript
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The [0]
means page 1
.
[0]表示第1页。
#2
30
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一起工作。使用exec()运行ghostscript命令将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-imagemagick
commands
要操作,创建一个新的占位符映像,$newimage = imagecreatetruecolor(…),并引入当前映像。$image = imagecreatefromjpeg('whatever.jpg'),然后您可以使用imagecopyresample()来更改大小,或任何其他内置的非imagemagick命令。
#3
28
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.']'
#4
14
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.)
他的回答和路易斯·梅加蒂的评论。您需要在载入图像之前设置分辨率。
#5
11
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);
#6
8
You can also try executing the 'convert' utility that comes with imagemagick.
您还可以尝试执行imagemagick附带的“convert”实用程序。
exec("convert pdf_doc.pdf image.jpg");
echo 'image-0.jpg';
#7
3
I install finished! It's worked!
我安装完成!这是工作!
You may be do base install imagemagick on windows.
你可以在windows上安装imagemagick。
In php (local)
use call exec(<command line>)
ex:
在php(本地)中使用call exec( <命令行> )ex:
<?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 imagick
in PHP Imagick class
此外,还可以在PHP imagick类中使用类imagick
Thanks all helped me!
谢谢所有帮助我!
#8
3
I'm the author of PDFlib which is a GhostScript wrapper for php, advantage of using this library is, it is already tested and it does not require ImageMagic
我是PDFlib的作者,它是php的代写脚本包装器,使用这个库的好处是,它已经经过了测试,并且不需要ImageMagic
Always GhostScript
commands are faster than ImageMagic
when it comes to pdf so you should either go for a GhostScript wrapper or pure GhostScript commands
当涉及到pdf时,GhostScript命令总是比ImageMagic要快,所以您应该使用GhostScript包装或者纯GhostScript命令
$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->convert();
#9
1
Here is a simple class I've written and used on a couple of projects. It just wraps imagick
and handles writing each page out to disk. If anyone is still looking for an easy way to do this, this link might be helpful.
下面是我在几个项目中编写和使用的一个简单类。它只是包装imagick并处理将每个页面写到磁盘上。如果有人还在寻找一种简单的方法来实现这一点,这个链接可能会有所帮助。
#10
0
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