0

I am using imagick with PHP/Windows IIS. I have a simple script that converts a TIF file to PDF an presents it to the browser. It works flawlessly with single page TIF files, but with multiple pages, its only showing the last page.

I understand that it shows the last page by default because the $im variable is an array. Any attempt I make to fix it makes it an invalid PDF. Below is my code. I am new to imagick and any help is appreciated!

$im = new imagick("tmp/tmp.tif");
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im;

ImageMagick version ImageMagick 7.0.7-11 Q16 x64 2017-11-23

ImageMagick library version ImageMagick 7.0.7-11 Q16 x64 2017-11-23

(this is very rough testing code, I will clean it up later)

1 Answer 1

3

The internal image iterator is pointing at the last page read. You just need to reset it to the first page with Imagick::setFirstIterator.

$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
header('Content-Type: application/pdf');
echo $im->getImage();

Or even

$im->setIteratorIndex(0);

Edit based on comments

If you are attempt to output the entire PDF document, you would use Imagick::getImagesBlob.

$im = new imagick("tmp/tmp.tif");
$im->setFirstIterator();
$im->setImageFormat('pdf');
$blob = $im->getImagesBlob();
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($blob));
echo $blob;
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks @emcconville! Now it displays the first page, but only the first :( even doing a foreach loop on $im only displays the first page :(
You may need to updated the question with what you're expecting, or at least clarify. The Image.getImage will only return one page. If you want to give the whole document to the browser, then just call echo $im; and not bother with page iterators.
Well I have tried it without the ->getImage as well (just echo $im) and I get the same result, just the LAST page.
Got it working.. I needed to add your $im->setFirstIterator(); as well as change $im->getImage(); to $im->getImagesBlob(); and now it converts all the pages and displays all the pages.
@alexander7567 You should post your solution as another answer, not attempt to edit others. Plus you can accept your own answers as the solution to your original questions (it'll help future readers).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.