Page 1 of 1

Convert Array of Tiff Images to PDF

Posted: 2007-10-02T15:38:34-07:00
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;
	}

Re: Convert Array of Tiff Images to PDF

Posted: 2007-10-03T01:24:16-07:00
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 );

?>



Re: Convert Array of Tiff Images to PDF

Posted: 2007-10-03T07:09:35-07:00
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.

Re: Convert Array of Tiff Images to PDF

Posted: 2007-10-03T07:30:30-07:00
by mkoppanen
I will check that later today! I'll post back results here.

Re: Convert Array of Tiff Images to PDF

Posted: 2007-10-03T07:35:13-07:00
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...

Re: Convert Array of Tiff Images to PDF

Posted: 2007-10-03T07:46:09-07:00
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?

Re: Convert Array of Tiff Images to PDF

Posted: 2007-10-03T08:04:26-07:00
by xhenxhe
Attached is a php script, two tif images and the result that I get on my machine. Thanks!