Convert Array of Tiff Images to PDF

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
xhenxhe

Convert Array of Tiff Images to PDF

Post by xhenxhe »

Since MagickWand is no longer currently supported for IIS, I'm changing my functions from using MagickWand to IMagick.

Can someone help me with the process to convert an array of tiff image paths to a PDF file? Here is the MagickWand code I was using...

Code: Select all

	function savePagesAsPDF($pages, $saveAs='temp.pdf')
	{
		if ($pages)
		{
			$w = NewMagickWand();
			MagickAppendImages($w);

			foreach ($pages as $p)
			{
				MagickReadImage($w, $p);
				// Convert image to jpg compression (otherwise it will convert jpgs to tifs and they will be HUGE)
				MagickSetImageCompression($w, MW_JPEGCompression);
				MagickSetImageCompressionQuality($w, 40); // 40% compression
			}

			MagickSetFormat($w, 'PDF' );
			MagickWriteImages($w, $saveAs, true);
		}
		return $saveAs;
	}
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: Convert Array of Tiff Images to PDF

Post by mkoppanen »

xhenxhe wrote:Since MagickWand is no longer currently supported for IIS, I'm changing my functions from using MagickWand to IMagick.

Can someone help me with the process to convert an array of tiff image paths to a PDF file? Here is the MagickWand code I was using...

Code: Select all

	function savePagesAsPDF($pages, $saveAs='temp.pdf')
	{
		if ($pages)
		{
			$w = NewMagickWand();
			MagickAppendImages($w);

			foreach ($pages as $p)
			{
				MagickReadImage($w, $p);
				// Convert image to jpg compression (otherwise it will convert jpgs to tifs and they will be HUGE)
				MagickSetImageCompression($w, MW_JPEGCompression);
				MagickSetImageCompressionQuality($w, 40); // 40% compression
			}

			MagickSetFormat($w, 'PDF' );
			MagickWriteImages($w, $saveAs, true);
		}
		return $saveAs;
	}
I would propably try something like this:

Code: Select all

<?php

/* Create new Imagick object */
$im = new imagick( $pages );
$im->setFormat( 'PDF' );

$im->setImageCompression( Imagick::COMPRESSION_JPEG );
$im->setImageCompressionQuality( 40 );


$im->writeImages( 'temp.pdf', true );

?>


Mikko Koppanen
My blog: http://valokuva.org
xhenxhe

Re: Convert Array of Tiff Images to PDF

Post by xhenxhe »

Thanks mkoppanen. Wow, that seems to be much simpler code. It doesn't quite work though. The first page is ok, but other pages are not. If I have 3 files paths in my array, the first page is ok, but the next two pages are not created.
Attachments
Here is an example of what I mean.
Here is an example of what I mean.
pdf-problem.gif (13.71 KiB) Viewed 20863 times
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: Convert Array of Tiff Images to PDF

Post by mkoppanen »

I will check that later today! I'll post back results here.
Mikko Koppanen
My blog: http://valokuva.org
xhenxhe

Re: Convert Array of Tiff Images to PDF

Post by xhenxhe »

Thanks. I've been playing with it. It seems if I do something like this

Code: Select all

$im = new Imagick($pages);
$im->setFormat('PDF');
$im->setCompression( Imagick::COMPRESSION_JPEG );
$im->setCompressionQuality( 40 );
$im->writeImages($pdf, false);
then it will create 3 pdf files that look right, but then if I change the second parameter of writeImages to true, it creates that problem. I'm wondering if this is a bug in the interface...
mkoppanen
Posts: 309
Joined: 2007-06-09T07:06:32-07:00

Re: Convert Array of Tiff Images to PDF

Post by mkoppanen »

xhenxhe wrote:Thanks. I've been playing with it. It seems if I do something like this

Code: Select all

$im = new Imagick($pages);
$im->setFormat('PDF');
$im->setCompression( Imagick::COMPRESSION_JPEG );
$im->setCompressionQuality( 40 );
$im->writeImages($pdf, false);
then it will create 3 pdf files that look right, but then if I change the second parameter of writeImages to true, it creates that problem. I'm wondering if this is a bug in the interface...
I tested the code here and I seem to get the correct results (??).

Can you post the original images so i can test?
Mikko Koppanen
My blog: http://valokuva.org
xhenxhe

Re: Convert Array of Tiff Images to PDF

Post by xhenxhe »

Attached is a php script, two tif images and the result that I get on my machine. Thanks!
Attachments
imagick-tiff-pdf.zip
(106.07 KiB) Downloaded 1628 times
Post Reply