4

I'm writing an application that faxes a document (many supported types) provided by the end user. A requirement is that the end user can also provide text to be used as part of a custom fax header.

I've been using Ghostscript to render PDFs as TIFFs and it's been working great so far, but I have yet to find a straightforward way of overlaying the custom header at the top of a PDF. I've tried out a few recommendations:

... with no luck.

I've used ImageMagick to do this successfully with documents rendered to TIFF via other tools, and I'm aware that ImageMagick can render PDF-to-TIFF on its own. However, I want to stick with Ghostscript because in my experience it has performed better and rendered clearer TIFFs.

Is this possible using Ghostscript and perhaps a PS helper script?

Edit:

Ghostscript (v9.04) is not throwing any errors. For example:

gswin64c -dSAFER -dBATCH -dNOPAUSE -dPDFFitPage -sDEVICE=tiffg3 ^
    -sOutputFile=goofy.tif ^
    -c "/Courier findfont 12 scalefont setfont 50 765 moveto (header text) show" ^
    -f goofy.pdf

... produces a TIFF of the original PDF, but without the text I tried to add. If I append showpage to the postscript one-liner it (predictably, I suppose) prints a new, blank-except-for-header page, which doesn't help me much.

Community
  • 1
  • 1

3 Answers3

3

I would use another commandline tool combined with Ghostscript for this task. This tool is pdftk.exe. Then use a 3 step approach:

  1. The task of Ghostscript would be to create an (otherwise empty) page with the header text:
    gswin64c.exe ^
      -o header.pdf ^
      -sDEVICE=pdfwrite ^
      -c "/Courier findfont 12 scalefont setfont" ^
      -c "50 765 moveto (header text) show showpage"
    
  2. The task of pdftk would be to overlay (stamp or background) the PDF file with the text header over the original PDF:
     pdftk.exe goofy.pdf background header.pdf output goofy-with-header.pdf
    or
     pdftk.exe goofy.pdf stamp header.pdf output goofy-with-header.pdf
  3. The last step is to employ Ghostscript again in order to create your final TIFF output:
    gswin64c.exe ^
       -dPDFFitPage ^
       -o goofy-with-header.tif ^
       -sDEVICE=tiffg3 ^
        goofy-with-header.pdf
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
1

I just tried your exact same approach with your exact same result. Then I removed -dSAFER from my command-line arguments and it works like a charm.

chowey
  • 9,138
  • 6
  • 54
  • 84
0

The way I see doping it is appending what you need on the PDF to the PDF file itself - before any conversion - the PDF filew format is designed so that one can append extra information at the end of the file (even information that goes on previous pages).

Unfortunatelly, I never worked on it - so I can'tell you eactly what you need to do - maybe there is aome PDF editing library in a programing language that would make this task easier - else you will have to create the PDF bits yourself. (Tradiditional libraries that render PDF's from some input format won't do, as you have to work inside the structure of your existing document) - but maybe taking a looka t the PDF specification can enlighten you on this approacj, and you check if it is worth: http://www.adobe.com/devnet/pdf/pdf_reference_archive.html

Another approach there would be to work on the "other end" of your files: layout text on the post-rendered TIFF files using an image manipulation library. This is only possible, of course, if there is a fixed space on the pages reserved for you to add the information.

Sorry for not being able to offer a complete solution

jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • I was using a commercial library to do the PDF manipulation, but it often rendered images incorrectly so I was aiming to replace it with Ghostscript. If I can't get this to work end-to-end with Ghostscript I will likely stamp the header on the rendered TIFF with ImageMagick. – lightthievesall Jan 17 '12 at 16:01
  • As I said - all ghostscript could do would be to render extra PDF (or postscript) code that you would add yourself to the code. BTW - are the docuemnts a single page long or multi-page? – jsbueno Jan 18 '12 at 02:37