PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 15, 2022

[FIXED] How to programmatically get the name of the current printer through the libcups in Linux?

 September 15, 2022     c++, cups, linux, printing     No comments   

Issue

I'm trying to get the name of the current printer using the libcups library in Linux, but I can't find such a method. I found only how to get a complete list of printers, but how to find out which one will print is not clear.

#include <cups/cups.h>

QStringList getPrinters()
{    
   QStringList printerNames;
   cups_dest_t *dests;
   int num_dests = cupsGetDests(&dests);
   for (int pr = 0; pr < num_dests; ++pr) {
      QString printerName = QString::fromUtf8(dests[pr].name);
      printerNames.append(printerName);
   }
   cupsFreeDests(num_dests, dests);
   return printerNames;
}

Solution

Once you have a valid destination (cups_dest_t), one can retrieve the informations via: cupsGetOption

Example (from https://openprinting.github.io/cups/doc/cupspm.html#basic-destination-information):

const char *model = cupsGetOption("printer-make-and-model",
                                  dest->num_options,
                                  dest->options);

To find the default printer one can use:

  • cupsGetDest (param name: NULL for the default destination)
  • cupsGetDests2 (param http: Connection to server or CUPS_HTTP_DEFAULT)

Other suggestion would be:

  • https://openprinting.github.io/cups/doc/cupspm.html#finding-available-destinations

Last but not least:

  • CUPS Programming Manual

Sidenote:

Since you're using Qt, doesn't Qt have printer support?

E.g.

QPrinter::QPrinter(const QPrinterInfo &printer, QPrinter::PrinterMode mode = ScreenResolution);

(see https://doc.qt.io/qt-6/qprinter.html#QPrinter-1)

and

bool QPrinterInfo::isDefault() const;

(see https://doc.qt.io/qt-6/qprinterinfo.html#isDefault)



Answered By - Erdal Küçük
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing