source: other-projects/trunk/realistic-books/src/pdf2realbook.pl@ 19253

Last change on this file since 19253 was 19253, checked in by davidb, 15 years ago

Establishing a source code repository for Veronica's Realistic Book's software

File size: 4.6 KB
Line 
1#!/usr/bin/perl -w
2#
3# A component of the Realistic Book software
4# from the New Zealand Digital Library Project at the
5# University of Waikato, New Zealand.
6#
7# Copyright (C) 2007 New Zealand Digital Library Project
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22#
23
24#*****************************************************************
25#Realistic Book application
26# Converting each PDF page into a SWF file. Also produce
27# the XHTML input file for the Realistic book program
28#
29#by Veronica Liesaputra --- 1 August 2007
30#*****************************************************************/
31
32use warnings;
33use strict;
34
35use File::Basename;
36use File::Spec::Functions;
37use File::Copy;
38
39BEGIN {
40 die "REALISTIC_BOOKS_HOME not set. Have you sourced setup.bash?\n"
41 unless defined $ENV{'REALISTIC_BOOKS_HOME'};
42 die "RBOS not set. Have you sourced setup.bash?\n"
43 unless defined $ENV{'RBOS'};
44}
45
46
47if ($#ARGV < 0)
48{
49 my $full_progname = $0;
50 my $progname = basename($full_progname);
51
52 print STDERR "\nUsage: $progname pdf_filename\n";
53 print STDERR "or perl -S $progname pdf_filename\n\n";
54}
55else
56{
57 my $progname = `pdf2swf --version`;
58 if (defined $progname && $progname =~ m/swftools/i) {
59 } else {
60 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");
61 }
62 my $filename = $ARGV[0];
63 if (-d $filename) {
64 die("$filename is a directory! Please specify the file extension. \n");
65 }
66 if (-f $filename) {
67 } else {
68 die("Cannot find $filename! \n");
69 }
70
71## print STDERR "Ignore the message called RegOpenKeyEx failed\n";
72
73 my($file_root, $fulldir, $file_ext) = fileparse($filename,qr/\.[^.]*/);
74
75 my $folder_name = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books",$file_root);
76
77 #--
78 # Copy modelcol content to new book folder
79 # --
80
81 mkdir($folder_name);
82 my $modelcol_dir = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books","modelcol");
83 opendir(DIR, $modelcol_dir)
84 || die "Cannot open directory $modelcol_dir: $!";
85
86 my @model_files = grep { -f catfile($modelcol_dir,$_) } readdir(DIR);
87 closedir DIR;
88
89 foreach my $f ( @model_files) {
90 my $src_filename = catfile($modelcol_dir,$f);
91 my $dst_filename = catfile($folder_name,$f);
92 copy($src_filename,$dst_filename);
93 }
94
95 my $html_input = "<HTML>\n<title>$folder_name</title>\n<Description>\n";
96 $html_input = "$html_input<!-- Please do not remove Filename and PageFolder metadata -->\n";
97 $html_input = "$html_input<Metadata name=\"Filename\">$folder_name</Metadata>\n";
98 $html_input = "$html_input<Metadata name=\"PageFolder\">$folder_name</Metadata>\n";
99 $html_input = "$html_input<Metadata name=\"StartNumbering\">1</Metadata>\n";
100 $html_input = "$html_input</Description>\n";
101
102 my $i = 1;
103 my $page_num = $i;
104
105 while ($i <= $page_num)
106 {
107 my $backFile = "Page_$i.swf";
108 my $frontFile = "$folder_name";
109 my $output_file = catfile($frontFile,$backFile);
110 my $imgfile = `pdf2swf -p $i -s insertstop -s zoom=100 $filename -o $output_file`;
111
112 if ($i == 1)
113 {
114 if ($imgfile =~ m/Pages:/i)
115 {
116 my $tmp = $';
117 if ($tmp =~ m/(\d+)/)
118 {
119 $page_num = $1;
120 print STDOUT "Total page: $page_num\n";
121 }
122 }
123 }
124
125 print STDOUT "creating page $i\n";
126
127 #$html_input = "$html_input<newpage totalframes=\"1\" /><!-- filenum=\"$i\" -->\n";
128 $html_input = "$html_input<newpage filename=\"Page_$i\" />\n";
129 $i = $i + 1;
130 }
131
132 $html_input = "$html_input</HTML>\n";
133
134 my $html_file = catfile($folder_name,"pages.htm");
135 open (PROD, ">$html_file") || die("Error Writing to File: $html_file $!");
136 print PROD $html_input;
137 close (PROD) || die("Error Closing File: $html_file $!");
138 print STDERR "creating $html_file\n";
139
140 print STDOUT "Realistic book generated in $folder_name\n";
141
142}
Note: See TracBrowser for help on using the repository browser.