C++ Redirect output to a printer or PDF

Please, read the updated version here: http://www.matejtomcik.com/Public/Projects/CPPPrinterStream

I have been wondering quite a long time if there is a way how one can redirect C++ cout to a printer. STD does not support such a method so if you need this behavior, you have to create your own output stream just like I did. And I want to share my source code with you, so start by downloading printerstream class.

This small handy template class is an extension of a standard output stream (std::basic_ostream) class which provides the functionality to output whatever you want to a printer instead of console window. To make it work, first download the printerstream class. Unzip and add printerstream.h and printerstream.cpp to your project. Note that printerstream is implemented for a Windows platform only. Then you can create an instance of the printerstream class in several ways:

  • printerstream<char> output(bool useDefaultPrinter = false);
    will create an instance of the printerstream asking user to choose the printer using standard print dialog. First parameter to this constructor is omited, but if you set it to true, the default printer will be choosen instead of showing print dialog.
  • printerstream<char> output(“PDFCreator”);
    will initialize printerstream using the printer specified by its name. To enumerate local printers on your PC call printerstream<char>::enumerate_printers.
  • printerstream<char> output(handle to a drawing context);
    will initialize printerstream using an already open handle to a drawing context (HDC). You can create a HDC using CreateDC function. Thats how the previous constructor creates a handle to the printer.

Once the printerstream object is initialized you have to call begin(const char * documentName = 0) method to start a new document. Without this call the printerstream will ignore any output written to it. This method basically creates a new print job, so if you need to split your report to multiple documents (PDFs for example), call begin everytime you wish to start a new print job. Specifying a document name is optional.

To explicitly finish the print job, call end method. To abort the print job, call abort method. If a previous call to a begin method was made, printerstream will implicitly call end in its destructor finishing the print job and sending it to the printer. It is also possible to set the page margins calling margins method.
REMEMBER: printerstream class translates page size into pixels, so to set margins to a 5% of the document size, you can use this method:

printer.margins(page_margins((printer.page_width() / 100) * 5));

Click “Read more” for an example code.

#include "printerstream.h"

using namespace std;

int main()
{
	// Initialize output to a printer using PDFCreator
	printerstream<wchar_t> printer("PDFCreator");
	// Set margins to a 5% of the document width
	printer.margins(page_margins((printer.page_width() / 100) * 5));
	// Begin printing a new document called "Test"
	printer.begin("Test");
	// ... print here
	printer << "Hello world";
	// This can be omited
	printer.end();

	return 0;
}

To see the output, click download sample PDF output.

Reklamy

Pridaj komentár

Zadajte svoje údaje, alebo kliknite na ikonu pre prihlásenie:

WordPress.com Logo

Na komentovanie používate váš WordPress.com účet. Odhlásiť sa / Zmeniť )

Twitter picture

Na komentovanie používate váš Twitter účet. Odhlásiť sa / Zmeniť )

Facebook photo

Na komentovanie používate váš Facebook účet. Odhlásiť sa / Zmeniť )

Google+ photo

Na komentovanie používate váš Google+ účet. Odhlásiť sa / Zmeniť )

Connecting to %s