#
# A component of the Realistic Book software
# from the New Zealand Digital Library Project at the
# University of Waikato, New Zealand.
#
# Copyright (C) 2007 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#*****************************************************************
#Realistic Book application
# Converting each PDF page into a SWF file. Also produce
# the XHTML input file for the Realistic book program
#
#by Veronica Liesaputra --- 1 August 2007
#*****************************************************************/
use warnings; use strict;
my $err = 0;
if ($#ARGV < 0) {
$err = 1;
}
my $newIndex = 0;
my $newStructure = 0;
my $filename = "";
my $j = 0;
while (($j <= $#ARGV) && ($err == 0)) {
if ($ARGV[$j] =~ m/^\-noindex$/i) {
$newIndex = 1;
} else {
if ($ARGV[$j] =~ m/^\-nostructure$/i) {
$newStructure = 1;
} else {
if ($ARGV[$j] =~ m/^\-swfupdate$/i) {
$newIndex = 1;
$newStructure = 1;
} else {
if ($ARGV[$j] =~ m/^\-/) {
print STDERR "Unknown option: $ARGV[$j]\n\n";
$err = 1;
} else {
$filename = $ARGV[$j];
}
}
}
}
$j = $j + 1;
}
# exit from the application and show the usage message
if ($err == 1) {
print STDERR "USAGE: perl pdf2image.pl [-options] pdf_filename\n\n";
print STDERR "Options:\n";
print STDERR "-swfupdate \t Only convert each page of the pdf file.\n";
print STDERR " \t Do not generate new index.html and book structure files.\n";
print STDERR "-noindex \t Do not generate new index.html file.\n";
print STDERR "-nostructure \t Do not generate new book structure file.\n";
die("\n");
}
# check that file exists
if (-d $filename) {
die("$filename is a directory! Please specify the file extension. \n");
}
if (-f $filename) {
} else {
die("Cannot find $filename! \n");
}
# check that pdf2swf exists
my $progname = `pdf2swf --version`;
if (defined $progname && $progname =~ m/swftools/i) {
} else {
die("\nPlease make sure that pdf2swf is located in the same location as this script.\nIf you are in Linux/Macintosh, please install swftools in SWFToolsPackage.\n");
}
# create folder for swf files
my $folder_name = $filename;
$folder_name=~ s/.pdf//i;
mkdir($folder_name);
# convert each page of pdf into swf and get its page size
my $width = 0;
my $height = 0;
my $page_input = "";
my @output_lines = split(/\n/,`pdf2swf -I $filename`);# get info of each pages in pdf
foreach my $line (@output_lines) {
if ($line =~ m/^page=(\d+)\s+width=(\d+\.?\d*)\s+height=(\d+\.?\d*)$/i) {
# get the smallest width and height
if (($width == 0) || ($2 < $width)) {
$width = $2;
}
if (($height == 0) || ($3 < $height)) {
$height = $3;
}
# convert to swf
my $i = $1;
my $backFile = "Page_$i.swf";
my $frontFile = "$folder_name/";
my $output_file = "$frontFile$backFile";
my $imgfile = `pdf2swf -p $i -s insertstop -s zoom=100 -s bboxvars $filename -o $output_file`;
print STDOUT "creating page $i\n";
$page_input = "$page_input