A simple way to create PDFs from the command line

Dec 08, '09 07:30:03AM

Contributed by: owlgorithm

I have used many different methods over the years to print documents to PDFs from the command line. Some have been complicated sequences of pipes to and from groff, others required TeX, and occasionally I set up a "virtual" printer, simply to print to file. I recently read documentation for cups-pdf, however, and found that cupsfilter command is sufficient for most of my own tasks in its bare form!

For example, to print 80-column ASCII plaintext (the majority of my code), I can use this:

$ cupsfilter foo.txt > foo.pdf 
If you find the output of that command a bit verbose (as I do), you can send the errors silently to the null device using this version:
$ cupsfilter foo.txt > foo.pdf 2> /dev/null
There are many ways to wrap this simple command even more conveniently, but I'll omit those here for now. The reason this method is ideal is because it uses built-in routines in OS X; any time you can take advantage of these, do, because many of the core technologies are significantly faster and more secure than third-party alternatives.

Of course, your mileage may vary :-).

[robg adds: cupsfilter PDF conversions work in at least 10.5 and 10.6, and I suspect in earlier OS X releases.]

Comments (10)


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