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!

Dani AI

Generated

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:

  • Use the exact printer name (enumerate printers or use GetDefaultPrinterA). CreateDCA returns NULL if the name is wrong or the spooler is unavailable.
  • Convert point sizes to device pixels via GetDeviceCaps(LOGPIXELSY) as shown. For wrapping/multi-line or pagination use DrawTextA or measure text with GetTextExtentPoint32A.
  • For raw, unformatted output (receipts, ESC/POS) prefer OpenPrinter / StartDocPrinter / WritePrinter instead of GDI.
  • This method yields true font control (face, size, style) and is appropriate for console apps that must print rendered text; it answers and supplies the example requested by and .

Recommended Answers

All 6 Replies

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

commented: Don't bump old threads -1

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??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.