Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a footer to the bottom of each page of a postscript or pdf file in linux?

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...

like image 231
cwd Avatar asked Jan 22 '11 07:01

cwd


2 Answers

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
like image 123
Shmoken Avatar answered Sep 24 '22 16:09

Shmoken


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=outpdf. 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:

  • current development branch: Readme.htm + Use.htm + Ps2pdf.htm
  • 9.00 release: Readme.htm + Use.htm + Ps2pdf.htm
  • 8.71 release: Readme.htm + Use.htm + Ps2pdf.htm

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

  • current development branch: Readme.htm + Use.htm + Ps2pdf.htm
  • current development branch: Readme.htm + Use.htm + Ps2pdf.htm
like image 21
Kurt Pfeifle Avatar answered Sep 24 '22 16:09

Kurt Pfeifle