Linux使用cups进行打印

复制的代码,还没有试过。

#include

//...

int print_label( const char *text, const char *printer_name, const char *job_name )

{

    int jobId = 0;

jobId = cupsCreateJob( CUPS_HTTP_DEFAULT, printer_name, job_name, 0, NULL );

if ( jobId > 0 )

    {

        qDebug( ) << "Printing job #" << jobId << " (\"" << job_name << "\").";

const char* format = CUPS_FORMAT_TEXT; // CUPS_FORMAT_POSTSCRIPT;

cupsStartDocument( CUPS_HTTP_DEFAULT, printer_name, jobId, text, format, true );

        cupsWriteRequestData( CUPS_HTTP_DEFAULT, text, strlen( text ) );

cupsFinishDocument( CUPS_HTTP_DEFAULT, printer_name );

    }

return jobId;

}

//...

// Now, inside any Qt function (may be a slot):

QPrinter printer;

QPrintDialog *dialog = new QPrintDialog( &printer, this );

dialog->setWindowTitle( tr( "Zebra label" ) );

if ( dialog->exec( ) != QDialog::Accepted )

    return;

// This is the sample label. Can be anything.

const char label[] =

    "^XA~TA000~JSN^LT0^MNW^MTD^PON^PMN^LH0,0^JMA^PR4,4^MD0^JUS^LRN^CI0^XZ\n"

    "^XA\n"

    "^MMT\n"

    "^LL0600\n"

    "^PW900\n"

    "^LS0\n"

    "^BY2,3,54^FT24,109^BCN,,Y,N\n"

    "^FD>;43210000>773>0000^FS\n"

    "^PQ1,0,1,Y^XZ\n";

// Informative only.

ui->txtLabelScript->setPlainText( label );

// Call the printing function.

if ( print_label( label, printer.printerName( ).toAscii( ), "Zebra_Label" ) == 0 )

    qDebug( ) << "CUPS Error: " << ippErrorString( cupsLastError( ) );

它已经完成了。 不要忘记链接libcups(-lcups)。 我仍然希望任何伙伴添加另一个解决方案,证明Qt-only是可能的。同时,这已经足够了。 谢谢大家。

你可能感兴趣的:(Linux/Shell,C/C++)