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

Print to Unix systems without direct printer access UNIX
My department runs a fairly large network of Unix machines with two "industrial" public printers. I often work there on my PowerBook, connected to the network via a public port. When I needed to print something, I used to save it as a PDF, ftp or scp it to my Unix account, then log in via ssh, use pdf2ps, and finally print the document using lpr. Having read about PDF Workflow here on oxshints, I decided to streamline this process a little. I now have a "Print in DCS" (Department of Computer Science) button in every Print dialogue. First, I placed this shell script in ~/Library/PDF Services. Remember to replace username@server in the two places it appears. The string Server in the third dialogue was originally the name of my server; you can put the name of yours there if you want.

You will also need to have exchanged SSH keys with the server, so that you can log in without a password. I suggest that anyone who uses SSH regularly should do this as it makes life so much easier. See the SSH documentation for details. Finally, the script above sends the PDF directly to the printers. I discovered that they are able to handle this natively. If yours can't, you will need to add pdf2ps to the chain.

In case you were wondering, I can't use any of the various remote printing technologies because only Department machines can use those printers. This is entirely sensible as they operate a "printer credits" system they wouldn't want people bypassing.
    •    
  • Currently 3.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[9,701 views]  

Print to Unix systems without direct printer access | 13 comments | Create New Account
Click here to return to the 'Print to Unix systems without direct printer access' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Print to Unix systems without direct printer access
Authored by: Kool on Apr 20, '05 04:20:53PM

Another approach would be to set up the printer locally by setting the hostname to localhost and choosing the appropriate printermodel and printqueue name, then do this:

sudo ssh -L515:<ssh-server>:515 <username>@<ssh-server>

Now you can print as normal. You need to rerun the above command each session.

Not sure, if this is better than this hint or not. Just wanted to point out this way too.



[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: richwiss on Apr 20, '05 10:57:50PM

Can you explain that a little further? I like this idea (since I remotely print somewhat frequently) but my limited understanding of port forwarding keeps me from knowing what's actually going on.



[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: Kool on Apr 21, '05 06:27:43AM

What they above command does is forward my local port 515 to port 515 of the ssh- aka printerserver. So when this ssh-tunnel is set up, I print to localhost on port 515. This is then forwarded to the ssh-tunnel, which makes it looks like I print on the ssh-server from the administration point of view.

This works great for me, I only need to remember to make sure the ssh-tunnel is established.



[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: PeteVerdon on Apr 23, '05 07:42:30PM

Not sure this would work in my department. Everything is locked down pretty tightly. Plus, although that's conceptually simpler, it would actually be less convenient than the "Print in DCS" button I currently have. Might be handy for some people, though.



[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: fenner on Apr 20, '05 05:53:00PM
A small modification to your script which may make it more reliable: use
lpr -r printUpload.pdf
Most versions of lpr support the "-r" flag, which specifies that it should remove the file after printing is done. It may be possible that the
lpr printUpload.pdf; sleep 1; rm printUpload.pdf
will remove the file before lpr gets a chance to actually send it to the printer.

[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: PeteVerdon on Apr 23, '05 07:44:59PM

Yup, that's a lot better. Bear in mind that this was a quick hack written quite quickly after I was fed up with doing it manually one too many times. But anything involving sleep is usually a monstrous hack.



[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: PeteVerdon on May 10, '05 08:56:34AM

I've just tried to make this modification, and it seems the version of lpr on our Sun server doesn't in fact have an option to remove the file. Never mind - I'm out of there in a month or so any way.

Pete



[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: GaelicWizard on Apr 21, '05 01:57:18AM
Whoa! That's *two* ssh connections, and two unqualified `rm's. Lets try this:


#!/bin/bash

printResult=`ssh username@server lpr 2>&1 < "$3"`
if [ -n "$printResult" ]; then
	osascript -e 'tell application "System Events" to display dialog "$printResult" buttons {"OK"} default button 1'
	exit 3
fi

rm -f "$3" || exit 2


You'll notice that at the end of the ssh line, "$3" is piped to stdin of the ssh process. No second ssh connection. Also, no deletion of the file on the server, as there is no file. Also, no need for `lpr -r ...' that another poster mentioned, as there is again no file.

I also changed the AppleScript to tell "System Events" to display the dialog, instead of Finder as that is old and incorrect. At the same time, I re-wrote the dialog to a) not add unneeded information (i.e. "Server's lpr said..."), which may be wrong (i.e. "Server's lpr wanted to say: `ssh: connection failed`") and b) to be all in one argument because it saves a few nano-seconds processing time for osascript (which is dog slow) and it makes it easier to read.

It is rather superfluous to inform the user of an error in deletion, as the file is in /tmp anyway and will be cleared at a later time, so I just removed that part. I still maintain the return value, should anyone care. Just no useless information.

I haven't looked, but if PDF services provides the name of the application doing the printing, then the AppleScript should use *that* application to display the dialogs, not the Finder or System Events.

Hope this works better for you! :-)

---
Pell

[ Reply to This | # ]

Print to Unix systems without direct printer access
Authored by: Sathamoth on Apr 22, '05 01:21:33AM
You have to quote $printResult properly to get its content displayed in a dialog box. In addition to that, the exit status of SSH command should be checked as well. So, my suggestion:

#!/bin/bash

PRINT_RESULT=`ssh username@server lpr 2>&1 < "$3"`
EXIT_STATUS=$?;

if [ -n "$PRINT_RESULT" ] || [ $EXIT_STATUS -ne 0 ]; then
	osascript -e 'tell application "System Events"' -e "display dialog \"$PRINT_RESULT\" buttons {\"OK\"} default button 1" -e 'end tell'
	exit 3
fi

rm -f "$3" || exit 2

---
--
Sathamoth

[ Reply to This | # ]

Print to Unix systems without direct print er access does not quite work
Authored by: ars on Apr 25, '05 11:31:53AM

I used this script from home with one modification to print on the printer in my office at work. I specified the name of the printer by modifying the lpr command to: lpr -Pmyprintername
I put the file in PDF services folder and printed a test page from Textedit. The file was printed, but it was not recognized as a pdf file, so that I got several pages of print-out most of them with only a few lines with gibberish on them. I guess that I might need to append the output file with .pdf. What is the syntax for this?



[ Reply to This | # ]
Print to Unix systems without direct print er access does not quite work
Authored by: Sathamoth on Apr 27, '05 01:05:00AM

The PDF file which is produced contains .pdf extension already (the temporary file is something like /tmp/printing.123456/Print job.pdf, filename being always "Print job.pdf") so you shouldn't have to append the .pdf extension by hand. Try adding -p option to lpr command.

---
--
Sathamoth



[ Reply to This | # ]
Print to Unix systems without direct print er access does not quite work
Authored by: PeteVerdon on Apr 27, '05 11:25:31AM

It sounds more like your printer doesn't understand PDF natively. That's not at all surprising - most big lasers only speak Postscript and plain ASCII. My department's printers are new this year; the old ones didn't do PDF. As I suggested, you'll need to include pdf2ps or something similar in the process; off the top of my head I don't know the best way to do that. I'm sure some of the more skillful Bash users who've already posted here can help.



[ Reply to This | # ]
Print to Unix systems without direct printer access
Authored by: PeteVerdon on Apr 23, '05 07:52:19PM

As I said to Fenner, this was a quick hack. So I'm not remotely worried about things like the number of SSH connections opened - if I'm using this then clearly I'm in the same building as the server so latency isn't an issue. Your use of stdin across the network is nifty, though - I didn't realise ssh did that.

I don't understand what you mean about "unqualified" rms.



[ Reply to This | # ]