Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Is it possible to take a file and print it to a PDF using nothing but terminal commands? I am guessing you would somehow make use of the lpr command, but I have yet to discover the correct syntax.

share|improve this question
    
What sort of file? plain text? –  Doug Harris Jan 5 '11 at 15:46
    
@Daniel, I should've been more clear in my question: What sort of source file? -- My answer below will work for plain text files, but not for images. –  Doug Harris Jan 5 '11 at 16:06
    
@Doug I apologize, I actually misread the question. –  Daniel Beck Jan 5 '11 at 16:08
    
@Daniel - What I'm really aiming for is something more graphically intensive, say HTML pages. Is this possible? –  tambler Jan 5 '11 at 18:05
    
Your main problem probably is, that the viewer application is file format dependent. Make up your mind what format you want and you'll get specific answers. –  Daniel Beck Jan 5 '11 at 18:41

7 Answers 7

1) The Open Source Word processor, AbiWord, includes an option of converting between any formats it knows on the commandline, including PDF e.g.,

abiword --to=pdf filename.html

(It'll also handle, e.g., MS Word .doc files, .docx, .odt, .rtf, etc., both input and output.)

I know it's available for Mac, though I've never used it on a mac.

2) The ebook-convert command line program from calibre (on mac you need to follow the instructions here to get the commandline tools) is useful for many formats, too.

ebook-convert filename.html filename.pdf

It can handle .rtf and .odt input, and lots of ebook formats like .epub and .mobi.

3) PrinceXML can be used on the commandline, and will convert HTML to PDF very beautifully.

4) There's wkhtmltopdf, which uses the webkit engine to convert HTML to PDF from the commandline. I've never tried it.

5) I can think of other methods using, e.g., ConTeXt or pdflatex, but they get more and more involved.

share|improve this answer
    
NB: AbiWord doesn't work on intel based Mac –  inspectorG4dget Jan 22 at 18:31

lpr is indeed the correct command. There's not much to it, just

lpr filename

should simply work, presuming that the default printer is set correctly. If not, you might have to do

lpr -Pprintername filename

Note that there's a man page -- "man lpr" if you need to see the content. Yes, the CUPS print daemon that OS X uses is already PDF aware.

share|improve this answer

For plain text files, you can use enscript (comes with OS X) to generate a postscript file.

$ enscript -p ~/Desktop/profile.ps /etc/profile
[ 1 pages * 1 copy ] left in /Users/dharris/Desktop/profile.ps

OS X's Preview app can open (and convert) to PDF, or you can install ps2pdf (I installed it via macports) and convert at the command line.

$ ps2pdf ~/Desktop/profile.ps ~/Desktop/profile.pdf
share|improve this answer
    
What if I wanted to do something more graphically intensive? Say, converting an HTML page to PDF from the terminal? –  tambler Jan 5 '11 at 17:53
    
I know this is old, but PhantomJS can do this, and it does it very well. –  Brad Jun 13 '13 at 3:34

Actually it's a lot easier than that. You can just type on the command line "open *.pdf" and Preview will open with all the files there. Weirdly though if you type "open *.pdf /.pdf" you get two windows. In each you then have to go into the side bar, select one of the boxy formats (if there are a lot of files it opens by default in list format which doesn't work) type cmd-A to select all, then use the print-selected-pages command to get all of them.

share|improve this answer

I know that I have seen something in a merely hidden system folder:

/Library/Scripts/Printing Scripts/Convert To PDF.scpt

I have created a new application from AppleScript but calling the script directly should work also.

EDIT: actually you simply can call

/System/Library/Printers/Libraries/convert -f file.rtf -o file.pdf -j "application/pdf"
share|improve this answer
    
Great Hack !! Thanks. –  Pushpendre Mar 18 at 16:47
    
This answer is obsolete. Tested in OS X 10.9.5. –  ksh Oct 15 at 7:38

For local web page files, you can try to use Download URLs as PDF Automator action. Download and install.


I had to start Automator in 32 bit mode:

alt text


Create an Automator Application that automatically receives files and folders as input, with this action as the only one. Save e.g. as Save as PDF.

Open the Get Info dialog of your application, and check Open in 32-bit mode also here. Double-click it once to make sure Launch Services knows the application.


You can use this application from the command line like this:

osascript -e 'tell application "Save as PDF" to open { POSIX file "/path/to/file.html" as alias }'

Of course, you can wrap this in a nice AppleScript .scpt, bash function, shell script, or what not.


This solution is actually pretty flexible. I was able to convert text files and HTML files, and the Automator action is also somewhat configurable.

share|improve this answer

You could also use pandoc from http://johnmacfarlane.net/pandoc/installing.html

With this command you would convert the html file to pdf:

pandoc -o output.pdf input.html

But note that pandoc uses LaTeX for the conversion, so you have to have a LaTeX system installed (http://www.tug.org/mactex/).

Pandoc can also convert to other formats like .odt, .docx and .epub

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.