#!/public/bin/perl -w # Converts a postscript file into PDF format # Basically it is a wrapper script calling nup and ps2pdf with the options combined # Read the README file for parameter rules and requirements. # Note: This script does have code to check input variables for shell security # because it assumes the user already have the right to run the script. # # Programmed by Eric Tong. (tonge@uvic.ca) # Copyleft 1999(c). (http://www.gnu.org/copyleft/copyleft.html) use strict; use Getopt::Long; umask(0000); my $version = "1.00"; my $help_text = "Usage:\n". " ps2pdf \n\n". " options:\n". " <-i filename> - The input postscript filename\n". " <-o filename> - The output PDF filename (optional)\n". " <-p number> - The number of pages shrinked to\n". " one page (must be 2,4,8,16) (optional)\n". " <-s number> - The starting offset page (optional)\n". " <-override yes> - It must be set to \"yes\" if you\n". " want the script to overwrite files\n". " <-perm perm> - Sets the output file permission when\n". " completed (must be in octal format)\n"; MAIN: { my ($input_filename, $output_filename, $override_option, $nup_pages, $help_option, $start_page, $pdf_permission); &GetOptions( "i=s" => \$input_filename, "o:s" => \$output_filename, "p:i" => \$nup_pages, "h:s" => \$help_option, "s:i" => \$start_page, "override:s" => \$override_option, "perm:s" => \$pdf_permission); $input_filename = $ARGV[0] if (defined($ARGV[0])); # Set default varibles $nup_pages = 1 if (!defined($nup_pages)); $start_page = 1 if (!defined($start_page)); # Check input for validity if (!check_nup_range($nup_pages)) { exit_with_message("Invalid n-up page range!"); } elsif (!defined($input_filename)) { exit_with_message("The input file is not defined!"); } elsif (!-e $input_filename) { exit_with_message("The input file ($input_filename) does not exist!"); } elsif (!defined($output_filename)) { if ($input_filename =~ /^(.*)\.ps/) { $output_filename = $1 . $nup_pages . ".pdf"; } elsif(defined($override_option) and $override_option eq "yes") { $output_filename = $input_filename . ".pdf"; } else { exit_with_message("The intput file does not end with a .ps extension!\n". "(Use -override=yes if you want to use it anyway)"); } } if (-e $output_filename and (!defined($override_option) or $override_option ne "yes")) { exit_with_message("The output file ($output_filename) already exist already!\n". "(Use -override=yes if you want to overwrite)"); } else { my $intermediate_filename = $input_filename; my $erase_temp_file = 0; # N-up the file if necessary if ($nup_pages > 1) { print "Creating a intermediate file...\n"; $intermediate_filename = "$$.temp"; my $command = "nup -$nup_pages " . ($start_page > 1 ? "-s $start_page" : "") . " $input_filename > $intermediate_filename"; exit_with_message("Unable to create the intermediate file: $!") if (system("$command")); $erase_temp_file = 1; } # Convert the file print "Converting to pdf file...\n"; my $command = "ps2pdf $intermediate_filename $output_filename"; exit_with_message("Unable to create the output file: $!") if (system("$command")); # Erase the intermediate file if necessary if ($erase_temp_file and -e $intermediate_filename) { print "Removing intermediate file...\n"; unlink($intermediate_filename) or exit_with_message("Unable to remove $intermediate_filename: $!"); } # Updates the file permissions if (defined($pdf_permission)) { print "Setting PDF file permission...\n"; chmod(oct($pdf_permission), $output_filename) or exit_with_message("Unable to set permission for $output_filename: $!"); } } } # Checks for valid range for nup sub check_nup_range($) { return ( $_[0] == 1 or $_[0] == 2 or $_[0] == 4 or $_[0] == 8 or $_[0] == 16 ); } # Personally I don't like dying, do you? sub exit_with_message($) { print "Postscript to PDF converter $version\n\n". "Error: $_[0]\n\n$help_text"; exit 1; }