source: other-projects/trunk/realistic-books/bin/script/pdf2realbook.pl@ 19631

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

addition of bin directory

  • Property svn:executable set to *
File size: 5.5 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>null";
117 }
118 else {
119 $cmd .= " 2>/dev/null";
120 }
121
122 my @output_lines = split(/\n/,`$cmd`);
123
124 my $last_line = pop(@output_lines);
125
126 # Watch out for trailing \n
127 $last_line = pop(@output_lines) if ($last_line =~ m/^$/);
128
129 my ($num_pages) = ($last_line =~ m/^page=(\d+)/);
130
131 return $num_pages;
132}
133
134
135sub generate_html
136{
137 my ($full_folder_name,$folder_root,$filename) = @_;
138
139 my $num_pages = pdf2swf_info($filename);
140 print STDOUT "Total page: $num_pages\n";
141
142 my $html_input = "<HTML>\n<title>$folder_root</title>\n<Description>\n";
143 $html_input = "$html_input<!-- Please do not remove Filename and PageFolder metadata -->\n";
144 $html_input = "$html_input<Metadata name=\"Filename\">$folder_root</Metadata>\n";
145 $html_input = "$html_input<Metadata name=\"PageFolder\">.</Metadata>\n";
146 $html_input = "$html_input<Metadata name=\"StartNumbering\">1</Metadata>\n";
147 $html_input = "$html_input</Description>\n";
148
149 for (my $i=1; $i<=$num_pages; $i++) {
150
151 my $backFile = "Page_$i.swf";
152 my $frontFile = "$full_folder_name";
153 my $output_file = catfile($frontFile,$backFile);
154 my $imgfile = `pdf2swf -p $i -s insertstop -s zoom=100 $filename -o $output_file`;
155
156 print STDOUT "creating page $i\n";
157
158 $html_input = "$html_input<newpage filename=\"Page_$i\" />\n";
159 }
160
161 $html_input = "$html_input</HTML>\n";
162
163 return $html_input;
164}
165
166
167sub main
168{
169 my ($argc,@argv) = @_;
170
171 my $full_progname = $0;
172
173 if ($argc < 0) {
174 print_usage($full_progname);
175 }
176
177 check_pdf2swf();
178
179 my $filename = $argv[0];
180 check_file($filename);
181
182 # print STDERR "Ignore the message called RegOpenKeyEx failed\n";
183
184 my($file_root, $fulldir, $file_ext) = fileparse($filename,qr/\.[^.]*/);
185
186 my $full_folder_name = catfile($ENV{'REALISTIC_BOOKS_HOME'},"books",$file_root);
187
188 copy_model_col($full_folder_name);
189
190 my $html_input = generate_html($full_folder_name,$file_root,$filename);
191
192 # Write out HTML file
193 my $html_file = catfile($full_folder_name,"pages.htm");
194 open (PROD, ">$html_file") || die("Error Writing to File: $html_file $!");
195 print STDERR "Creating $html_file\n";
196
197 print PROD $html_input;
198 close (PROD) || die("Error Closing File: $html_file $!");
199
200
201 print STDOUT "Realistic book generated in $full_folder_name\n";
202
203}
204
205
206&main(scalar(@ARGV),@ARGV);
Note: See TracBrowser for help on using the repository browser.