1 | #!/usr/bin/perl -w
|
---|
2 |
|
---|
3 |
|
---|
4 | ###########################################################################
|
---|
5 | #
|
---|
6 | # pdfpstoimg.pl -- convert PDF or PS documents to various types of Image format
|
---|
7 | #
|
---|
8 | # A component of the Greenstone digital library software
|
---|
9 | # from the New Zealand Digital Library Project at the
|
---|
10 | # University of Waikato, New Zealand.
|
---|
11 | #
|
---|
12 | # Copyright (C) 2001 New Zealand Digital Library Project
|
---|
13 | #
|
---|
14 | # This program is free software; you can redistribute it and/or modify
|
---|
15 | # it under the terms of the GNU General Public License as published by
|
---|
16 | # the Free Software Foundation; either version 2 of the License, or
|
---|
17 | # (at your option) any later version.
|
---|
18 | #
|
---|
19 | # This program is distributed in the hope that it will be useful,
|
---|
20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | # GNU General Public License for more details.
|
---|
23 | #
|
---|
24 | # You should have received a copy of the GNU General Public License
|
---|
25 | # along with this program; if not, write to the Free Software
|
---|
26 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
27 | #
|
---|
28 | ###########################################################################
|
---|
29 | # pdfpstoimg.pl is a wrapper for running the ImageMagick 'convert' utility
|
---|
30 | # which converts PDF and PS documents to various types of image (e.g. PNG,
|
---|
31 | # GIF, JPEG format). We then create an item file to join the images together
|
---|
32 | # into a document. The item file will be processed by PagedImagePlugin
|
---|
33 |
|
---|
34 | BEGIN {
|
---|
35 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
36 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
37 | }
|
---|
38 |
|
---|
39 | use parsargv;
|
---|
40 | use util;
|
---|
41 | use FileUtils;
|
---|
42 | use Cwd;
|
---|
43 | use File::Basename;
|
---|
44 |
|
---|
45 | sub print_usage {
|
---|
46 | print STDERR
|
---|
47 | ("pdfpstoimg.pl wrapper for converting PDF or PS files to a series of images.\n",
|
---|
48 | "Usage: pdfpstoimg.pl [options] <PDF/PS-file> <output-filestem>>\n",
|
---|
49 | "Options:\n",
|
---|
50 | "\t-convert_to\toutput image type (gif, jpg, png) \n"
|
---|
51 | );
|
---|
52 | exit (1);
|
---|
53 | }
|
---|
54 |
|
---|
55 | sub main {
|
---|
56 | my (@ARGV) = @_;
|
---|
57 | my ($convert_to);
|
---|
58 |
|
---|
59 | # read command-line arguments so that
|
---|
60 | # you can change the command in this script
|
---|
61 | if (!parsargv::parse(\@ARGV,
|
---|
62 | 'convert_to/.*/^', \$convert_to,
|
---|
63 | )) {
|
---|
64 | print_usage();
|
---|
65 | }
|
---|
66 |
|
---|
67 | # Make sure the user has specified both input and output files
|
---|
68 | if (scalar(@ARGV) != 2) {
|
---|
69 | print_usage();
|
---|
70 | }
|
---|
71 |
|
---|
72 | my $input_filename = $ARGV[0];
|
---|
73 | my $output_filestem = $ARGV[1];
|
---|
74 |
|
---|
75 | # test that the directories exist to create the output file, or
|
---|
76 | # we should exit immediately.
|
---|
77 | &FileUtils::makeDirectory($output_filestem) if (!-e $output_filestem);
|
---|
78 |
|
---|
79 | my @dir = split (/(\/|\\)/, $input_filename);
|
---|
80 | my $input_basename = pop(@dir);
|
---|
81 | $input_basename =~ s/\.(pdf|ps)$//i;
|
---|
82 | my $dir = join ("", @dir);
|
---|
83 |
|
---|
84 | if (!-r $input_filename) {
|
---|
85 | print STDERR "Error: unable to open $input_filename for reading\n";
|
---|
86 | exit(1);
|
---|
87 | }
|
---|
88 | # don't include path on windows (to avoid having to play about
|
---|
89 | # with quoting when GSDLHOME might contain spaces) but assume
|
---|
90 | # that the PATH is set up correctly.
|
---|
91 | $cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl convert";
|
---|
92 |
|
---|
93 | my $output_filename = &FileUtils::filenameConcatenate($output_filestem, $input_basename);
|
---|
94 | if ($convert_to eq "gif") {
|
---|
95 | $cmd .= " \"$input_filename\" \"$output_filename-%02d.$convert_to\"";
|
---|
96 | } else {
|
---|
97 | $cmd .= " \"$input_filename\" \"$output_filename.$convert_to\"";
|
---|
98 | }
|
---|
99 |
|
---|
100 | # system() returns -1 if it can't run, otherwise it's $cmds ret val.
|
---|
101 | # note we return 0 if the file is "encrypted"
|
---|
102 | $!=0;
|
---|
103 | my $status = system($cmd);
|
---|
104 | if ($status != 0) {
|
---|
105 | print STDERR "Convert error for $input_filename $!\n";
|
---|
106 | # leave these for gsConvert.pl...
|
---|
107 | #&FileUtils::removeFiles("$output_filestem.text") if (-e "$output_filestem.text");
|
---|
108 | #&FileUtils::removeFiles("$output_filestem.err") if (-e "$output_filestem.err");
|
---|
109 | return 1;
|
---|
110 | } else {
|
---|
111 | # command execute successfully
|
---|
112 | &util::create_itemfile($output_filestem, $input_basename, $convert_to);
|
---|
113 | }
|
---|
114 | return 0;
|
---|
115 | }
|
---|
116 |
|
---|
117 | # indicate our error status, 0 = success
|
---|
118 | exit (&main(@ARGV));
|
---|
119 |
|
---|
120 |
|
---|
121 |
|
---|