How to use Qt QPrinter to send cut paper command

2.4k Views Asked by At

I am writing a Qt desktop program which need to print receipt after a transaction. For this I need to issue a "cut paper" in the end of each receipt. I understand that the following ascii characters(ascii 27 + ascii 105) need to be sent at the end of the print text to cut paper.

I could not find any documentation on how to send this using QPrinter. I use QPrinter & QPainter to implement printing.

If anyone has tried this please advice how to handle cut paper printer command in Qt.

2

There are 2 best solutions below

0
On

I found answer to this question and posting this so that it may be useful for others.

I used append(ascii character) command to append the printer commands to the printer.

Here is the sample code I used:

QString printer_name = "PrinterOne";
qDebug() << "Test printing started...";

QByteArray print_content_ba("Test Print text ");
print_content_ba.append("\n");

//add end of the receipt buffer & cut command
print_content_ba.append(27);
print_content_ba.append(105);

HANDLE p_hPrinter;
DOC_INFO_1 DocInfo;
DWORD   dwJob = 0L;
DWORD   dwBytesWritten = 0L;
BOOL    bStatus = FALSE;

//code to convert QString to wchar_t
wchar_t szPrinterName[255];
int length = printer_name.toWCharArray(szPrinterName);
szPrinterName[length]=0;

if (OpenPrinter(szPrinterName,&p_hPrinter,NULL)){
qDebug() << "Printer opening success " << QString::fromWCharArray(szPrinterName);
DocInfo.pDocName = L"Loyalty Receipt";
DocInfo.pOutputFile = NULL;
DocInfo.pDatatype = L"RAW";
dwJob = StartDocPrinter( p_hPrinter, 1, (LPBYTE)&DocInfo );
if (dwJob > 0) {
    qDebug() << "Job is set.";
    bStatus = StartPagePrinter(p_hPrinter);
    if (bStatus) {
        qDebug() << "Writing text to printer" << print_content_ba ;
        bStatus = WritePrinter(p_hPrinter,print_content_ba.data(),print_content_ba.length(),&dwBytesWritten);
        if(bStatus > 0){
            qDebug() << "printer write success" << bStatus;
        }
        EndPagePrinter(p_hPrinter);
    } else {
        qDebug() << "could not start printer";
    }
    EndDocPrinter(p_hPrinter);
    qDebug() << "closing doc";
} else {
    qDebug() << "Couldn't create job";
}
ClosePrinter(p_hPrinter);
qDebug() << "closing printer";
}
else{
   qDebug() << "Printer opening Failed";
}
1
On

Altough I don't have an exact answer to your question, I've my receipt printer running. The "cut" command is given by the TmxPaperSource=DocFeedCut argument in the print command.

I make a PDF and then send that to the printer (i'm not exactly printing normal receipts...).

void printSomething(QGraphicsScene* scene)
{
    /* Make a PDF-Printer */
    QPrinter pdfPrinter(QPrinter::ScreenResolution);
    pdfPrinter.setOutputFormat( QPrinter::PdfFormat );
    pdfPrinter.setPaperSize( QSize(100, 80), QPrinter::Millimeter );
    pdfPrinter.setPageMargins( QMarginsF(2, 0, 5.8, 0) ); //dont set top and bottom margins
    pdfPrinter.setColorMode(QPrinter::GrayScale);
    pdfPrinter.setResolution(203); //dpi of my printer
    pdfPrinter.setFullPage(true);
    pdfPrinter.setOutputFileName( "hello.pdf" );

    /* Render the Scene using the PDF-Printer */
    QPainter pdfPainter;
    pdfPainter.begin( &pdfPrinter );
    scene->render( &pdfPainter );
    pdfPainter.end();

    /* Print */
    system( std::string("lp -o PageSize=RP80x297 -o TmxPaperReduction=Bottom -o Resolution=203x203dpi -o TmxPaperSource=DocFeedCut -o TmxMaxBandWidth=640  -o TmxPrintingSpeed=auto hello.pdf").c_str() );
}