Printing from UNIX programs

eyver

Registered
How would I go about printing (to an Epson or HP printer) from, for example, an X-Windows program like the GIMP? When you set up your printer in the MacOS X environment, does it automatically set that printer to the "lp" device in UNIX? Or how would I go about making printing possible? (This is assuming the printer is USB and hooked up directly to my Mac.)
 
No, setting up a printer in OS X does nothing to the PRINTER environment variable, or the behaviour of lp, unfortunately. lp takes postscript files, while OS X works with pdf files - printing to eg. a LaserWriter actually translates pdf to ps before sending it off, so you might be able to print to such a printer, if it takes ps files. Your typical USB printer, as far as I can tell, won't print straight from Unix very easily.

You might see about saving the ps file that would have been printed, and then opening that file in OS X and printing it. I have ps2pdf, to do the conversion from the commandline, but I'm afraid I forgot where I have it from...
 
This excerpt was COPIED from a link over at http://www.macosxhints.com and I don't know if it works because I haven't tried this yet.

Setting up lpd printing on OS X
Fri, Dec 21 '01 at 10:33AM ? from: eagle lpd is the standard Unix printing system, so others who come from the Unix community (as I did; a longtime Linux and NeXT user) might be interested in sharing their printers to their other Unix machines.

See, not having lpd as the printing system in OS X was a big bummer to me, because it meant that I could no longer print with my NeXT and other Unix computers, because my printer is now hooked to my G4 Cube (permanently assigned to OS X 10.1). With this little hacked up lpd, I can now do that again. :)

Read the rest of the article for the how-to.

[Editor's note: I have not done this on my machine as of yet, but Daniel vouches that it works. Any errors in the following article are a result of my formatting work; please let me know if you see any troublesome lines!]

Here's how to get lpd working:

1] Create the printcap entry. Here's the entry I used:

lp: \
:sh=true:eek:f=/Users/foo/Applications/lpd-hack: \
:lf=/var/log/lpd-errs:sd=/var/spool/output/lpd:lp=/dev/null:


The options are:

* sh=true -- suppresses the header page (sh=true is required to print PostScript and PDF files)
* of=.../lpd-hack -- this is the Output Filter that lpr will use. This is what actually prints the file. The of parameter must point to the actual executable script.
* lf=/var/log/lpd-errs -- this was in there by default and I didn't remove it
* sd=/var/spool/output/lpd -- the spool directory; needs to be a valid directory
* lp=/dev/null -- the device where the printer exists; I use /dev/null because I have no printer device!

After you have edited /etc/printcap, simply run:

niload printcap / < /etc/printcap

2] Save the following as the script pointed to by the "of=" parameter above

#!/usr/bin/env perl
$temp_file = "/tmp/printing.$$";

# LPD passes via STDIN the file to be printed
# (we don't have any knowledge of the original filename...too bad)
# save this as a file to be processed
@lines = <STDIN>;
open(TMPFILE,">/tmp/printing.$$");
print TMPFILE @lines;
close(TMPFILE);

`/usr/sbin/Print /tmp/printing.$$`;
`/bin/rm -f /tmp/printing.$$`;


Don't forget to make the script executable.

3] Start lpd. lpd resides at /usr/libexec/lpd. Simply run "/usr/libexec/lpd" as root from the command line.

4] Restart the queue. Run "lpc" and issue the command "restart all" then exit lpc.

5] Now print. Make sure the $PRINTER environment variable matches the name of the printer in the printcap entry. The default printer is "lp" so if you use the above, you need not set $PRINTER. Print using the command lpr, as in:

lpr file.txt
lpr file.ps
lpr file.pdf


With this setup, you can print text, PostScript and (!) PDF files. Additionally, you can share a networked printer. Simply add the remote machine's DNS name (haven't tried IP address yet but I would bet that it works) to /etc/hosts.lpd or /etc/hosts.equiv on the OS X box, you can configure /etc/printcap on the remote machine and use lpr/lpd on the remote machine to spool to a printer on the OS X box.
 
the line that reads '@lines = ;' isn't valid Perl. It's missing the < STDIN >

Second, I dont see how the filter discerns between text, PS and PDF since
the 'Print' command needs to be told what it's printing particularly since
the commentary shows that they have no clue as to what the filename is.

A better method might be to use 'file' (which uses /etc/magic) to determine
the filetype and then set the proper flags to Print.

[localhost:~] howardm% /usr/sbin/Print
Print [flags] file
-Pqueuename: print the job to this queue/printer.
DEFAULT: is the currently selected printer
-Mmimetype: mime type(format) or the print job.
DEFAULT: look at the file extension
VALID TYPES: Postscript(ps), PDF(pdf), Text(txt)
-Npages: number of pages per sheet (nUp)
NOTE: (pdf documents only)
 
Originally posted by howardm4
Second, I dont see how the filter discerns between text, PS and PDF since the 'Print' command needs to be told what it's printing particularly since the commentary shows that they have no clue as to what the filename is.

According to the man page for Print(1), "If file format is inconclusive, Print looks for %!PS or %PDF in the job to determine if the format is postscript or pdf."
 
Back
Top