Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Remote printing via SSH using PDF Services Apps
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.
    •    
  • Currently 2.33 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[27,383 views]  

10.4: Remote printing via SSH using PDF Services | 10 comments | Create New Account
Click here to return to the '10.4: Remote printing via SSH using PDF Services' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Remote printing via SSH using PDF Services
Authored by: Egrefen on Dec 07, '06 09:10:45AM

This is a pretty cool hint, from a technical standpoint, and I'll keep it handy should the occasion to use it arise.

However, I'm a bit puzzled when it comes to that specific point: what context would warrant using such a script?

If I need a printed copy of a document immediately, this doesn't help.
If I need a copy waiting for me when I get home, can't I just save the document as a PDF and print it when I get home?
If I need someone at home to have a copy, can't I just email them the PDF?

The two cases where I think something like this would come in handy is if I'm away, need to something in print as soon as I get home and don't have time to print it when I get home (if, for example, I need to rush out again immediately).

Or if, for example, I need 10 documents in print for my grandmother, but don't know if she'll know how to print them all (or simply don't want to bother her), and can simply SSH into her mac to print them (so she can just collect them).

But that's about it.

Any other uses come to mind?



[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: FlyBoy on Dec 07, '06 09:21:56AM

While you can email a document to someone else to print, you can transfer the document to the home network via scp or some other means, or you could just hold the document to print when you get back to the home/office, these options require intervention from other users or just plain remembering to print the document next time you get on the local network. If the goal is to print a document to a printer at a remote location, this option avoids bothering someone else, filling up an email box with a large file, etc.

I considered all those other options, but wanted to have the capability to print when needed without bothering anyone else.



[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: edjusted on Dec 07, '06 10:08:38AM

I also agree that this is a neat hint. I also want to point out that if your printer is a network printer with an ip address, print jobs held in queue will stay there and automatically print the next time you're attached to the network with no user intervention. I do this all the time. I'll "print" to my office printer when I'm at home, and when I come in to the office and connect to the network that the printer's on, everything in the queue starts printing out.



[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: freilich on Dec 07, '06 11:25:37AM
This is a great hint for sending faxes by using the phone line back home! I've been doing something similar for a while by calling it from the command line.

lp -d Internal_Modem -o phone=12125551212 file.pdf

[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: joemoreno on Dec 07, '06 12:37:58PM
Great hint! An alternative to using DynDNS is Hamachi which will create a VPN that can tunnel through firewalls and NAT and your VPN clients are each assigned a static IP address.

[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: da2357 on Dec 07, '06 01:53:49PM

TO "joemoreno": However, Hamachi is a Windows application and since macosxhints is a Mac support site, Hamachi will do us little good.



[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: joemoreno on Dec 07, '06 04:12:12PM
Actually, Hamachi runs on Windows, Mac OS X (where I used it) and Linux
http://homepage.mac.com/lxr/homepage/spaceants/hamachix/download.html

[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: joemoreno on Dec 07, '06 04:13:53PM
10.4: Remote printing via SSH using PDF Services
Authored by: maggard on Dec 08, '06 07:56:33AM

Hamachi actually solves the printing issue in a couple of ways:

  • Hamachi’s connections are AES encrypted, so your connection is well secured.
  • Hamachi takes care of finding your connection, so no need to install & maintain a dynamic DNS client or account.
  • Hamachi is remarkably easy to get through firewalls & routers (including dual-NAT setups) etc. It’ll even relay connections that wouldn’t otherwise work. The same isn’t true for SSH tunnels.
  • After enabling the printer to be shared from your Mac (using whatever flavor of sharing is appropriate) you can then install the Mac’s printer as a networked printer on your client machine and print directly from it.

Of course through the Hamachi VPN tunnel you’ll also see your shared iTunes from the remote client(s), able to connect to your Mac’s built-in VNC server for remote control, mount any folders or drives it is sharing, etc.

You can go with the bare-bones Hamachi for MacOS X install, or GUI with the 3rd party HamachiX. Also there is a shu-weet Dashboard widget for Hamachi. All are free, and Hamachi’s mediation is free and perfectly acceptable for this application (they offer paid levels of service with compression & faster relaying but that’s just gravy.)



[ Reply to This | # ]
10.4: Remote printing via SSH using PDF Services
Authored by: ars on Dec 07, '06 01:11:26PM
There was a previous hint about remote printing to Unix printers via ssh:
http://www.macosxhints.com/article.php?story=2005041819210211
That hint should also work for remotely printing to a Mac, running OS X.
I use it from time to time to print something on my work computers from home, so that it is ready when I walk into the office. I had to modify that old hint in order to print to my work printers, as they were expecting postscript files. I used for a while pdf2ps on the remote site, but that often failed for printing pdf files. Now I modified my script to use convert on my Mac to do the conversion.

[ Reply to This | # ]