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

3

6

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.

Durbar answered 2/2, 2015 at 20:41 Comment(3)
It's a bug. The feature is not supported, but repeated all over the PHP manual. Duplicate: https://mcmap.net/q/1778081/-pdf-to-jpg-imagic-page-selection/1163786 Solution using setIteratorIndex() over at: https://mcmap.net/q/1778081/-pdf-to-jpg-imagic-page-selectionMilfordmilhaud
Good find...I figured it out by reading through all the supported methods...but still...it's unintuitive at best. I also don't consider my solution a duplicate solution as I am not trying to iterate and produce multiple images...and therefore was unable to find that solution when searching earlier.Durbar
Sorry, Kevin - i didn't notice that this issue relates to the HHVM extension.Milfordmilhaud
D
4

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();
Durbar answered 2/2, 2015 at 21:23 Comment(0)
E
1

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.

Eristic answered 2/2, 2015 at 20:55 Comment(1)
I appreciate the guess, but I'm not sure using chdir() is a good option. If this really is the issue...that's a disappointing bug.Durbar
M
1

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]");
Milfordmilhaud answered 2/2, 2015 at 23:7 Comment(1)
Except I'm already using absolute server paths...not relative paths...which is why it works if I don't use the page specifier on the end. The problem is with PHP/Imagemagick not accepting a filename with a page specifier if the path is also given...which seems to be a bug. It should accept the filename with page specifier or full path to file with page specifier, but it doesn't. I'll test to be certain though.Durbar

© 2022 - 2024 — McMap. All rights reserved.