将PDF文档的一部分呈现为图像文件需要哪些库、扩展等?
我发现大多数PHP PDF库都围绕着创建PDF文档,但是是否有一种简单的方法可以将文档渲染成适合web使用的图像格式呢?
我们的环境是一个LAMP堆栈。
将PDF文档的一部分呈现为图像文件需要哪些库、扩展等?
我发现大多数PHP PDF库都围绕着创建PDF文档,但是是否有一种简单的方法可以将文档渲染成适合web使用的图像格式呢?
我们的环境是一个LAMP堆栈。
当前回答
使用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;
?>
(Paolo Bergantino的回答和Luis Melgratti的评论。您需要在加载图像之前设置分辨率。)
其他回答
使用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;
?>
(Paolo Bergantino的回答和Luis Melgratti的评论。您需要在加载图像之前设置分辨率。)
你可以使用下面的库来使用javascript将pdf转换为图像
http://usefulangle.com/post/24/pdf-to-jpeg-png-with-pdfjs
如果你从一个blob中加载PDF,这是你如何获得第一页而不是最后一页:
$im->readimageblob($blob);
$im->setiteratorindex(0);
你需要ImageMagick和GhostScript
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
[0]表示第一页。
我安装完毕!这是工作!
你可以在windows上安装imagemagick。
在php(本地)使用调用exec(<命令行>) 例:
<?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");
?>
此外,你可以在PHP imagick类中使用类imagick
谢谢大家的帮助!