Wednesday, January 04, 2006

TILDWSB: Creating PDF Reports in Linux

From the Things I Learned During Winter Solstice Break department, I've been toying with generating my system reports in PDF. Normally, my reports are generated in ASCII text, but for some bizarre reason, there are some organizations that are wanting reports in PDF.

I ran into two problems. The first is that the native Linux tools will change text to PostScript, and PostScript to PDF, but not text to PDF. The second problem was that I could not get the PDF converter to accept piped input. This meant it would be a two step process.
    ls -l /etc | enscript -o; ps2pdf x.pdf
The first pipe captures the text output, converts it to PostScript, then redirects it to a file. Next, the ps2pdf utility reads the PS file and converts it to PDF. It's actually pretty sharp.

I was showing this to some folks and explained my two step dilemma. The next day, one of them caught me in the hall and showed me how to get ps2pdf to accept piped input:
    ls -l /etc | enscript -o - | ps2pdf - x.pdf
The second pipe has changed to output to "-". We now pipe to ps2pdf, and replace our input with "-". This has the effect of attaching the output of one to the input of the other. Sweet!

This generated a four page PDF (on my laptop-- YMMV). The next trick was to use mpage to get this onto a single sheet. As it turns out, mpage can do the ASCII to PS conversion:
    ls -l /etc | mpage - | ps2pdf - x.pdf
We use the same dash trick to get it in one step. I felt like this needed one more option, to install a margin around each pane on the page:
    ls -l /etc | mpage -M20 - | ps2pdf - x.pdf
Adding the "-M20" option to mpage, provided a clean, even margin on all sides of the output.

No comments:

Post a Comment