Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » Print raw data (direct print)
Print raw data (direct print) [message #35876] Fri, 30 March 2012 10:36 Go to next message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor

Hello,

I propose to add method to communicate direct with a printer.

For this I want to share with windows code, for linux I developed this by using lpr -oraw in nodeJS JavaScript code.

OK. The windows U++ version is:
#include <Core/Core.h>
#include <windows.h>
#include <Winspool.h>
#pragma  comment(lib, "Winspool.lib")

using namespace Upp;

bool RawDataToPrinter(const String &printername, const String &data, const String &docname, const String &type);

bool RawDataToPrinter(const String &printername, const String &data, const String &docname){
	return RawDataToPrinter(printername, data, docname, "RAW");
}

bool RawDataToPrinter(const String &printername, const String &data){
	return RawDataToPrinter(printername, data, "My Document", "RAW");
}

#if COMPILER_MSC
// 
// RawDataToPrinter - sends binary data directly to a printer 
//  
// printername: String specifying printer name
// data:        String raw data bytes
// docname:     String specifying document name
// type        	String specifying data type. E.G.: RAW, TEXT, ...
//  
// Returns: true for success, false for failure. 
//  
bool RawDataToPrinter(const String &printername, const String &data, const String &docname, const String &type){
    bool     bStatus = false;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob = 0L;
    DWORD      dwBytesWritten = 0L;

    // Open a handle to the printer. 
    bStatus = OpenPrinter( (LPTSTR)(~printername), &hPrinter, NULL );
    if (bStatus) {
        // Fill in the structure with info about this "document." 
        DocInfo.pDocName = (LPTSTR)(~docname);
        DocInfo.pOutputFile = NULL;
        DocInfo.pDatatype = (LPTSTR)(~type);

        // Inform the spooler the document is beginning. 
        dwJob = StartDocPrinter( hPrinter, 1, (LPBYTE)&DocInfo );
        if (dwJob > 0) {
            // Start a page. 
            bStatus = StartPagePrinter( hPrinter );
            if (bStatus) {
                // Send the data to the printer. 
                bStatus = WritePrinter( hPrinter, (LPTSTR)(~data), data.GetLength(), &dwBytesWritten);
                EndPagePrinter (hPrinter);
            }else{
                Cout()<<"StartPagePrinter error\n";
            }
            // Inform the spooler that the document is ending. 
            EndDocPrinter( hPrinter );
        }else{
            Cout()<<"StartDocPrinter error\n";
        }
        // Close the printer handle. 
        ClosePrinter( hPrinter );
    }else{
        Cout()<<"OpenPrinter error "<<printername<<"\n";
    }
    // Check to see if correct number of bytes were written. 
    if (!bStatus || (dwBytesWritten != data.GetLength())) {
        bStatus = false;
        Cout()<<"not sent all bytes\n";
    } else {
        bStatus = true;
    }
    return bStatus;
}
#else
	#error Wrong OS for compile raw printer
#endif


and a little test to communicate with ZEBRA label printer:
CONSOLE_APP_MAIN
{
	String data;
	data<<"N"
		<<"\nS4"
		<<"\nD15"
		<<"\nq400"
		<<"\nR"
		<<"\nB20,10,0,1,2,30,173,B,\"123456\""
		<<"\nP0"
		<<"\n";
	
	ToAscii(data);
	if(RawDataToPrinter("\\\\ilupascu.main.orange.md\\ZEBRA", data)){// or local printer just the name of the printer
		Cout()<<"OK\n";
	}else{
		Cout()<<"FAILED\n";
	}
}
Re: Print raw data (direct print) [message #35894 is a reply to message #35876] Sat, 31 March 2012 17:05 Go to previous messageGo to next message
Lance is currently offline  Lance
Messages: 526
Registered: March 2007
Contributor
Interesting.

A few years ago I did some work to print to ESC-capable printer (eg. Epson dot-matrix) because I need to print to label of 1" height.

Your work could be used when there are no fixed paper heights, e.g., receipt printing. A quick question, can I use it to print formatted text or even image?
Re: Print raw data (direct print) [message #35896 is a reply to message #35894] Sat, 31 March 2012 21:14 Go to previous message
tojocky is currently offline  tojocky
Messages: 607
Registered: April 2008
Location: UK
Contributor


Lance wrote on Sat, 31 March 2012 18:05

A quick question, can I use it to print formatted text or even image?
It depends on printing language!
Previous Topic: What's up with Google subversion mirror?
Next Topic: Precisions on keyflags value in MouseMove
Goto Forum:
  


Current Time: Fri Mar 29 11:10:49 CET 2024

Total time taken to generate the page: 0.01699 seconds