#!/usr/bin/perl
#
# This is a perl script to convert a MIA PDF presentation 
# (in landscape orientation) into a PDF document with 
# 1,2 or 4 slides on each page (suitable for printing). 
#
# Usage: slide2print [-4|-2|-1] [-e num] [-p] filename.pdf 
#        optional parameters: 
#          -1, -2, -4 ...number of pages to print on each sheet
#                        (default is 4)
#          -e num ... denotes the last page to be printed 
#          -p     ... keep also the postscript with n slides / page
#
# Output: a new file is created with the name
#                  file.pdf -> file_(n)print.pdf
#         where (n) stands for 1, 2 or 4.
#
# By Pavel Mrazek, mrazek@mia.uni-saarland.de, May 2003.

# Read options -e,-p,-1,-2,-4 (if present):
use Getopt::Std;
&getopts("pe:124");

# read input file:
$file_in = $ARGV[0];

if (-r "$file_in") {

    # how many pages / sheet to print (1,2 or 4; 4 is the default):
    if ($opt_1)    { $scaling = 1;  }
    elsif ($opt_2) { $scaling = 2;  }
    else           { $scaling = 4;  }

    $file_out = $file_in;
    $file_out =~ s/.pdf$/\_$scaling\print.pdf/;
    $file_tmp = $file_in;
    $file_tmp =~ s/.pdf$/\_tmp.ps/;
    $file_outps = $file_in;
    $file_outps =~ s/.pdf$/\_$scaling\print.ps/;
    if ($opt_e) { # last page specified
	$endparam = "-end $opt_e";
    }
    else {
	$endparam = "";	
    }
    system("cat $file_in | acroread -toPostScript -level2 -size a4 $endparam -landscape -shrink > $file_tmp");
    system("psnup -pa4 -Pa4 -b2mm -m3mm -l -$scaling $file_tmp > $file_outps");
    system("ps2pdf $file_outps $file_out");

    system("rm $file_tmp");
    if (not $opt_p) { # do not keep postscript:
	system("rm $file_outps");
    }

}
elsif ($file_in) {
    print("Error: could not read input file $file_in\n");
}
else { # no parameters given
    print("\nslide2print ...
       is a perl script to convert a MIA PDF presentation 
       (in landscape orientation) into a PDF document with 
       1,2 or 4 slides on each page (suitable for printing). 

Usage: slide2print [-4|-2|-1] [-e num] [-p] filename.pdf 
       optional parameters: 
         -1, -2, -4 ...number of pages to print on each sheet
                       (default is 4)
         -e num ... denotes the last page to be printed 
         -p     ... keep also the postscript with n slides / page

Output: a new file is created with the name
                 file.pdf -> file_(n)print.pdf
        where (n) stands for 1, 2 or 4.

By Pavel Mrazek, mrazek\@mia.uni-saarland.de, May 2003.
\n");
}



