Possible to print to PDF from Mac terminal?

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.

111k 16 16 gold badges 290 290 silver badges 338 338 bronze badges asked Jan 5, 2011 at 15:38 371 1 1 gold badge 3 3 silver badges 4 4 bronze badges What sort of file? plain text? Commented Jan 5, 2011 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.

Commented Jan 5, 2011 at 16:06

@Daniel - What I'm really aiming for is something more graphically intensive, say HTML pages. Is this possible?

Commented Jan 5, 2011 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.

Commented Jan 5, 2011 at 18:41

Did one of the responses answer your question? If not, what doesn't work or doesn't behave as intended?

Commented Jan 28, 2011 at 15:42

10 Answers 10

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

lpr filename.pdf 

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

lpr -P printername filename.pdf 

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.

103 4 4 bronze badges answered Jun 13, 2013 at 21:10 312 2 2 silver badges 4 4 bronze badges Provided solution gives zero-size pdf on Yosemite. Any ideas how to solve it? Commented Jun 1, 2015 at 12:41

Sorry for duplication. Provided solution gives zero-size pdf on Yosemite, when I'm trying to pring .pages document. RTF or txt are printed well. Any ideas how to fix it?

Commented Jun 1, 2015 at 13:06

lpr does not generate pdf files, so I am not sure I understand the comment. You cannot generate pdf files with lpr.

Commented May 18, 2017 at 21:32 This does not answer the original question as it was how to 'print to pdf', not print a pdf. Commented Jun 27, 2017 at 1:29

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" 
answered Jan 15, 2012 at 13:53 user113731 user113731 This answer is obsolete. Tested in OS X 10.9.5. Commented Oct 15, 2014 at 7:38

@4ae1e1 No, it isn't. Just that "convert" doesn't exist. It's a symlink to /usr/sbin/cupsfilter and cupsfilter will still work this way even on 10.13.6.

Commented Nov 8, 2018 at 3:13

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.