Cómo convertir un documento PDF a una imagen de vista previa en PHP? [cerrado]
Qué bibliotecas, extensiones, etc. se requiere para representar una parte de un documento PDF a un archivo de imagen?
La mayoría de las bibliotecas PDF PHP que he encontrado se centran en la creación de documentos PDF, pero ¿hay una forma sencilla de renderizar un documento a un formato de imagen adecuado para uso web?
Nuestro entorno es una pila de lámparas.
10 answers
Usted necesita ImageMagick
y GhostScript
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
El [0]
significa page 1
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-08 10:07:50
Para aquellos que no tienen ImageMagick por cualquier razón, las funciones GD también funcionarán, en conjunto con GhostScript. Ejecute el comando ghostscript con exec()
para convertir un PDF a JPG, y manipule el archivo resultante con imagecreatefromjpeg()
.
Ejecute el comando ghostscript:
exec('gs -dSAFER -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sOutputFile=whatever.jpg input.pdf')
Para manipular, cree una nueva imagen de marcador de posición, $newimage = imagecreatetruecolor(...)
, y traiga la imagen actual. $image = imagecreatefromjpeg('whatever.jpg')
, y luego puede usar imagecopyresampled()
para cambiar el tamaño, o cualquier número de otros comandos incorporados que no seanimagemagick
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-04 14:14:09
También puede obtener el número de páginas usando
$im->getNumberImages();
Entonces puede crear pulgares de todas las páginas usando un bucle, por ejemplo.
'file.pdf['.$x.']'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-03 14:47:28
Use la extensión php Imagick. Para controlar el tamaño deseado de la imagen de salida ráster, utilice la función 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;
?>
(Extensión sobre Paolo Bergantino su respuesta y Luis Melgratti su comentario. Es necesario establecer la resolución antes de cargar la imagen.)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-03 16:14:50
Si está cargando el PDF desde un blob, así es como obtiene la primera página en lugar de la última:
$im->readimageblob($blob);
$im->setiteratorindex(0);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-14 04:23:33
También puede intentar ejecutar la utilidad 'convert' que viene con imagemagick.
exec("convert pdf_doc.pdf image.jpg");
echo 'image-0.jpg';
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-01-16 07:32:20
Instalo terminado! Ha funcionado!
Puede ser do base instalar imagemagick en windows.
En php (local)
use call exec(<command line>)
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");
?>
Además, puedes usar class imagick
en PHP Imagick class
Gracias a todos me ayudó!
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-04-07 15:05:49
Soy el autor de PDFlib que es un GhostScript wrapper para php, la ventaja de usar esta biblioteca es que ya está probada y no requiere ImageMagic
Siempre GhostScript
los comandos son más rápidos que ImageMagic
cuando se trata de pdf, por lo que debe elegir un envoltorio GhostScript o comandos GhostScript puros
$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->convert();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-22 03:03:47
Aquí hay una clase simple que he escrito y utilizado en un par de proyectos. Simplemente envuelve imagick
y se encarga de escribir cada página en el disco. Si alguien todavía está buscando una manera fácil de hacer esto, este enlace podría ser útil.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-19 22:43:33
Piense de manera diferente, puede usar la siguiente biblioteca para convertir pdf a imagen usando javascript
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-29 17:58:36