Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving invalid filename error when converting first page of pdf to jpg

I'm using imagick to convert a pdf to a jpg in a script I have...it works fine if I give the direct path to the uploaded pdf without the page specified, but when I add the [0] onto the end of the file path to specify that I only want to read the first page, it bombs out with the following error:

"Fatal error: Uncaught exception 'ImagickException' with message 'Invalid filename provided' Imagick->readImage()"

I've also tried using '/path/to/file.pdf[0]' directly in the constructor with no luck, but without the page specifier, that also works fine.

According to the documentation...this should work. What am I doing wrong here?

$doc_preview = new Imagick();
$doc_preview->setResolution(180,180);
$doc_preview->readImage('/path/to/file.pdf[0]');
$doc_preview->setImageFormat('jpeg');
$doc_preview->writeImage('/path/to/file.jpg');
$doc_preview->clear();
$doc_preview->destroy();

UPDATE: I should have mentioned I am using HHVM. Not sure this would make a difference in this case...but I am.

UPDATE2: I have opened an issue over on the HHVM github repo. Hopefully they will fix this bug...until then, the answer I have marked correct below is a decent (albeit hacky) workaround.

like image 900
kevindeleon Avatar asked Feb 02 '15 20:02

kevindeleon


Video Answer


3 Answers

OK...so the (kind of hacky) way to fix this in my situation is to use fopen() and then use setIteratorIndex(0) which is HIGLY unintuitive. But for those having the same problem...there you go!

$pdf_handle = fopen('/path/to/file.pdf', 'rb');

$doc_preview = new Imagick();
$doc_preview->setResolution(180,180);
$doc_preview->readImageFile($pdf_handle);
$doc_preview->setIteratorIndex(0);
$doc_preview->setImageFormat('jpeg');
$doc_preview->writeImage('/path/to/file.jpg');
$doc_preview->clear();
$doc_preview->destroy();
like image 200
kevindeleon Avatar answered Oct 13 '22 22:10

kevindeleon


I don't have imagick in my computer but I can guess what the problem is. The problem is that when you open a PDF file with the page part you cannot include directory path string that includes the forward slash. I think that because I read the readImage function page from php.net here http://php.net/manual/en/imagick.readimage.php and they there don't include directory path so I suppose that that is a glich but continuing. You should try to change directory to the PDF file using chdir()(http://php.net/manual/en/function.chdir.php) and then try the same function like this $doc_preview->readImage('file.pdf[0]'); without the directory path. Most probably it will work. Some API's have gliches and you have to work around them.

like image 42
David Costa Avatar answered Oct 13 '22 22:10

David Costa


readImage() requires that $filename is an absolute path.

If you want to use a relative path, then use realpath() on it, which turns the relative path into an absolute file path.

Instead of

$imagick->readImage('/path/to/file.pdf[0]');

try

$imagick->readImage(realpath("/path/to/file.pdf")."[0]");
like image 29
Jens A. Koch Avatar answered Oct 14 '22 00:10

Jens A. Koch