Here is a simple example how to create a preview of PDF document's first page:
PHP:
The number between [] is the number of the page starting from 0.
Here is a simple example how to create a preview of PDF document's first page:
The number between [] is the number of the page starting from 0.
This entry was posted on September 7, 2007, 11:43 pm and is filed under Imagick, PHP stuff. You can follow any responses to this entry through RSS 2.0. You can leave a response, or trackback from your own site.
Arclite theme by digitalnature | powered by WordPress
#1 by Sylvain Filteau on December 6, 2007 - 12:01 am
Quote
Is it possible to do this when loading the pdf with the method readImageBlob ?
#2 by Mikko Koppanen on December 6, 2007 - 2:58 am
Quote
Sylvain,
yes.
#3 by Sylvain Filteau on December 6, 2007 - 4:27 pm
Quote
And…. how to do that ?
#4 by Mikko Koppanen on December 6, 2007 - 4:58 pm
Quote
Sylvain,
It should be no different that $im->readImageBlob( “stringhere” );
That might require some tuning on the PDF delegate thou. I can test it later!
#5 by Sylvain Filteau on December 6, 2007 - 6:54 pm
Quote
Hum, I think I was not specific enough in my question, sorry
Loading the entire PDF with readImageBlob is ok, what I’m not able to do is selecting the first page of the PDF with this method.
#6 by Mikko Koppanen on December 7, 2007 - 3:34 am
Quote
Sylvain,
what do you mean by selecting the page? At that point you have read the whole PDF in to memory so you can use Imagick::setImageIndex/setIteratorIndex to select a single page.
#7 by JC on December 30, 2007 - 2:27 pm
Quote
Hi,
i am trying to have pdf page displayed by ImageMagick on a web page through PHP.
If I use your code, I get :
“Uncaught exception ‘ImagickException’ with message ‘Unable to read the file:” & the name of my pdf file.
If I use the same command to open an image file, I get a correct result.
I am usinf Imagemagick 6.3.7.8-Q8 and Imagick 2.1.0 RC1.
if you have any idea, it could be welcome since I am completely stuck !
by the way, congratulations for your blog which was of
#8 by JC on December 30, 2007 - 2:27 pm
Quote
great support ! (oops)
#9 by Mikko Koppanen on December 30, 2007 - 2:29 pm
Quote
JC,
seems like it’s an issue with the pdf delegate. Can you test the same code from command line? Have you got ghostscript installed?
#10 by JC on December 30, 2007 - 2:42 pm
Quote
one more thing : I finally manage to have it working with my Linux. the question remains on windows…
#11 by JC on December 30, 2007 - 2:43 pm
Quote
I will try the code from the command line on windows.
#12 by JC on December 30, 2007 - 2:48 pm
Quote
ok, from the command line, windows says Postcript delegate failed…
looks like I have something missing (Ghostscript ?)
#13 by Mikko Koppanen on December 30, 2007 - 2:50 pm
Quote
JC,
You need Ghostscript.
#14 by JC on December 30, 2007 - 2:56 pm
Quote
ok thanks ! I’ll try to sort out how to install it on windows.
you can remove or regroup my comments from here, as they look more like spam than real comments….
thanks for you blog, it is really worthed reading !
#15 by James on January 21, 2008 - 2:20 pm
Quote
Thanks for the demo! I’m completely new to ImageMagick, and I’m struggling to get the PDF preview to work.. I have tried this with several different PDFs, and when I try to open the file in FireFox, I always get the message:
The image “http://www.myserver.com/pdf_prev/preview.php†cannot be displayed, because it contains errors.
In IE I only get a little box with a red X in it.
Do you have any idea what could cause this? The PDFs I’m using were all created using either Adobe Acrobat or Photoshop. The code I’m using is exactly what you’ve posted above, I even renamed my PDFs to “test.pdf”
Any help would be very much appreciated!
#16 by James on January 22, 2008 - 2:21 am
Quote
Sorry, please ignore my previous post. I have finally figured out the problem, it was very simple (although it took me a long time to notice). I had a space in the PHP file before the opening <?php tag.
Sorry for bothering you with such a trivial problem.
#17 by Arnaud on February 18, 2008 - 5:24 pm
Quote
Hello,
When converting a PDF document to PNG there is an issue with resolution. When the document is read into an imagick object the resolution is 72 dpi. The result is barely readable and not really printable when converted to PNG.
When using the convert command-line tool I can achieve the result I need by using the -density option. e.g.: convert -density 150 myfile.pdf myfile.png
I tried using the following code but the generated image is pretty small when viewed at 100%
$images = new Imagick(‘myfile.pdf’);
foreach ($images as $k => $image) {
$filename = ‘foo’ . $k . ‘.png’;
if (file_exists($filename)) {
unlink($filename);
}
print_r($image->getImageResolution());
echo “\n”;
$image->setImageFormat(‘png’);
$image->setImageUnits(Imagick::RESOLUTION_PIXELSPERINCH);
$image->setImageResolution(150, 150);
print_r($image->getImageResolution());
echo “\n”;
$image->writeImage($filename);
}
Am I missing something ?
#18 by Mikko Koppanen on February 19, 2008 - 11:12 pm
Quote
Arnaud,
what does identify -verbose file give you ?
#19 by Scott F on February 27, 2008 - 11:42 pm
Quote
Thanks again
#20 by Timo on February 28, 2008 - 7:03 pm
Quote
Hi,
i’ve got the the same problem like arnaud. i’d like to read a pdf into imagick with more then 72dpi.
at the console it is possible to write:
convert -density 300×300 /home/timo/test.pdf[1] test.jpg
so i need something like:
$im = new imagick();
$im->setDensity(300, 300);
$im->readImage(‘/home/timo/test.pdf[1]‘);
thanks in advance
#21 by Mikko Koppanen on February 28, 2008 - 7:33 pm
Quote
Hi,
if I have understood you correctly, try this:
$im = new imagick();
$im->setResolution(300, 300);
$im->readImage(’/home/timo/test.pdf[1]’);
If it does not do the trick try: http://uk.php.net/manual/en/function.imagick-resampleimage.php
#22 by Timo on February 28, 2008 - 8:24 pm
Quote
thanks for the quick response!!!!
is there a way to read out the images in a higher resolution than 72dpi directly from the pdf? with $im->setResolution(300, 300) i can change the resolution afterwards, but the source output still is 72 dpi. But since we are dealing with vector graphics within the pdf heremust be away to excerpt those in a higher quality. or not?
with $im->resampleimage() i can change the resolution afterwards too, but i cant fit the source resolution …
the density property on the console makes this ‘magic’ thing
#23 by Mikko Koppanen on February 28, 2008 - 8:29 pm
Quote
Timo,
you call setResolution before reading. So it’s the initial resolution. My understanding is that formats like PDF and SVG don’t have resolution at all.
#24 by Timo on February 28, 2008 - 8:53 pm
Quote
Hello Mikko,
you´re totally right. But AFTER $im->readImage(’/home/timo/test.pdf[1]’); the resolution result of the images excerpted out of the pdf is 72 dpi, and then blowed up by the document (300 dpi) so the script puts a low quality image onto the page we created for 300 dpi.
The same problem appears trying to render vector informations out the pdf.
Using the console typing: “convert -density 300×300 /home/timo/test.pdf[1] test.jpg” leads to an internal rendering process that fetches the informations (vectors, fonts, pictures etc.) with 300dpi. How is this possible with IMagick without a loss of quality?
Thanks for your pacience and a very quick and great support
#25 by Mikko Koppanen on February 28, 2008 - 9:22 pm
Quote
Timo,
I’ll ask that from the ImageMagick guys. Now I need to run to the PHPLondon conference social event!
#26 by Lyno on March 7, 2008 - 11:47 am
Quote
Could anyone tell me if there is a similar solution using ASP?
#27 by Matt C on May 8, 2008 - 5:58 pm
Quote
Hi Mikko,
Hmm, I seem to get an image that looks like an old tv when its not tuned in properly… Am I missing an important lib or setting?
Cheers
#28 by Matt C on May 8, 2008 - 6:02 pm
Quote
Fixed it… was to do with the resolution again!! I should read everything before I post!! … Thanks for the great site
#29 by Nick on June 11, 2008 - 3:00 pm
Quote
Can someone please clarify the use of ReadImageBlob for loading the PDF into the $im var?
I’m attempting this:
$im = new Imagick();
$im -> readImageBlob($im, $BinData);
$im -> setImageIndex(0);
$im -> setImageFormat(“png”);
echo $im;
The PHP manual is no help at all.
Pingback: Previsualizar un archivo PDF con PHP « CuatroXL - Cuatro Xl
#30 by tib on July 2, 2008 - 3:46 pm
Quote
Damn, it is too good to be true!!
What I need to install in order to be able to successfully run this script ?
Can anyone tell how to make things works on a Windows system?
thanks
#31 by Jeremy D on August 6, 2008 - 8:51 pm
Quote
I’m using php 5.2.6 on windows, imagemagick 6.4.2, ghostscript 8.6.3. Imagick works fine for gif, jpeg, and png, but I get this error every time I try to read a pdf file:
Fatal error: Uncaught exception ‘ImagickException’ with message ‘Unable to read the file: test.pdf’ in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\pdf\test.php:3 Stack trace: #0 C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\pdf\test.php(3): Imagick->readimage(‘test.pdf’) #1 {main} thrown in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\pdf\test.php on line 3
Here’s my code:
readImage(‘test.pdf’);
$thumb->writeImage(‘test.jpg’);
$thumb->clear();
$thumb->destroy();
?>
The following works in a command prompt:
convert test.pdf test.jpg
Thanks in advance
#32 by tiandiwonder on September 3, 2008 - 10:37 am
Quote
when i run
i got unable to read file ‘test.pdf’;
i changed to
readImageBlob(‘test.pdf’);
?>
i got error:
Fatal error: Uncaught exception ‘ImagickException’ with message ‘UnableToOpenCon
figureFile `delegates.xml” in C:\web\intra_malabs_com\test.php:3
Stack trace:
#0 C:\web\intra_malabs_com\test.php(3): Imagick->readimageblob(’1.pdf’)
#1 {main}
thrown in C:\web\intra_malabs_com\test.php on line 3
i’v installed ImageMagick-6.4.3-Q16 and gs8.63. php 5.0, imagick dll version is 2.2.1-dev
#33 by Mikko Koppanen on September 3, 2008 - 11:46 am
Quote
tiandiwonder,
You have problems with ImageMagick configuration, particulary delegates.xml
#34 by tiandiwonder on September 5, 2008 - 3:04 am
Quote
thank you, Koppanen.
I just refered your blog and cofigured the MAGICK_HOME.
then the error message is :
Error: /undefinedfilename in (72×72)
Operand stack:
Execution stack:
%interp_exit .runexec2 –nostringval– –nostringval– –nostringval-
- 2 %stopped_push –nostringval– –nostringval– –nostringval– fa
lse 1 %stopped_push
Dictionary stack:
–dict:1154/1684(ro)(G)– –dict:0/20(G)– –dict:70/200(L)–
Current allocation mode is local
Last OS error: No such file or directory
GPL Ghostscript 8.63: Unrecoverable error, exit code 1
Fatal error: Uncaught exception ‘ImagickException’ with message ‘Postscript dele
gate failed `C:\web\intra_malabs_com\1.pdf” in C:\web\intra_malabs_com\test.php
:3
Stack trace:
#0 C:\web\intra_malabs_com\test.php(3): Imagick->__construct(’1.pdf’)
#1 {main}
thrown in C:\web\intra_malabs_com\test.php on line 3
Do i need to change the delegate.xml?
#35 by tiandiwonder on September 5, 2008 - 5:02 am
Quote
it’s ok to use ms-dos to execute cmd:
convert test.pdf test.jpg
i run php program as administrator, so the premission error is excluded. what do ‘undefinedfilename’ mean? which file?
#36 by Stefan on September 17, 2008 - 12:34 pm
Quote
I have the the same (unsolved?) problem Timo metioned long time ago:
When I read a PDF with imagick it is only 72dpi. But I need 300dpi – does anyone have a solutions for this?
#37 by Alessandro Coletti on September 26, 2008 - 10:43 am
Quote
Stefan,
Just do this:
exec(“convert -density 300 test.pdf test.png”);
Have a great day
#38 by Martin on October 23, 2008 - 12:29 pm
Quote
Mikko,
thank you for your code example. PDF preview (first page) works fine.
$image = new Imagick();
$image -> readImageBlob($bytecode);
$image -> setImageIndex(0);
$image -> setImageFormat(‘png’);
$image -> scaleImage(200,200,1);
header(‘Content-type: image/png’);
echo $image;
But precessing is slowing down to ~ 10 seconds when the document has 50 pages (size: 1.3 MB). I guess, imagemagick is scaling all the 50 pages. Is there any possibility to avoid the processing of second and following pages?
Best regards
#39 by Martin on October 23, 2008 - 1:57 pm
Quote
Update:
microtime() informed me that not the resize process takes the time, but the readImageBlob() function…
#40 by Martin Beek on November 4, 2008 - 6:26 pm
Quote
Mikko,
i have a traditional imagemagick commandlne that i am struggeling with. Whatever i try, i can’t get it to work using iMagick.
Here is the line:
convert -density 180 -define pdf:use-cropbox=true -colorspace CMYK ./template.pdf -quality 90 -resize 320 -colorspace RGB test.jpg
The order of the parameters is very important. The original CMYK pdf is using its cropbox (ghostscript parameter) and resized and forced to RGB. If i leave out any of these steps, the result is not what i want.
In imagick i can not find a way to do the -define part and i get horrible colors.
Your help is much appreciated. The template pdf can be downloaded from http://easyorder.xsprint.eu/test/template.pdf
Thanks again,
Martin Beek
#41 by RENE on November 7, 2008 - 7:03 am
Quote
¿¿¿¿HOW can I convert a complete PDF FIle in images??????? DO youhave an idea??? please
#42 by Matt on February 26, 2009 - 4:02 am
Quote
Hello-
I’m attempting to convert PDF files into PNGs. It works great from the command line (I do have Ghostscript 8.64 installed). But from PHP I’m having a problem:
code:
$im = new Imagick($pdf_file); // this is where it throws the exception below
output:
Fatal error: Uncaught exception ‘ImagickException’ with message ‘Postscript delegate failed `23_1235606503.pdf’: No such file or directory @ pdf.c/ReadPDFImage/612′ in get_thumbnail.php:93
Stack trace:
#0 get_thumbnail.php(93): Imagick->__construct(‘…’)
blah blah
Any help will be greatly appreciated!!!!
#43 by Mikko Koppanen on February 26, 2009 - 12:54 pm
Quote
Matt,
check that gs in the $PATH for the webserver user.
#44 by Matt on February 26, 2009 - 8:09 pm
Quote
Thanks Mikko!! Actually I just figured that out and was about the post the news of success.
Though one thing I did not figure out was how to add /usr/local/bin to Apache’s environment path variable. I tried the SetEnv PassEnv directives, and also the php function putenv(). None of these worked. The way I got it working was to place a symbolic link to /usr/local/bin/gs in /usr/bin.
#45 by TheBaud on April 6, 2009 - 5:14 pm
Quote
Hi !
I had exactly the same issue as Matt.
And linking gs to /usr/bin did the trick.
thanks for that !
#46 by Chris B on May 11, 2009 - 5:20 am
Quote
Because PDF’s don’t have a resolution, you need to set it before you read the pdf.
$im->setResolution(300, 300);
$im->readImage($pdf);
…
also, be warned that 600dpi takes a very long time to generate (>30sec)
#47 by momo on June 2, 2009 - 11:49 am
Quote
I need to display this pdf preview icon (first page) in an html page, which contain html text and this preview. Is it possible to do that since the header already sent use image/png? What about my text?
#48 by Christian on June 10, 2009 - 2:16 pm
Quote
Hi,
/* Read page 1 */
$im = new imagick();
$im->setResolution(300, 300);
$im->readImage($pdf_file_name);
/* Convert to png */
$im->thumbnailImage(400, null);
$im->setImageColorspace(1);
$im->setImageFormat( “jpg” );
/* Send out */
$im->writeImage($jpg_file_name);
does crop a bit of the pdf on the right and the bottom side. Why? How can i force imagick to converte the whole/complete pdf?
thanks a lot,
Christian
#49 by Jason Dugmore on July 3, 2009 - 12:27 pm
Quote
Thank you! thank you! thank you! I have been trying to figure out a way of working with PDFs in PHP for ages!