[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: How to Print to Printer from g++ program?



> As a physicist I wrote large programs in Pascal before retiring in
> 1994 but nothing since until last week.  I thought of a problem I
> wanted to solve, read up on C++ and got my program working with only
> a few struggles with syntax but how to write directly to the printer
> has eluded me.

This has little to do with C++; it is instead a question of operating
environment.  I'll assume that you want to print portably on Unix-like
operating systems.

The Right Thing on Unix for printing is to execute "lpr" in a child
process, and write PostScript down a pipe to that process' standard
input.  See fork(2), pipe(2), execvp(3), and lpr(1).  Bonus points for
allowing the user to configure the arguments you pass to lpr so they
can, e.g., print to a different printer with an lpr that doesn't
understand the PRINTER environment variable.

Most Unix programs do exactly this.  It is up to the user to ensure
that feeding PostScript to lpr does the right thing, even on
non-PostScript printers.

> I know when I wrote those Pascal programs I distributed them to
> people with different printers.  The programs could check printer
> capabilities and print excellent graphs if they found a postscript
> printer.  I haven't the foggiest recollection of how I programed
> this.

There are probably ways to do this if your users have a more advanced
printing system like CUPS installed, but most don't.

> If this question is regarded as wildly off target for this list, for
> the record I tried a Google search...

It's probably off topic, but not wildly so.

Hope this helps,
Lucas
-- 
Lucas Bergman <lucas@fivesight.com>
Tired of getting duplicate copies of mailing list messages?  I respect
the 'mail-followup-to' header field: http://cr.yp.to/proto/replyto.html



Reply to: