Solved

Printing to printer in C++

Posted on 2002-04-04
7
527 Views
Last Modified: 2008-03-17
Hi,

I am writing a C++ client application that will be receiving a set of lines for printing ie. say 50 lines per transmission from a server.  My application is supposed to receive these lines on a socket and send it to the printer.

The question is how do I send these periodic lines that I receive from the server and send it to the printer?  How do I decide when the lines are equal to one page and forward it to the printer?

Any kind of help would be appreciated.  An example code would be really helpful.
 
Thanks in advance.

P.S: This is my first time in asking a question so please bear with me if you dont understand it.
0
Comment
Question by:jaldu
7 Comments
 
LVL 4

Expert Comment

by:ct.smith
ID: 6919740
A relatively easy approach is to jsut write to some sort of temp file and then fire it off to the printer queue.  then things like page sizes and such are not an issue.

Or, on unix systems, you can write directly to the printer's file descriptor, just like any other file (not usually a great option).

If it's not simple text, you'll need to learn the API of some sort of third party library.  For portability, I recommend Qt.  A good library will do most of the work for you, like getting the page size etc, then you typically define what's to be printed by creating a "graphic" of what you want on the page.  But all this varies depending on what library you use.
0
 

Author Comment

by:jaldu
ID: 6920744
Thanks.

Writing to a temp file and then firing it off to a printer queue is a good idea.  I have tried to look up for code on how to send a file to a printer but couldnt find any.  I have seen examples like:

****************************************
#include <fstream.h>

int main()

{
ofstream print; // stream variable declaration
print.open("LPT1"); // open stream

// Print Text (the character ‘\f’ will produce a form feed)
print << "This text will print on the printer.\f";
print.close(); // close stream
return 0;
}
****************************************

But I want a dialog box to come up(on NT) and let the user pick any network printer.

So in lieu of the above example I should be writing all that I receive from the server to a temp file and then once done read the file line by line and send it to the printer?

Thanks in advance.

0
 
LVL 4

Expert Comment

by:ct.smith
ID: 6922023
Sorry, I have no idea how to get the printer selcteion APIs on Windows.  You may have a bit of luck looking up that part on MSDN.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Accepted Solution

by:
TheFriend earned 20 total points
ID: 7004215
Here's the sequence, see doc's for proper syntax

Call PrintDlg() to get a DC, paper size, printer capabilities. That dialog works in both ways, with user interaction and without.
Calculate the surface size you need to print. If you print text only, use GetTextExtentPoint32() to get the height of a line of current font.
You can start a print job right away with StartDoc().
Then use StartPage() to start a new page.
Use TextOut() to print to the printer DC.
Check if have printed enough lines on this page.
Call EndPage() and goto StartPage() again.
Call EndDoc() when you are done.

PrintDlg can give you the default printer as well.

If the time between transmitions from the server is too long, you might print one page per document. So don't go to StartPage() but call EndPage() and EndDoc() and the wait for more data from that 50-line-server. This releases the spooler and completes the print job after one pagefull.

All the functions mentioned have their 'overview' in MSDN publications with examples included.

I am not including any example since the structures used are to big with too many parameters and require a lot of typing. Sorry.

TheFriend
0
 

Author Comment

by:jaldu
ID: 7005855
Hi TheFriend,

Thanks.

>>>I am not including any example since the structures used are to big with too many parameters and require
a lot of typing. Sorry.

What you have written will at least get me going.  So thanks.

I will let you know once I look into it and try out few things to get a better understanding.

Thanks.

RJ

0
 
LVL 11

Expert Comment

by:griessh
ID: 7122083
Dear jaldu

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "TheFriend"

comment(s) as an answer.

If you think your question was not answered at all, you can explain here why you want to do this and post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
ID: 7138044
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now