Use groff to easily create PDF pages

Oct 24, '05 05:09:00AM

Contributed by: kd4ttc

I need to automate PDF document generation and have looked at a number of options for doing this from a PHP enabled web site. The basic problem is that for machine-generated documents, it's easiest to work with text files, but for documents that end up in PDF format with headers, footers, and graphics, a word processor is easiest. Right now I use AppleScript to control Appleworks, but this means I have an Appleworks template, an AppleScript, I then edit a bit, and print to PDF. I have other processes that could benefit from automatic PDF generation, but generating the commands to drive the PDF creation is a bit complex. I want to be able to just generate a text file and have it become a nice PDF.

I ended up looking at TeX and LaTeX, but downloads were huge, a build I needed to do didn't work, and I couldn't get the complex software to work.

Then I stumbled on the standard groff. I had thought it was just for generating decent looking monospaced ASCII for a terminal. How wrong I was. This is a fully featured typesetting program on par with TeX. It takes plain ASCII text and generates beautiful, typeset output. There are resources called macro packages that allow complex editing tasks to be done.

Here are some examples of how it may be used -- these are for use in the Terminal:

Want to print up a hard copy of a man page? groff -l -mandoc `man -w apachectl` will give you the apachectl man page.

Want a postscript file of the groff man page that is then opened in Preview?

$ groff -mandoc >x.ps `man -w groff`
$ open x.ps
How about a system that will typset a manuscript? There is a document processing system called mom. It is a set of commands to groff that resides in what is called a macro file. Create a text file called x.txt and fill it with the following:
.TITLE    "MacOSXHints"
.SUBTITLE "A Fine Source of Hints"
.AUTHOR   "Your Name"
.PRINTSTYLE TYPESET
.blm PP
.START

Here is a fine first paragraph.  

Here is the second.

Here is the third.
Then typeset it and open in Preview:
$ groff -mom >x.ps x.txt
$ open x.ps
Well, not earth shaking, but any text you want to look at is now typeset as a Postscript document. Just put the paragraphs as separate chunks of text separated by blank lines.

The benefit of this is that if you want to generate PDF's programmatically, all you have to do is write a text file, easy from most any program, to a file x.txt and then invoke:
groff -mom x.txt | pstopdf -i -o x.pdf
This uses the pstopdf utility to create file x.pdf from x.txt. You want PDF's of man pages? An exercise for the reader. And groff has great documentation, already on your computer. Look in /usr/share/doc/groff/ and open the folder there (1.19.1 or 1.19.1 or something like that). Go into each folder and find the intro.html file. As an example:

/usr/share/doc/groff/1.19.1/html/momdoc/intro.html

Contains an introduction to mom.

Also, the groff man page is good, as are the roff and troff manpages. Yeah, it is a bit of reading, but it is much easier to use than TeX and LaTeX, and it is already on your system, ready to go.

What I will be doing is using groff to generate automatic reports from PHP. Just have to emit some text to a file and execute the shell command for PDF creation...

[robg adds: I haven't tested this one, and we've mentioned groff on a few occasions before.]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20051020122911920