#!/usr/bin/perl -w use Env; use Config; use Term::ReadLine; =head1 NAME prvps - preview postscript document and eventually print it =head1 SYNOPSIS system call: prvps [] =head1 DESCRIPTION B previews PostScript documents and eventually prints them, letting you choose the pages and the output format. Without a F, standard input is used. If a file with F does not exist, F is tried or, if that also does not exist, F. B displays your Postscript document using B, and then interrogates you about the pages you want to print. To that end the following prompt appears: Pages ([range[,range...]|a] [b|t] [x])? You can react to this as in the following examples: 5 # to print page 5 5- # to print pages 5 through the end 5-7 # to print pages 5, 6 and 7 -7 # to print the first 7 pages 5-7,19- # to print pages 5, 6, 7 and 19 through the end a # to print the whole document a x3 # to print 3 copies of the document x3 # the same 5 x3 # to print 3 copies of page 5 t # print the whole document twosided t 2- # print twosided starting at page 2 b # to print the whole document as an a5 size booklet b -12 # to print the first 12 pages as an a5 size booklet If you use b to print a booklet, or t to print twosided, then you will have to arrange your printer such that, with the printed sides up, the first page printed will be at the bottom of the stack, and the last page printed will be on top. Normally you will then have your output come out the back of your printer. B will first print one side of the paper and prompt you for a return when the printer is ready and you have turned the stack and put it back in the paper feed. Finally, if you used the b option to print a booklet, you will have to reshuffle your pages manually as follows: - take the bottom page from the stack and put it aside without changing its position - take the top page from the stack and put it upside down on the bottom page - repeat these two actions until the stack is empty - if you find out how to prevent this manual action, please tell me how you did it!! (wybo@servalys.hobby.nl) =cut $term=new Term::ReadLine 'test'; $TMP='/tmp' unless $TMP; $in = "$TMP/$$.in"; # temp file for saving standard input $sel = "$TMP/$$.sel"; # temp file for selected pages $front = "$TMP/$$.front"; # temp file for front pages $back = "$TMP/$$.back"; # temp file for back pages $print = $Config{'lpr'} or $print='lpr'; $booklet=$selection=$lpropt=$doit=""; $prompt='Pages ([range[,range...]|a] [b|t] [x])? '; $print_prompt="printer ready? then turn pack and type return "; $file=shift or $file=""; # empty -> stdin if ($file) { # if a filename was given if (! -f $file) { # and it does not exist if (-f "$file.ps") { $file.=".ps" } elsif (-f "$file.eps") { $file.='.eps' } else { die "no file $file.eps or $file.ps found\n" } } } else { $file=$in; open(OUT,">$in") or die("Could not open $file"); while(<>) { print OUT }; close OUT; } # look in the first non-empty line to check if it is right: open(IN,$file); do { chomp($_=) } until $_; close IN; /^%!PS/ or die("Input file is not a PostScript file\n"); system "gv -scale 2 -spartan $file"; $front_spec='"4:-3L@.707(210mm,0)+0L@.707(210mm,148.5mm)"'; $back_spec='"4:1L@.707(210mm,0)+-2L@.707(210mm,148.5mm)"'; while (1) { if ($selection) { # if anything was set print STDERR "\n"; $selection =~ /[1-9]/ or $selection.=" -e -o "; system("psselect -q $selection $file $sel"); if ($booklet) { system("pstops -q $front_spec $sel >$front"); system("$print $lpropt $front"); $term->readline($print_prompt); system("pstops -q $back_spec $sel >$back"); system("$print $lpropt $back"); } elsif ($twosided) { system("pstops 2:0 $sel >$front"); system("$print $lpropt $front"); $term->readline($print_prompt); system("pstops 2:1 $sel >$back"); system("$print $lpropt $back"); } else { system("$print $lpropt $sel"); } } $booklet=$twosided=$selection=$lpropt=$doit=""; $com=$term->readline($prompt); for (split(/\s+/,$com)) { CASE: { /^q$/ # die on n of q and goto READY; # don't just exit: may be called with do /^x[0-9]+$/ # x3 -> lpr -#3 and do { s/x//; print STDERR " $_ copies"; $lpropt = '-#'.$_; $selection.=" -q"; }, last CASE; /^b$/ # print a5 booklet and do { $selection.=" -q "; print STDERR " booklet"; $booklet=1; }, last CASE; /^t$/ # print twosided and do { $selection.=" -q"; $twosided=1; print STDERR " twosided"; }, last CASE; /^a$/ # print all and do { $selection="-e -o"; print STDERR " all pages"; }, last CASE; /^(([0-9]+-?[0-9]*|[0-9]*-?[0-9]+),?)+$/ # n-m or n- or -m and do { ($selection=$_) =~ s/,$//; print STDERR " pages: $selection"; }, last CASE; warn "Illegal specification(s)\n". "Examples of page specifications:\n". "5 to print page 5\n". "5- to print pages 5 through the end\n". "5-7 to print pages 5, 6 and 7\n". "-7 to print the first 7 pages\n". "5-7,19- to print pages 5, 6, 7 and 19 through the end\n". "a to print the whole document\n". "a x3 to print 3 copies of the document\n". "x3 the same\n". "5 x3 to print 3 copies of page 5\n". "t print the whole document twosided\n". "t 2- print twosided starting at page 2\n". "b to print the whole document as an a5 size booklet\n". "b -12 to print the first 12 pages as an a5 size booklet\n"; } } } READY: for (glob "/tmp/$$*") { unlink $_ }