Page 1 of 2

ImageMagick can't render PDF files to JPEG

Posted: 2015-05-19T17:06:36-07:00
by gustavopi
Hi. I did a simple php code that works in the host:

Code: Select all

$urlArq = $_REQUEST["url"];
$pag = $_REQUEST["pag"];
$largura = $_REQUEST["w"];
$a = (297/210);
$altura = round($largura*$a);
$Img = new Imagick();
//$Img->setSize($largura, $altura);
$Img->setResolution(150, 150);
$Img->readImage($urlArq."[".$pag."]");
$Img = $Img->flattenImages();
$Img->setImageFormat("jpeg");
$imgw = $Img->getImageWidth();
$imgh = $Img->getImageHeight();
//$image = $Img->getImageBlob();
//$image = $Img->thumbnailImage($largura, NULL);
$imagefull = imagecreatefromstring($Img->getImageBlob());
$image = imagecreatetruecolor($largura, $altura);
imagecopyresampled($image, $imagefull, 0, 0, 0, 0, $largura, $altura, $imgw, $imgh);
if($image){
	header("Content-Type: image/jpeg");
	echo(imagejpeg($image));
} else {
	echo("Erro! Imagem não criada.");
}
But in my local test server, it does not. The error in the php log is:
PHP Fatal error: Uncaught exception 'ImagickException' with message 'Postscript delegate failed `/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/713' in /Library/WebServer/Documents/www/sintran/conteudo_apitaco_jpeg.php:10
It seams ImageMagick can't find Ghostscript, can't use it or can write where suppose to. I have a Mac OS X Lion, so I get the Ghostscript 9.16 from Richard Koch Home Page (http://pages.uoregon.edu/koch/). I talk with him but he said that it's better get the source code and do my own compilation. The source are in http://www.ghostscript.com. The 9.16 tar gz file didn't open so I taken 9.15. I also had to in install Xcode and Command Line Tools… all day fixing bugs! So I tried to compile using Richard's instructions, but the Terminal is full of error messages, I don't know where to start.

The gs still works, the gs -v command returns
GPL Ghostscript 9.16 (2015-03-30)
Copyright (C) 2015 Artifex Software, Inc. All rights reserved.
It means to me the version I tried to compile was not compiled or not installed.

I think I need more information, but I don't know how to get it, don't know exactly what to answer to Richard so he can help me with Ghostscript - if the problem is Ghostscript.

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-19T17:20:11-07:00
by magick
It could be a permissions problem. Try from the command line:
  • convert -verbose /Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf page.jpg

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-19T18:13:55-07:00
by gustavopi
It returns: -bash: convert: command not found

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-20T05:18:13-07:00
by magick
You have ImageMagick installed since you are using it with iMagick. Find out where its installed and look for the convert command.

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-20T11:41:48-07:00
by gustavopi
Could not find convert, only the .so module inside Mamp. So I decided to install ImageMagic using MacPorts (port install ImageMagick).

A lot of things were made, no error were found. So I try the convert test again:

Code: Select all

 convert -verbose /Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf page.jpg
/var/tmp/magick-83151B5t3M6M0Y2Tr1 PNG 595x842 595x842+0+0 8-bit sRGB 348KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr2 PNG 595x842 595x842+0+0 8-bit sRGB 373KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr3 PNG 595x842 595x842+0+0 8-bit sRGB 222KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr4 PNG 595x842 595x842+0+0 8-bit sRGB 166KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr5 PNG 595x842 595x842+0+0 8-bit sRGB 380KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr6 PNG 595x842 595x842+0+0 8-bit sRGB 428KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr7 PNG 595x842 595x842+0+0 8-bit sRGB 132KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr8 PNG 595x842 595x842+0+0 8-bit sRGB 333KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr9 PNG 595x842 595x842+0+0 8-bit sRGB 303KB 0.000u 0:00.000
/var/tmp/magick-83151B5t3M6M0Y2Tr10 PNG 595x842 595x842+0+0 8-bit sRGB 284KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[0] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[1] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[2] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[3] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[4] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[5] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[6] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[7] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[8] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf[9] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-0.jpg[0] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-1.jpg[1] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-2.jpg[2] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-3.jpg[3] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-4.jpg[4] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-5.jpg[5] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-6.jpg[6] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-7.jpg[7] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-8.jpg[8] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
/Library/WebServer/Documents/www/sintran/apitaco/edicao-03-005-12-2013.pdf=>page-9.jpg[9] PDF 595x842 595x842+0+0 16-bit sRGB 348KB 0.000u 0:00.000
convert: unable to open image `page-0.jpg': Permission denied @ error/blob.c/OpenBlob/2709.
[ghostscript library] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72"  "-sOutputFile=/var/tmp/magick-83151B5t3M6M0Y2Tr%d" "-f/var/tmp/magick-83151-OCpXsEjUV5X" "-f/var/tmp/magick-83151gq1kAcILER2I"gustavopi-macmini:/ gustavopi$ 
It seams there was a permission problem at the end.

I try to run my php code and got the same error again. Also php info shows the same version of imagick module: 3.1.2. But I don't think I'm still at the same point, just not sure where to go now.

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-20T13:02:30-07:00
by Bonzo
The first thing I would do is try something simple: try running some code on an image in the same folder as the php code.

Code: Select all

$im = new Imagick('someImage.jpg');
$im->resizeImage(100,100,Imagick::FILTER_LANCZOS,1);
$im->writeImage('output.png');
$im->destroy();

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-20T14:33:05-07:00
by gustavopi
Bonzo: the test works!

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-21T00:13:57-07:00
by Bonzo
This means Imagemagick and Imagick are working and installed correctly; I would now try the same thing with your pdf in the same folder as the code. I suspect you can not read a file outside of the localhost folder.
You could also try reading a jpg in the same folder as your original pdf which may point to a ghostscript problem if the jpg file works.

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-21T06:04:29-07:00
by gustavopi
So, this folder I created for testing purposes, I did using FileZilla and set 777 permissions for everything, starting by the folder itself. I think there is no permissions problem here. I add a "Documento.pdf" file to test with the code…

Code: Select all

<?php
$urlArq = "Documento.pdf";
$pag = 0;
$largura = 480;
$a = (297/210);
$altura = round($largura*$a);
$Img = new Imagick();
//$Img->setSize($largura, $altura);
$Img->setResolution(150, 150);
$Img->readImage($urlArq."[".$pag."]");
$Img = $Img->flattenImages();
$Img->setImageFormat("jpeg");
$imgw = $Img->getImageWidth();
$imgh = $Img->getImageHeight();
//$image = $Img->getImageBlob();
//$image = $Img->thumbnailImage($largura, NULL);
$imagefull = imagecreatefromstring($Img->getImageBlob());
$image = imagecreatetruecolor($largura, $altura);
imagecopyresampled($image, $imagefull, 0, 0, 0, 0, $largura, $altura, $imgw, $imgh);
if($image){
	header("Content-Type: image/jpeg");
	echo(imagejpeg($image));
} else {
	echo("Erro! Imagem não criada.");
}
/*
$im = new Imagick('Foto.jpg');
$im->resizeImage(100,100,Imagick::FILTER_LANCZOS,1);
$im->writeImage('output.png');
$im->destroy();
*/
?>
And the log was…
[21-May-2015 09:56:49 America/Sao_Paulo] PHP Fatal error: Uncaught exception 'ImagickException' with message 'Postscript delegate failed `Documento.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/713' in /Library/WebServer/Documents/www/teste/teste.ImageMagick.php:10
Stack trace:
#0 /Library/WebServer/Documents/www/teste/teste.ImageMagick.php(10): Imagick->readimage('Documento.pdf[0...')
#1 {main}
thrown in /Library/WebServer/Documents/www/teste/teste.ImageMagick.php on line 10
The problem really occurs when trying to read de PDF, where I think Ghostscript is called. I don't know how it works, if will be called "convert" command and write some temporary file for ImageMagick to process - maybe in this "working area" there is a permissions problem.

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-21T13:45:13-07:00
by Bonzo
What happens if you create a variable with the image name and use that in readImage?

Code: Select all

$document = $urlArq."[".$pag."]";
// Check what $document actualy contains
echo $document;
$Img->readImage($document);

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-22T12:36:02-07:00
by gustavopi
The $document contains "Documento.pdf[0]". I think is right because the code I show in the first post is working properly in the webserver. The problem is in my local testing server.

You can see the in the website http://www.sintran.org.br/ the ImageMagick doing it's magic! In the right side, above the text "Leia o Apitaço" will open the first page of the last edition of the Apitaço news, a PDF document. This does not happens in my local version of the page. The problem is it's holding the Ajax execution to render the, so I got to improve this page, but is difficult testing online, with everybody accessing along with my tests.

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-23T07:47:59-07:00
by gustavopi
I did another test by copying a line from another post of this community:

Code: Select all

convert -colorspace RGB -interlace none -density 104.6x104.6 -quality 100 -bordercolor white Documento.pdf teste.jpg
I just change the file names, I don't know how this convert command works. By typing this in Terminal inside the test folder (all local files), 8 jpeg files were created, for each page of PDF document. Is not the same result as ImageMagick, the quality is poor, by I think we can say Ghostscript is working properly in the job of rendering the PDF document. The problem really seams to be how Ghostscript will work for ImageMagick. By a temporary file? In this case, where it tries to write? I think the problems might be:

1 - Ghostscript can't write were suppose to.
2 - ImageMagick can't read the file written by Ghostscript.

Anybody can say where is this doomed place?

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-23T08:15:13-07:00
by magick
For better quality, supersample:
  • convert -colorspace RGB -interlace none -density 418.4x418.4 -quality 100 -bordercolor white Documento.pdf -resize 25% teste.jpg

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-23T11:26:20-07:00
by gustavopi
Ok, magick, but I'm not worry about the quality right now.

I need to know how imagemagick uses ghostscript, as described in my post above.

Re: ImageMagick can't render PDF files to JPEG

Posted: 2015-05-23T11:34:26-07:00
by snibgo
"-verbose" will show you the GS command. Is that what you want?