[10358] | 1 | #!/usr/bin/perl -w
|
---|
| 2 |
|
---|
| 3 |
|
---|
| 4 | ###########################################################################
|
---|
| 5 | #
|
---|
[17328] | 6 | # pdfpstoimg.pl -- convert PDF or PS documents to various types of Image format
|
---|
[10358] | 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 | ###########################################################################
|
---|
[17328] | 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
|
---|
[10358] | 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;
|
---|
[27758] | 41 | use FileUtils;
|
---|
[10358] | 42 | use Cwd;
|
---|
[17328] | 43 | use File::Basename;
|
---|
| 44 |
|
---|
[10358] | 45 | sub print_usage {
|
---|
| 46 | print STDERR
|
---|
[17328] | 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",
|
---|
[10358] | 49 | "Options:\n",
|
---|
[17328] | 50 | "\t-convert_to\toutput image type (gif, jpg, png) \n"
|
---|
[10358] | 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,
|
---|
[10402] | 63 | )) {
|
---|
| 64 | print_usage();
|
---|
[10358] | 65 | }
|
---|
| 66 |
|
---|
[17328] | 67 | # Make sure the user has specified both input and output files
|
---|
[10358] | 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
|
---|
[17328] | 76 | # we should exit immediately.
|
---|
[27758] | 77 | &FileUtils::makeDirectory($output_filestem) if (!-e $output_filestem);
|
---|
[10358] | 78 |
|
---|
| 79 | my @dir = split (/(\/|\\)/, $input_filename);
|
---|
| 80 | my $input_basename = pop(@dir);
|
---|
[17328] | 81 | $input_basename =~ s/\.(pdf|ps)$//i;
|
---|
[10358] | 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.
|
---|
[24600] | 91 | $cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl convert";
|
---|
[17328] | 92 |
|
---|
[27758] | 93 | my $output_filename = &FileUtils::filenameConcatenate($output_filestem, $input_basename);
|
---|
[10402] | 94 | if ($convert_to eq "gif") {
|
---|
[10526] | 95 | $cmd .= " \"$input_filename\" \"$output_filename-%02d.$convert_to\"";
|
---|
[10402] | 96 | } else {
|
---|
[10526] | 97 | $cmd .= " \"$input_filename\" \"$output_filename.$convert_to\"";
|
---|
[10402] | 98 | }
|
---|
| 99 |
|
---|
[10358] | 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;
|
---|
[24600] | 103 | my $status = system($cmd);
|
---|
| 104 | if ($status != 0) {
|
---|
[10358] | 105 | print STDERR "Convert error for $input_filename $!\n";
|
---|
| 106 | # leave these for gsConvert.pl...
|
---|
[27758] | 107 | #&FileUtils::removeFiles("$output_filestem.text") if (-e "$output_filestem.text");
|
---|
| 108 | #&FileUtils::removeFiles("$output_filestem.err") if (-e "$output_filestem.err");
|
---|
[10358] | 109 | return 1;
|
---|
| 110 | } else {
|
---|
| 111 | # command execute successfully
|
---|
[25994] | 112 | &util::create_itemfile($output_filestem, $input_basename, $convert_to);
|
---|
[10358] | 113 | }
|
---|
| 114 | return 0;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | # indicate our error status, 0 = success
|
---|
| 118 | exit (&main(@ARGV));
|
---|
| 119 |
|
---|
[10402] | 120 |
|
---|
| 121 |
|
---|