3

I want to print a book on a laser printer, so I have prepared a PostScript file ready for printing, with reordered and 2-up'ed pages. Now I want to add booklet marks on appropriate pages, like on the following picture:

Booklet marks

From other SO questions I know that its showpage command that separates individual pages in PS file, so I wrote simple Perl script which counts showpage's occurences and prepends PostSript code if necessary, just to test if this approach works:

#!/usr/bin/env perl
use strict;
use warnings;

my $bookletSheets = 6;

my $occurence = 1;
while (my $line = <>) {
    if ($line !~ /^showpage/) {
        print $line;
        next;
    }

    my $mod = $occurence % (2*$bookletSheets);
    if ($mod == 1) {
        print " 1    setlinewidth\n";
        print " 5  5 newpath moveto\n";
        print "-5  5 lineto\n";
        print "-5 -5 lineto\n";
        print " 5 -5 lineto\n";
        print " 5  5 lineto\n";
        print "0 setgray\n";
        print "stroke\n";
        print "%NOP\n"
    }

    print $line;
    $occurence++;
}

But after running:

$ cat book.ps | ./preprocess.pl > book-marked.ps

I can see no signs of additional marks in document viewer, despite the code got injected correctly. What have I done wrong?

There are some links I've based my thinking on:

Community
  • 1
  • 1
firegurafiku
  • 3,017
  • 1
  • 28
  • 37

1 Answers1

3

After some more investigation, it turned out that the lack of image was caused by BoundingBox clipping. Bounding box was shifted because the PS file was obtained from PDF with stripped margins.

firegurafiku
  • 3,017
  • 1
  • 28
  • 37
  • 2
    +1 glad you solved it. Another gotcha that I was about to post as a possible answer is the current transformation matrix. You might want to add `defaultmatrix setmatrix` at the front of your injected code to make sure your coordinates and size numbers are being interpreted at the correct position and scale.. – luser droog Dec 27 '15 at 04:34
  • @luserdroog: in fact I didn't solved the whole task yet, as it turns out that `pdf2ps` and `psnup` do extremely weird things with PostScript code. It seems that the only reliable approach is to override `showpage` in PostScript itself, avoiding text-level manipulation entirely. – firegurafiku Dec 27 '15 at 14:25