【问题标题】:How to use Qt QPrinter to send cut paper command如何使用Qt QPrinter发送剪纸命令
【发布时间】:2014-11-11 00:38:09
【问题描述】:

我正在编写一个需要在交易后打印收据的 Qt 桌面程序。为此,我需要在每张收据的末尾开一张“剪纸”。 我了解需要在打印文本末尾发送以下 ascii 字符(ascii 27 + ascii 105)来剪纸。

我找不到任何关于如何使用 QPrinter 发送此文件的文档。我使用 QPrinter & QPainter 来实现打印。

如果有人试过这个,请建议如何在 Qt 中处理切纸打印机命令。

【问题讨论】:

    标签: c++ qt printing qprinter


    【解决方案1】:

    我找到了这个问题的答案并将其发布,以便对其他人有用。

    我使用 append(ascii character) 命令将打印机命令附加到打印机。

    这是我使用的示例代码:

    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";
    }
    

    【讨论】:

      【解决方案2】:

      尽管我对您的问题没有确切的答案,但我的收据打印机正在运行。 “cut”命令由 print 命令中的TmxPaperSource=DocFeedCut 参数给出。

      我制作了一个 PDF,然后将其发送到打印机(我并没有完全打印普通收据...)。

      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() );
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-12
        • 1970-01-01
        • 2020-11-16
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        • 2021-08-10
        相关资源
        最近更新 更多