Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject PostScript code before 'showpage'

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:

  • https://stackoverflow.com/a/532220/1447225
  • http://newsgroups.derkeiler.com/Archive/Comp/comp.lang.postscript/2007-11/msg00097.html
  • http://www.physics.emory.edu/faculty/weeks//graphics/howtops1.html
like image 779
firegurafiku Avatar asked Feb 11 '26 05:02

firegurafiku


1 Answers

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.

like image 176
firegurafiku Avatar answered Feb 15 '26 06:02

firegurafiku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!