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

Last change on this file since 20889 was 19734, checked in by davidb, 13 years ago

General improvements

File size: 6.4 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
47sub print_usage
48{
49 my ($full_progname) = @_;
50
51 my $progname = basename($full_progname);
52
53 print STDERR "\nUsage: $progname pdf_filename\n";
54 print STDERR "or perl -S $progname pdf_filename\n\n";
55
56 exit(-1);
57}
58
59sub check_pdf2swf
60{
61 my $output = `pdf2swf --version`;
62
63 if (!defined $output || ($output !~ m/swftools/i)) {
64 print STDERR "Unable to find pdf2swf: $!\n";
65 print STDERR "Have you sourced setup.bash (Linux/MacOS)?\n";
66 print STDERR "Have you run setup.bat (Windows)?\n";
67 exit(-1);
68 }
69}
70
71sub check_file
72{
73 my ($filename) = @_;
74
75 if (-d $filename) {
76 print STDERR "Error: $filename is a directory.\n";
77 print STDERR " Please specify the PDF file as input.\n";
78 exit(-1);
79 }
80
81 if (!-e $filename) {
82 print STDERR "Error: Unable to find $filename\n";
83 exit(-1);
84 }
85}
86
87sub copy_model_col
88{
89 my ($folder_name) = @_;
90
91 #--
92 # Copy modelcol content to new book folder
93 # --
94
95 mkdir($folder_name);
96 my $modelcol_dir = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books","modelcol");
97 opendir(DIR, $modelcol_dir)
98 || die "Cannot open directory $modelcol_dir: $!";
99
100 my @model_files = grep { -f catfile($modelcol_dir,$_) } readdir(DIR);
101 closedir DIR;
102
103 foreach my $f ( @model_files) {
104 my $src_filename = catfile($modelcol_dir,$f);
105 my $dst_filename = catfile($folder_name,$f);
106 copy($src_filename,$dst_filename);
107 }
108}
109
110sub pdf2swf_info
111{
112 my ($filename) = @_;
113
114 my $cmd = "pdf2swf --info \"$filename\"";
115 if ($ENV{'RBOS'} =~ /^windows$/i) {
116 $cmd .= " 2>nul";
117 }
118 else {
119 $cmd .= " 2>/dev/null";
120 }
121
122
123 my @output_lines = split(/\n/,`$cmd`);
124
125 my $curr_page = 0;
126 my $total_width = 0;
127 my $total_height = 0;
128
129 my $double_check = 0;
130 foreach my $line (@output_lines) {
131 next if ($line =~ m/^$/);
132
133 if ($line =~ m/^page=(\d+)\s+width=(\d+\.?\d*)\s+height=(\d+\.?\d*)$/) {
134 $curr_page = $1;
135 my $width = $2;
136 my $height = $3;
137 $total_width += $width;
138 $total_height += $height;
139 }
140 $double_check++;
141 }
142
143 #my $last_line = pop(@output_lines);
144
145 ## Watch out for trailing \n
146 #$last_line = pop(@output_lines) if ($last_line =~ m/^$/);
147
148 #my ($num_pages) = ($last_line =~ m/^page=(\d+)/);
149 my $num_pages = $curr_page;
150
151 if ($double_check != $num_pages) {
152 print STDERR "Warning: inconsistency found in page count: $double_check vs $num_pages\n";
153 print STDERR "Setting to $double_check\n";
154 $num_pages = $double_check;
155 }
156
157 my $avg_width = $total_width / $num_pages;
158 my $avg_height = $total_height / $num_pages;
159
160 return ($num_pages,$avg_width,$avg_height);
161
162}
163
164
165sub generate_html
166{
167 my ($full_folder_name,$folder_root,$filename) = @_;
168
169 my ($num_pages,$width,$height) = pdf2swf_info($filename);
170
171 print STDOUT "Total page: $num_pages\n";
172
173 my $html_input = "<HTML>\n<title>$folder_root</title>\n<Description>\n";
174 $html_input .= "<!-- Please do not remove Filename and PageFolder metadata -->\n";
175 $html_input .= " <Metadata name=\"Filename\">$folder_root</Metadata>\n";
176 $html_input .= " <Metadata name=\"PageFolder\">.</Metadata>\n";
177 $html_input .= " <Metadata name=\"StartNumbering\">1</Metadata>\n";
178 $html_input .= " <Metadata name=\"pagedim\">${width}x${height}</Metadata>\n";
179 $html_input .= "</Description>\n";
180
181 for (my $i=1; $i<=$num_pages; $i++) {
182
183 my $backFile = "Page_$i.swf";
184 my $frontFile = "$full_folder_name";
185 my $output_file = catfile($frontFile,$backFile);
186 my $cmd = "pdf2swf -p $i -s insertstop -s zoom=100 \"$filename\" -o \"$output_file\"";
187 ## $cmd .= " 2>1";
188
189 my $imgfile = `$cmd`;
190
191 print STDOUT "creating page $i\n";
192
193 $html_input = "$html_input<newpage filename=\"Page_$i\" />\n";
194 }
195
196 $html_input = "$html_input</HTML>\n";
197
198 return $html_input;
199}
200
201
202sub main
203{
204 my ($argc,@argv) = @_;
205
206 my $full_progname = $0;
207
208 if ($argc < 0) {
209 print_usage($full_progname);
210 }
211
212 check_pdf2swf();
213
214 my $filename = $argv[0];
215 check_file($filename);
216
217 # print STDERR "Ignore the message called RegOpenKeyEx failed\n";
218
219 my($file_root, $fulldir, $file_ext) = fileparse($filename,qr/\.[^.]*/);
220
221 my $full_folder_name = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books",$file_root);
222
223 copy_model_col($full_folder_name);
224
225 my $html_input = generate_html($full_folder_name,$file_root,$filename);
226
227 # Write out HTML file
228 my $html_file = catfile($full_folder_name,"pages.htm");
229 open (PROD, ">$html_file") || die("Error Writing to File: $html_file $!");
230 print STDERR "Creating $html_file\n";
231
232 print PROD $html_input;
233 close (PROD) || die("Error Closing File: $html_file $!");
234
235
236 print STDOUT "Realistic book generated in $full_folder_name\n";
237
238}
239
240
241&main(scalar(@ARGV),@ARGV);
Note: See TracBrowser for help on using the repository browser.