UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2015-01-12 02:57 PM

goldriver
Member
Registered: 2015-01-12
Posts: 2

How to print in linux From A C Program

I am implementing a network spooler printer. I want to communicate with a printer from my program in Linux. I have researched so far that there is CUPS library to achieve direct printing from a code but I would like some insight into it.

After receiving the file from the client side and storing it on the server. What are the steps that must be taken?

Can anyone provide a sample code?
For example i have file ,hello123.txt  and i want to send the print command from the SERVER to the printer. How do i achieve such a functionality?

Offline

#2 2015-01-12 10:44 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: How to print in linux From A C Program

I'm not familiar with the CUPS library...  I keep meaning to dig into it one day, but haven't gotten around to it yet...

The way it's traditionally been done on most Unix-like systems is to just feed the file to "lpr" or "lp" and let them deal with it...  You can do that on a Linux system using CUPS too; it comes with "lpr" and "lp" covers that work like the traditional Unix commands, but using CUPS to do everything...  You can't really access all the nifty CUPS features that way, though...  But, for just simple printing, that might be good enough for you...  Try something like "lpr -P <printername> <file>"...  Where <printername> is the short name for the printer from "/etc/printcap"...  If you've only got one printer configured, then just "lpr <file>" should work...

Offline

#3 2015-01-13 02:01 PM

goldriver
Member
Registered: 2015-01-12
Posts: 2

Re: How to print in linux From A C Program

Usinbg a c program and not the terminal.?

Offline

#4 2015-01-13 09:20 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: How to print in linux From A C Program

Well, sure, you could just run the lpr call via fork()+exec*() or system(), or popen() if you want to write the file contents to its stdin rather than specify a filename on the command-line...  That's what traditional Unix apps generally did when they wanted to print...  But, these days, it is kind of out of fashion on modern systems...  If writing a GUI app, your GUI library probably has some spiffy printing dialog you can use instead, and that might directly use libcups...  Eg: GTK+ has GtkPrintOperation and related stuff...  For non-GUI apps, you're probably left with learning libcups and doing all the work yourself, or just making do with spawning lpr/lp instead...

Offline

Board footer

Powered by FluxBB