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

Last change on this file since 20889 was 20889, checked in by anna, 12 years ago

Add new version of Realistic Books that include highlighting and searching

  • Property svn:executable set to *
File size: 6.5 KB
Line 
1#
2# A component of the Realistic Book software
3# from the New Zealand Digital Library Project at the
4# University of Waikato, New Zealand.
5#
6# Copyright (C) 2007 New Zealand Digital Library Project
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
20# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21#
22
23#*****************************************************************
24#Realistic Book application
25# Converting each PDF page into a SWF file. Also produce
26# the XHTML input file for the Realistic book program
27#
28#by Veronica Liesaputra --- 1 August 2007
29#*****************************************************************/
30use warnings; use strict;
31
32 my $err = 0;
33 if ($#ARGV < 0) {
34 $err = 1;
35 }
36
37 my $newIndex = 0;
38 my $newStructure = 0;
39 my $filename = "";
40 my $j = 0;
41 while (($j <= $#ARGV) && ($err == 0)) {
42 if ($ARGV[$j] =~ m/^\-noindex$/i) {
43 $newIndex = 1;
44 } else {
45 if ($ARGV[$j] =~ m/^\-nostructure$/i) {
46 $newStructure = 1;
47 } else {
48 if ($ARGV[$j] =~ m/^\-swfupdate$/i) {
49 $newIndex = 1;
50 $newStructure = 1;
51 } else {
52 if ($ARGV[$j] =~ m/^\-/) {
53 print STDERR "Unknown option: $ARGV[$j]\n\n";
54 $err = 1;
55 } else {
56 $filename = $ARGV[$j];
57 }
58 }
59 }
60 }
61
62 $j = $j + 1;
63 }
64
65 # exit from the application and show the usage message
66 if ($err == 1) {
67 print STDERR "USAGE: perl pdf2image.pl [-options] pdf_filename\n\n";
68 print STDERR "Options:\n";
69 print STDERR "-swfupdate \t Only convert each page of the pdf file.\n";
70 print STDERR " \t Do not generate new index.html and book structure files.\n";
71 print STDERR "-noindex \t Do not generate new index.html file.\n";
72 print STDERR "-nostructure \t Do not generate new book structure file.\n";
73 die("\n");
74 }
75
76 # check that file exists
77 if (-d $filename) {
78 die("$filename is a directory! Please specify the file extension. \n");
79 }
80 if (-f $filename) {
81 } else {
82 die("Cannot find $filename! \n");
83 }
84
85 # check that pdf2swf exists
86 my $progname = `pdf2swf --version`;
87 if (defined $progname && $progname =~ m/swftools/i) {
88 } else {
89 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");
90 }
91
92 # create folder for swf files
93 my $folder_name = $filename;
94 $folder_name=~ s/.pdf//i;
95 mkdir($folder_name);
96
97 # convert each page of pdf into swf and get its page size
98 my $width = 0;
99 my $height = 0;
100 my $page_input = "";
101 my @output_lines = split(/\n/,`pdf2swf -I $filename`);# get info of each pages in pdf
102 foreach my $line (@output_lines) {
103 if ($line =~ m/^page=(\d+)\s+width=(\d+\.?\d*)\s+height=(\d+\.?\d*)$/i) {
104 # get the smallest width and height
105 if (($width == 0) || ($2 < $width)) {
106 $width = $2;
107 }
108 if (($height == 0) || ($3 < $height)) {
109 $height = $3;
110 }
111
112 # convert to swf
113 my $i = $1;
114 my $backFile = "Page_$i.swf";
115 my $frontFile = "$folder_name/";
116 my $output_file = "$frontFile$backFile";
117 my $imgfile = `pdf2swf -p $i -s insertstop -s zoom=100 -s bboxvars $filename -o $output_file`;
118
119 print STDOUT "creating page $i\n";
120
121 $page_input = "$page_input<newpage filename=\"Page_$i\" />\n";
122 }
123 }
124
125 if ($newStructure == 0) {
126 # get page size
127 my $page_size = "x$height";
128 $page_size = "$width$page_size";
129
130
131 # create the book structure file
132 my $html_input = "<HTML>\n<title>$folder_name</title>\n<Description>\n";
133 $html_input = "$html_input<!-- Please do not remove Filename and PageFolder metadata -->\n";
134 $html_input = "$html_input<Metadata name=\"Filename\">$folder_name</Metadata>\n";
135 $html_input = "$html_input<Metadata name=\"PageFolder\">$folder_name</Metadata>\n";
136 $html_input = "$html_input<Metadata name=\"StartNumbering\">1</Metadata>\n";
137 $html_input = "$html_input<Metadata name=\"PageSize\">$page_size</Metadata>\n";
138 $html_input = "$html_input</Description>\n";
139 $html_input = "$html_input$page_input";
140 $html_input = "$html_input</HTML>\n";
141
142 my $html_file = "$folder_name.htm";
143 open (PROD, ">$html_file") || die("Error Writing to File: $html_file $!");
144 print PROD $html_input;
145 close (PROD) || die("Error Closing File: $html_file $!");
146 print STDOUT "creating $html_file\n";
147 }
148
149 if ($newIndex == 0) {
150 # creating index.html
151 my $index_input = "<html>\n<head>\n";
152 $index_input = "$index_input<title>$folder_name</title>\n";
153 $index_input = "$index_input<script src='RealisticBook.js' language='javascript'></script>\n</head>\n";
154 $index_input = "$index_input<body>\n";
155 $index_input = "$index_input<!-- You have to have this div tag with id = bookdiv in your HTML file!! This is where the program will be embedded -->\n";
156 $index_input = "$index_input<div id='bookdiv'></div>\n";
157 $index_input = "$index_input<script type='text/javascript'>\n";
158 $index_input = "$index_input<!-- \n";
159 $index_input = "$index_input embedBookProgram(new Array('$folder_name.htm'));\n";
160 $index_input = "$index_input// -->\n";
161 $index_input = "$index_input</script>\n";
162 $index_input = "$index_input<noscript>\n";
163 $index_input = "$index_input This content requires JavaScript and Adobe Flash Player.\n";
164 $index_input = "$index_input <a href='http://www.macromedia.com/go/getflash/'>Get Flash</a>\n";
165 $index_input = "$index_input</noscript>\n</body>\n</html>\n";
166
167 my $index_file = "index.html";
168 open (PROD, ">$index_file") || die("Error Writing to File: $index_file $!");
169 print PROD $index_input;
170 close (PROD) || die("Error Closing File: $index_file $!");
171 print STDOUT "creating $index_file\n";
172 }
173
174 print STDOUT "Done!\n";
175
Note: See TracBrowser for help on using the repository browser.