Listing Names of Printers in Dart Programming

In this post, we’ll explore various ways to list the names of printers in Dart programming, particularly when working with Flutter using plugins and native integrations.

Listing Names of Printers in Dart Programming

Getting Started

Printing from a Dart programming or Flutter application is a common requirement for Point of Sale (POS) systems, invoice generation, and document management apps. One useful feature is the ability to list all available printer names so users can select the device they want to print from.

Getting a list of printers (i.e., printers available on the local network or shared by other devices) is not directly supported in Flutter/Dart programming out-of-the-box. However, it can be done depending on the platform and printer type. You can use platform channels or system calls to fetch the list of printers, including shared ones.

Listing Names of Printers

Here’s a breakdown of how to get a list of shared printers, along with code examples for what’s possible in Dart/Flutter:

using printing package

The most common approach in Flutter to access printing features is the printing package, which provides a cross-platform API for printing documents and listing available printers including shared one.

Add the package to your project, once you added the package succesfully your pubspec.yaml file will look like below.

pubspec.yaml code:
 dependencies:  
  flutter:  
   sdk: flutter  
  printing: ^5.11.0 # use latest version  

You can use the printing plugin to list available printers. These could include shared printers if they are discoverable (e.g., AirPrint, IPP).

Dart code:
 import 'package:printing/printing.dart';  
 void main() {  
  final printers = await Printing.listPrinters();  
  for (final printer in printers) {  
   print('Printer name: ${printer.name}');  
  }  
 }  

Output
 Printer name: HP LaserJet Pro MFP M28-M31  
 Printer name: Canon TS3300 series  
 Printer name: Microsoft Print to PDF  

Using win32 package

if your code is targeting only Windows platform, then you can use the win32 package to query printers via Windows API. Check below code:

Dart code:
 import 'package:win32/win32.dart';  
 import 'dart:ffi';  
 void main() {  
  final level = 2;  
  final flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS;  
  final pcbNeeded = calloc<Uint32>();  
  final pcReturned = calloc<Uint32>();  
  // First call to get required buffer size  
  EnumPrinters(flags, nullptr, level, nullptr, 0, pcbNeeded, pcReturned);  
  final pPrinterEnum = calloc<Uint8>(pcbNeeded.value);  
  final result = EnumPrinters(  
   flags,  
   nullptr,  
   level,  
   pPrinterEnum,  
   pcbNeeded.value,  
   pcbNeeded,  
   pcReturned,  
  );  
  if (result != 0) {  
   final printers = pPrinterEnum.cast<PRINTER_INFO_2>();  
   for (var i = 0; i < pcReturned.value; i++) {  
    final printer = printers.elementAt(i).ref;  
    print('Printer name: ${printer.pPrinterName.toDartString()}');  
    print('Shared?: ${printer.Attributes & PRINTER_ATTRIBUTE_SHARED != 0}');  
   }  
  } else {  
   print('Error enumerating printers');  
  }  
  calloc.free(pPrinterEnum);  
  calloc.free(pcbNeeded);  
  calloc.free(pcReturned);  
 }  

Using system commands

You can call shell commands from Dart to list printers in Linux / macOS operating system.

Dart code:
 import 'dart:io';  
 void main() {  
  final result = await Process.run('lpstat', ['-p']);  
  if (result.exitCode == 0) {  
   print('Available printers:\n${result.stdout}');  
  } else {  
   print('Failed to list printers: ${result.stderr}');  
  }  
 }  

Make sure cups (Common UNIX Printing System) is installed.

Summary

Listing printer names in Dart is straightforward using the printing package for most platforms. It allows your Flutter app to offer users a choice of available printers, improving the flexibility of your printing solution.

Thanks

Kailash Chandra Behera

An IT professional with over 13 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies. Demonstrated expertise in delivering all phases of project development—from initiation to closure—while aligning with business objectives to drive process improvements, competitive advantage, and measurable bottom-line gains. Proven ability to work independently and manage multiple projects successfully. Committed to the efficient and effective development of projects in fast-paced, deadline-driven environments. Skills: Proficient in designing and developing applications using various Microsoft technologies. Total IT Experience: 13+ years

Previous Post Next Post

نموذج الاتصال