Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Home-grown psnup

by ambrus (Abbot)
on May 29, 2005 at 13:37 UTC ( #461513=CUFP: print w/ replies, xml ) Need Help??

This utility takes a dvips-generated postscript file, and creates a new potscript that has two of the pages of that on each new page. It only works for the output of dvips; for a4 paper only (because some magic numbers like 421.37259980 are hard-coded), and it can not put more than two pages on one page. It's ugly, but it works.

I tried to do this as a shell script first, but that attempt failed miserably.

#!/usr/bin/env perl # creates a two-paged postscript file -- works for dvips-generated fil +es only # assumes a4 paper use warnings; use strict; my $duper; sub main { @ARGV or die "usage: psdup foo.ps"; for my $arg (@ARGV) { $arg =~ /(.*)\.ps|(.*)/s or die "internal"; my $prefix = $+; my $ifile = -e $arg ? $arg : -e $prefix . ".ps" ? $prefix . ". +ps" : die "cannot find file: $arg"; open my $I, "<", $ifile or die "error opening file: $ifile"; my $tfile = $prefix . ".duptmp.ps"; my $ofile = $prefix . ".dup.ps"; open my $O, ">", $tfile or die "cannot open temporary file $tfile"; my $s = 2; $! = 0; while (defined(my $l = <$I>)) { $s and do { 2 == $s and $l =~ /^%%Creator: dvips\b/ and $s = 1; 1 == $s and $l =~ /^%!$/ and do { print $O $duper or die "error writing file $tfile: $!"; $s = 0; }; }; print $O $l or die "error writing file $tfile: $!"; } $! and !$!{"EBADF"} and die "error reading file $ifile: $!"; $s and die "$ifile is not dvips-generated postscript"; close $O or die "error closing file $tfile: $!"; system "ps2ps", $tfile, $ofile and die "ps2ps failed"; unlink $tfile or die "cannot unlink $tfile: $!"; } } $duper = <<'EOP'; %ambrus duper /zbd { bind def } bind def /z-showpage { showpage } zbd /z-scale0 { gsave 0 421.37259980 translate 0.70710678 0.70710678 scale -90.0 rotate } zbd /z-scale1 { gsave 0 842.7451996 translate 0.70710678 0.70710678 scale -90.0 rotate } zbd /z-unscale { grestore } zbd /z-dupe-odd false def /z-flush { z-dupe-odd { z-showpage } if } zbd /bop-hook { z-dupe-odd { z-scale0 } { z-scale1 } ifelse /z-dupe-odd z-dupe-odd not def } zbd /eop-hook { z-unscale z-dupe-odd not { showpage } if } zbd /end-hook { /z-flush } zbd /showpage { } zbd % EOP main; __END__

Comment on Home-grown psnup
Download Code
Re: Home-grown psnup
by dpavlin (Friar) on May 29, 2005 at 20:13 UTC
    Another way to do same thing is to use psnup utility (in Debian it's part of psutils). While that has nothing to do with perl, it has served me quite well over the years (even for some perl documentation :-)

    2share!2flame...

      I know psnup, that's why the node title is Home-grown psnup. I really wrote this only to learn some postscript, the perl part is quite trivial.

Re: Home-grown psnup
by Tanktalus (Canon) on May 29, 2005 at 21:19 UTC

    Do you have an aversion to if/unless? I'm noticing a lot of "and"s and "or"s in there that would make more sense as "if"s and "unless"s.

      Yes, you can see this in all my perl code (but not in C). The reason is to show that the condition is important, not the if statement. See also Re: A new idiom -or- I Hate Unless.

Re: Home-grown psnup
by Anonymous Monk on Dec 29, 2012 at 04:29 UTC
    Is there a way to make this put two 4.25 X 7 inch pages on a 8.5 X 7 in page and then another script that will put two 8.5 X 7 pages on a 8.5 X 14 inch page. I have am having problems with psnup recognizing the -W or -H options.

      You could edit the code to do that. But if you just want a utility, you might check out other alternatives that already exist, such as enscript.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://461513]
Approved by thor
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (15)
As of 2014-07-31 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (249 votes), past polls