10.4: Remote printing via SSH using PDF Services

Dec 07, '06 07:30:00AM

Contributed by: FlyBoy

I travel a fair bit and, on occasion, have the need to print to my home color laser printer. Printer sharing in OS X works great over the local area network (LAN), but trying to print from across the country to my home printer would seem to require opening up my home firewall to the outside world, which is something I really don't want to do. For unix gurus, the following hint may seem pretty basic, but it does use some unique OS X Tiger features, like Automator and PDF Services.

As part of the CUPS printing architecture, the command line tool lpr allows printing many types of files out of the box, including PDFs. OS X Tiger includes a great feature known as PDF Services, which allows the creation and manipulation of PDF files through the standard print dialog for any OS X printer. OS X also includes the secure shell, SSH, allowing secure, remote access across the internet. These three features allowed me to create a simple workflow to print to my home printer from anywhere with internet access. This hint assumes the following:

  1. You have either a static IP address, internet hostname, or dynamic host name through DynDNS (or another dynamic host name provider)
  2. Your local network firewall allows SSH access to one of your computers through a forwarded port (SSH is typically on port 22)
  3. You have public key authentication enabled for logging in to the remote machine, so you don't manually need to enter a password to login
  4. You are running something like SSHKeyChain or some other program that allows Automator access to your SSH passphrase, permitting the public key authentication without user intervention.
PDF services allows the user to place various scripts in the users ~/Library/PDF Services directory that can be called from the Print dialog. If one clicks on the PDF button with the downward facing arrow in the Print dialog, one finds all these scripts available. The Print dialog passes the PDF file to the script for further processing.

Since one can forward commands to a remote host through SSH, including the lpr command, and one can pipe input into that forwarded command, printing a PDF remotely to a CUPS-enabled computer is relatively straighforward shell command:

cat filename.pdf | ssh user@remotehost.org lpr
This command passes the content of the file filename.pdf to the remote lpr command via the user account at remotehost.org. The file will print on the default printer for the remote host. Choosing a specific printer is fairly easy to do using the -P destination option for lpr. See the man lpr command in Terminal to alter the script below to your specific needs.

Automator is a useful tool to create PDF Services workflows. In this case, I created an Automator workflow with a single Run AppleScript action (in the Automator library). The content of this action is:
on run {input, parameters}
  repeat with i in input
    set cmd to "cat " & quoted form of POSIX path of i ¬
     & " |ssh user@remotehost.org lpr"
    set theResult to do shell script cmd
  end repeat
  return input
end run
I saved this script in ~/Library/PDF Services. If you don't have that directory, either create the directory in the Finder or, alternatively, in the Terminal with a mkdir ~/Library/PDF\ Services command. The backslash before the space in PDF Services escapes the space from the shell, so that the name is parsed correctly.

This command takes the input (the list of PDFs to print), goes through that list one by one, and has the cat command send the full contents of the file to the remote lpr command. Returning input at the end of the script allows further processing of the files to occur if desired. The user will need to change the user and remotehost.org variables to the appropriate login name and host name for the remote computer that is connected to a remote LAN printer, either through printer sharing or on the remote network.

Comments (10)


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