#!/usr/bin/perl
#
# Name:
#	im-hax-pdf-2-png-1.pl.
#
# Purpose:
#	Demonstrate conversion from PDF to PNG.
#
# V 1.00 01-May-2002
# ------------------
# o Initial version
#
# Test environment:
#	OS: WinNT 4 SP 6a
#	Web server: Apache V 1.3.26
#	IM: V 5.4.7
#
# Input files:
#	jasper.pdf (ImageMagick/jp2/doc/jasper.pdf).
#	Any small(!) PDF file will do.
#
# Output files:
#	im-hax-pdf-2-png-1-0.png ..
#	im-hax-pdf-2-png-1-20.png,
#	if input is jasper.png
#
# Note:
#	o tab = 4 spaces || die.
#	o Text reformatted to max 50 chars wide
#		so source can be converted to PDF
#
# Author:
#	Ron Savage <ron@savage.net.au>
#	http://savage.net.au/ImageMagick.html
#
# Licence:
#	Copyright (c) 1999-2002 Ron Savage
#
#	All Programs of mine are
#	'OSI Certified Open Source Software';
#	you can redistribute them and/or modify them
#	under the terms of the Artistic License,
#	a copy of which is available at:
#	http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Image::Magick;

# -----------------------------------------------

my($work_dir_name)	= '/temp';
my($im_version)		= $Image::Magick::VERSION;

chdir($work_dir_name)
|| die("Can't chdir($work_dir_name): $!");

print "Image::Magick V $im_version. \n";
print "\n";

# /ImageMagick/jp2/doc/jasper.pdf
my($input_file_name)	= 'jasper.pdf';
my($output_file_type)	= 'png';
my($output_file_prefix)	=
	"im-hax-pdf-2-$output_file_type-1";
my($image)				= Image::Magick -> new();
my($result)				=
	$image -> Read($input_file_name);
die $result if $result;

print "Input file: $input_file_name contains: ",
	($#{$image} + 1), " pages (21 expected). \n";
print "Writing each page to a separate ",
	"$output_file_type image: \n";

my($output_file_name);

for my $page (0 .. $#{$image})
{
	$output_file_name	=
"$output_file_prefix-$page.$output_file_type";
	$result				=
	$$image[$page] -> Write($output_file_name);
	die $result if $result;

	print "Wrote: $output_file_name. \n";
}
