Does anybody have a tested function in C or C++ that sends text to the printer in a Windows Console Application? I would be very appreciative!
It would be nice, if I could specify the font. Yes, I can google!
Does anybody have a tested function in C or C++ that sends text to the printer in a Windows Console Application? I would be very appreciative!
It would be nice, if I could specify the font. Yes, I can google!
A concise, reliable approach is to use the Win32 GDI print pipeline so Windows renders the font and layout for you. and gave the classic raw-port / FILE* ideas (writing to PRN/LPT or fopen/ fprintf) — those can work for simple raw text on legacy setups but do not let Windows rasterize a chosen font. For rendered text use CreateDCA (WINSPOOL) -> StartDocA -> StartPage -> select a font (CreateFontA) -> TextOutA / DrawTextA -> EndPage -> EndDoc -> DeleteDC. Example:
// Minimal example (requires <windows.h>)
bool PrintTextToPrinter(const char* printerName, const char* text,
const char* faceName, int pointSize)
{
HDC hdc = CreateDCA("WINSPOOL", printerName, NULL, NULL);
if (!hdc) return false;
DOCINFOA di; ZeroMemory(&di,sizeof(di)); di.cbSize = sizeof(di); di.lpszDocName = "PrintJob";
if (StartDocA(hdc,&di) <= 0) { DeleteDC(hdc); return false; }
StartPage(hdc);
int dpiY = GetDeviceCaps(hdc, LOGPIXELSY);
HFONT hFont = CreateFontA(- (pointSize * dpiY) / 72, 0,0,0, FW_NORMAL,0,0,0, ANSI_CHARSET,
OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH|FF_DONTCARE, faceName);
HFONT old = (HFONT)SelectObject(hdc,hFont);
TextOutA(hdc, 100, 100, text, (int)strlen(text));
SelectObject(hdc, old); DeleteObject(hFont);
EndPage(hdc); EndDoc(hdc); DeleteDC(hdc);
return true;
} Notes and troubleshooting:
Jump to Post— Narue 5,707The easiest solution is to first try opening one of the following streams:
ofstream pout("PRN"); ofstream pout("LPT1"); ofstream pout("PRN:"); ofstream pout("LPT1:");If none of those work, and your compiler doesn't support an extension for talking to a printer, google over to MSDN and see what you can …
Jump to Post— Excizted 67i want to print the report on printer.
which function plz tell me
Lol? They just told above. And what report?
Elaborate if you want any kind of help, and please include why you think the two previous suggestions are useless :)
The easiest solution is to first try opening one of the following streams:
ofstream pout("PRN");
ofstream pout("LPT1");
ofstream pout("PRN:");
ofstream pout("LPT1:"); If none of those work, and your compiler doesn't support an extension for talking to a printer, google over to MSDN and see what you can find. But don't use the MSDN search feature, it really bites.
Narue,
thanks a lot! I will test drive your suggestion!
If you don't mind using the old FILE structure, you can print by using:
FILE *fp;
fp = fopen(prn, w);
fprintf(prn, "%s", charstr);
i want to print the report on printer.
which function plz tell me
i want to print the report on printer.
which function plz tell me
Lol? They just told above. And what report?
Elaborate if you want any kind of help, and please include why you think the two previous suggestions are useless :)
can anyone give example of this printer statement??
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.