Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

EDIT: I forgot to make it clear I'm using python and pycups. The code is python but I shouldn't have thought that was enough.

I've dug through the cups documentation a few times and couldn't find anything to do this. Is there any message that gets returned on a successful print that can be captured?

For example in this code:

import os
import cups

conn = cups.Connection()

f = open('testreceipt.txt', 'w')
f.write('something')
f.close()
printer_returns = conn.printFile('EPSON_TM-P2.01', 'testreceipt.txt', 'test', {})
print printer_returns

All that printer_returns gets assigned appears to be the print job id (208,209,...,n). This occurs whether or not a printer is connected, and thus can't be relied upon for confirmation.

In this case the target printer is a receipt printer attached by a serial->usb cable into a raspberry pi running raspbian.

I've considered monitoring the cups log file for successful prints but that seems like a bad way to go about it.

I'm not sure where else to go with finding a solution so if anyone has thoughts, please share them.

share|improve this question
    
If the printer is not connected, CUPS should keep it in the queue unti it is available. –  jordanm Jan 29 '13 at 2:48
1  
Define "successful print". A job could be in the queue indefinitely, is that "successful" when printed, even if there was a delay? There are lots of things to watch out for here. It is better to narrow down to "do something when printed" rather than "do something when successful", which is largely ambiguous. –  Chris Down Jan 29 '13 at 3:08
    
Sorry! This is a good point. In this case a successful print is a complete physical print and not just a queued job. I need to make sure the print occurred or return an error. –  Jeff H. Jan 31 '13 at 1:04
add comment

3 Answers

After sending the document, check the cupsLastErrorString. If it's null, you should be good. Also, most of the functions should tell you if it failed, as they return an integer.

Found here

share|improve this answer
    
Unfortunately I didn't find a duplicate function in pycups. The IPPError class considers a queued job an error free print. packages.python.org/pycups/cups.IPPError-class.html –  Jeff H. Jan 31 '13 at 1:24
add comment

This is what I finally ended up doing. The printFile() function returns the print id which cups uses, while the current print queue through the commandline is accessible via lpstat. I think it's a bit messy but it's good enough for my needs right now. Thanks for the help everyone, you all nudged me in the appropriate direction.

20 printid = conn.printFile('EPSON_TM-P2.01', 'testreceipt.txt', 'test', {})
21 
22 stop = 0
23 TIMEOUT = 60
24 
25 while str(subprocess.check_output(["lpstat"])).find(str(printid)) > 0 and    stop < TIMEOUT:
26     stop+= 1
27     time.sleep(1)
28 if stop < TIMEOUT:
29     print "PRINT SUCCESS"
30 else:
31     print "PRINT FAILURE"
share|improve this answer
add comment

I believe the equivalent in pycups is the IPPError class (note the try / except example code), since the cupsLastError* functions in cups returns the IPP error codes.

Checking the status code of a subprocess should also work, but seems like unnecessary overhead if pycups will give you the code directly.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.