[SOLVED] Printing to a [Zebra] CUPS printer from my Qt application
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Printing to a [Zebra] CUPS printer from my Qt application
I have written a Qt4 app and need to print to my barcode printer. I can create a valid barcode file and if I use cutecom to copy this file via the printer's serial port it prints the label, so I know I am producing the correct information.
I am having problems with CUPS though. Test pages print blank for a start. At first if I called the cups API with this kind of thing:
Code:
jobId = cupsCreateJob( CUPS_HTTP_DEFAULT, printer_name, job_name, 0, NULL );
if ( jobId > 0 )
{
const char* format = CUPS_FORMAT_TEXT; // CUPS_FORMAT_POSTSCRIPT;
cupsStartDocument( CUPS_HTTP_DEFAULT, printer_name, jobId, text, format, true );
// text is the ASCII string sent that can be sent via the serial port and works
cupsWriteRequestData( CUPS_HTTP_DEFAULT, text, strlen( text ) );
cupsFinishDocument( CUPS_HTTP_DEFAULT, printer_name );
}
It would print text rather than the barcode. Now I can't get anything out of it. I think I should be able to do something like:
lp -d S4M zebra.zpl
To send the file to the printer and, indeed, you can see the printer receives data but prints nothing out.
Is there something different/extra I need to do when using CUPS rather than the serial port for getting a printer to actually print out?
I've worked with the CUPS library before, however when I printed, the text came from a file. I do not know if that makes a difference. Do you know if your specialized printer requires text-formatted data, or does it accept postscript-formatted only? You seem to be certain that it is text only.
Here's some code I played with in the past (written in C++); it is very similar to yours in a lot of respects:
Code:
int
PrintUtility::print(std::istream& is, const char* filename)
{
int jobId = 0;
if (is.good() && !myPrinterName.empty())
{
collectOptions();
jobId = cupsCreateJob(CUPS_HTTP_DEFAULT, myPrinterName.c_str(), filename,
myNumOptions, myOptions);
if (jobId > 0)
{
const char* format = (std::string(filename).find(".ps") != std::string::npos ?
CUPS_FORMAT_POSTSCRIPT : CUPS_FORMAT_TEXT);
char buffer[4096] = {0};
snprintf(buffer, sizeof(buffer) - 1, "%s", filename);
cupsStartDocument(CUPS_HTTP_DEFAULT, myPrinterName.c_str(), jobId, buffer, format, true);
while (is.good())
{
is.read(buffer, sizeof(buffer));
if (is.gcount() > 0)
{
cupsWriteRequestData(CUPS_HTTP_DEFAULT, buffer, is.gcount());
}
}
cupsFinishDocument(CUPS_HTTP_DEFAULT, myPrinterName.c_str());
}
releaseOptions();
}
return jobId;
}
Last edited by dwhitney67; 10-02-2011 at 10:11 AM.
Well, I am pretty sure it's just a text thing. If I copy a text file to its serial port it prints out fine, and the ZPL language datasheet just goes on about ASCII characters.
Yours does indeed look very similar to mine. I've seen lots of people say this is doable, and I am sure I'm just missing something basic that would make it spring in to life! Just can't suss it though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.