7

So I'd like to add a "footer" (an attribution) to the bottom of every page of a pdf file I am generating via postscript with groff in linux. I am converting the file from ps to pdf myself, with the ps2pdf tool, so I have access to both formats.

These two posts have been somewhat helpful:

How to add page numbers to Postscript/PDF

How can I make a program overlay text on a postscript file?

I'm not against using the first method, but I don't have access to the pdflatex utility mentioned in the first script, nor do I have the option to install it on the machine that needs to do the work.

It looks like the second method could possibly work, but I have version 8.15 of ghostscript installed and I didn't see many of the flags listed on the man page ( http://unix.browserdebug.com/man/gs/ ). I think I have access to the "-c" flag to insert some postscript code, even though it is not listed. Anyhow, here are two commands I tried unsuccessfully:

gs -o output.pdf -sDEVICE=pdfwrite -g5030x5320 \
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (test-string) show" \
-f input.ps

that gives me this:

Unknown switch -o - ignoring
ESP Ghostscript 815.02 (2006-04-19)
Copyright (C) 2004 artofcode LLC, Benicia, CA.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
ERROR: /undefinedfilename in (output.pdf)
Operand stack:

Execution stack:
   %interp_exit   .runexec2   --nostringval--   --nostringval--   --nostringval--   2   %stopped_push   --nostringval--   --nostringval--   --nostringval--   false   1   %stopped_push
Dictionary stack:
   --dict:1117/1686(ro)(G)--   --dict:0/20(G)--   --dict:102/200(L)--
Current allocation mode is local
Last OS error: 2
ESP Ghostscript 815.02: Unrecoverable error, exit code 1

So obviously the -o flag has a problem and so I did some research and tried this syntax:


gs -sOUTPUTFILE=output.pdf -sDEVICE=pdfwrite -g5030x5320 \
-c "/Helvetica-Italic findfont 15 scalefont setfont 453 482 moveto (test-string) show" \
-f input.ps

which outputs this and makes me hit return 4 times (maybe there are 4 pages in input.ps)


ESP Ghostscript 815.02 (2006-04-19)
Copyright (C) 2004 artofcode LLC, Benicia, CA.  All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Can't find (or can't open) font file /usr/share/ghostscript/8.15/Resource/Font/Helvetica-Italic.
Can't find (or can't open) font file Helvetica-Italic.
Querying operating system for font files...
Didn't find this font on the system!
Substituting font Helvetica-Oblique for Helvetica-Italic.
Loading NimbusSanL-ReguItal font from /usr/share/fonts/default/Type1/n019023l.pfb... 3742416 2168114 2083056 759694 1 done.
Loading NimbusRomNo9L-ReguItal font from /usr/share/fonts/default/Type1/n021023l.pfb... 3781760 2362033 2365632 1015713 1 done.
Loading NimbusRomNo9L-Medi font from /usr/share/fonts/default/Type1/n021004l.pfb... 3865136 2547267 2365632 1029818 1 done.
Loading NimbusRomNo9L-Regu font from /usr/share/fonts/default/Type1/n021003l.pfb... 4089592 2759001 2365632 1032885 1 done.
Using NimbusRomanNo9L-Regu font for NimbusRomNo9L-Regu.
>>showpage, press <return> to continue<<

>>showpage, press <return> to continue<<

>>showpage, press <return> to continue<<

>>showpage, press <return> to continue<<


So it seems like it would be simple enough to use gs to simply insert something in a ps file, but it is proving to be quite complicated...

Community
  • 1
  • 1
cwd
  • 53,018
  • 53
  • 161
  • 198

3 Answers3

7

In your PostScript file you can use a page counter and redefine showpage to display it in the footer. Here's a sample program:

4 dict begin

/showpage_org /showpage load def            % you'll need this later!  
/page_num 0 def  
/page_str 3 string def                      % Page numbers -99 to 999 supported, error if > 3 char

/showpage                                   % with page number footer  
{  
    gsave
    /Courier findfont 10 scalefont setfont  % Set the font for the footer  
    /page_num page_num 1 add def            % increment page number counter  
    10 10 moveto (Page ) show                 
    page_num page_str cvs show              % convert page number integer to a string and show it  
    grestore  
    showpage_org                            % use the original showpage  
} def  

%Page 1  
/Courier findfont 22 scalefont setfont  
100 500 moveto (Hello) show  
showpage  

%Page 2  
100 500 moveto (World) show  
showpage  

end
Shmoken
  • 71
  • 1
  • 3
  • thanks for the answer! haven't had time to test this yet (wrote the question a while ago) but if I get back to this and it works well I will change my accepted answer. – cwd Jul 08 '11 at 12:11
  • You can use "bind def" in your new "showpage" function. This way, it uses the original "showpage" inside, without the need of an auxiliary method, avoiding recursion just as well. – Ricardo Nolde Sep 19 '11 at 17:37
  • How do you apply that command to all/multiple pages? (instead of copy-paste the code #NumOfPages times...) – Dor Jun 21 '16 at 19:05
  • @RicardoNoldeThere is no recursion. – U. Windl Oct 24 '22 at 08:19
  • @Dor As each page uses `showpage`, you only have to redefine that, and nothing when it is called/executed. – U. Windl Oct 24 '22 at 08:21
5

ESP Ghostscript is O-o-o-o-old. Don't use it any more unless you absolutely, absolutely cannot avoid it. It was a fork of the original Ghostscript which used by CUPS for a while. (And after some problems between developers where resolved, more recent versions of CUPS now also use the GPL Ghostscript again...)

Newer GPL Ghostscript versions are here: http://www.ghostscript.com/releases/

Also, -o out.pdf is only a shorthand for -dBATCH -dNOPAUSE -sOutputFile=out.pdf. So you should try this. (The -dNOPAUSE part relieves you from hitting <return> for every page advance....).

Lastly, don't expect the full range of documentation being provided by a third party man gs page. Rather refer to the original Ghostscript documentation for the version you use, the most important parts being:


Update: Ghostscript has moved to Git (instead of Subversion) for their source code repository. Therefor the following links have changed, repeatedly:

U. Windl
  • 3,480
  • 26
  • 54
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • I'm using a system that has ESP installed without privileges to install an upgraded version. I wonder if there is a way I can get it to work with ESP... – cwd Feb 05 '11 at 06:59
  • ESP Ghostscript is no longer maintained.... The only way to get this to work as a non-privileged user is to additionally install a newer version of Ghostscript into your home directory (or any other place writeable by your account). – Kurt Pfeifle Feb 12 '11 at 11:04
  • https://stackoverflow.com/a/6620599/6607497 should work with *any* PostScript interpreter. – U. Windl Oct 24 '22 at 08:22
1

The most logical place to add page footers is in the groff source. The exact way to do this will of course depend on the macro package youre using. For -ms, you can do:

.ds RF "Page \\n(PN

to add the page number as right footer. For -mm, it's more like:

.PF "'''Page \\\\nP'"

where the single quotes delimit the 'left part'center part'right part' of the footer.

luser droog
  • 18,988
  • 3
  • 53
  • 105