Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating two pdf pages with Imagick

Tags:

php

pdf

imagick

Currently i can create PDF files from images in Imagick with this function

$im->setImageFormat("pdf");
$im->writeImage("file.pdf");

And it's possible to fetch multiple pages with imagick like this

$im = new imagick("file.pdf[0]");
$im2 = new imagick("file.pdf[1]");

But is it possible to save two image objects to two pages? (example of what i am thinking, its not possible like this)

$im->setImageFormat("pdf");
$im->writeImage("file.pdf[0]");

$im2->setImageFormat("pdf");
$im2->writeImage("file.pdf[1]");
like image 308
Ólafur Waage Avatar asked Feb 13 '09 12:02

Ólafur Waage


2 Answers

I know this is long past due, but this result came up when I was trying to do the same thing. Here is how you create a multi-page PDF file in PHP and Imagick.

    $images = array(
    'page_1.png',
    'page_2.png'
);
$pdf = new Imagick($images);
$pdf->setImageFormat('pdf');
if (!$pdf->writeImages('combined.pdf', true)) {
    die('Could not write!');
}
like image 145
Mitch C Avatar answered Sep 20 '22 16:09

Mitch C


Accepted answer wasn't working for me, as a result, it always generated one page pdf (last image from constructor), to make this work I had to get file descriptor first, like this:

$images = array(
  'img1.png', 'img2.png'
); 
$fp = fopen('combined.pdf', 'w');
$pdf = new Imagick($images);
$pdf->resetiterator();
$pdf->setimageformat('pdf');
$pdf->writeimagesfile($fp);
fclose($fp);
like image 26
wojtek Avatar answered Sep 19 '22 16:09

wojtek