OpenPrinting / cups

OpenPrinting CUPS Sources
https://openprinting.github.io/cups
Apache License 2.0
1.01k stars 182 forks source link

Get the print result or status of the printer through the API #769

Closed qq645327806 closed 1 year ago

qq645327806 commented 1 year ago

Hello, Recently, I want to write code to get printer status and result after creating the print job. But I don't know what APIs do I need. The functionality I want to implement is as follows:

  1. When the print job is created, I want to know the printer if start to print. If it's not printed, how can I get the reason.
  2. When the print job is finished, how do I know if job is finished. How do I get exception information if an error occurs during printing? (Errors such as: paper jam, paper out, out of ink )

    I looked at a little code and tried to write a test code, but it didn't work for my purposes. This is the code file printer.c that I have written. I was hoping someone could guide me.

    Thank you.

                                                                                                                                                         Best Regards

    printer_2023_08_01_151951.zip

qq645327806 commented 1 year ago

I thought for a moment, does CUPS only get printing success or not?

michaelrsweet commented 1 year ago

Once you submit a job, you can monitor the job to see its state (IPP_JSTATE_PENDING when queued, IPP_JSTATE_PROCESSING when it starts printing, IPP_JSTATE_COMPLETED, `IPP_JSTATE_CANCELED, or IPP_JSTATE_ABORTED when the job is done.

ipp_jstate_t get_job_state(const char *printer_name, int job_id)
{
  char printer_uri[1024];
  ipp_t *request, *response;
  ipp_jstate_t job_state;

  httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", NULL, "localhost", 631, "/printers/%s", printer_name);
  request = ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES);
  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printer_uri);
  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "job-state");

  response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/jobs");
  job_state = (ipp_jstate_t)ippGetInteger(ippFindAttribute(response, "job-state", IPP_TAG_ENUM), 0);
  ippDelete(response);

  return (job_state);
}
qq645327806 commented 1 year ago

Thank you for your reply. @michaelrsweet . I had solved my problem.