windows_printer 0.2.1 copy "windows_printer: ^0.2.1" to clipboard
windows_printer: ^0.2.1 copied to clipboard

PlatformWindows

A Flutter plugin for managing and interacting with printers on Windows platforms.

Windows Printer #

A comprehensive Flutter plugin for managing and interacting with printers on Windows platforms, with specialized support for both regular printers and thermal/receipt printers.

Features #

  • Universal Printer Support: Works with regular office printers and thermal/receipt printers
  • Printer Management: Get available printers, set default printer, view properties
  • Thermal Printer Support: Built-in ESC/POS command generation for receipt printers
  • Multiple Print Modes: Raw data, PDF documents, and rich text with formatting
  • Advanced Configuration: Paper size details, printer properties, and settings dialog

Requirements #

  • Windows platform
  • Flutter 3.0.0 or higher
  • Windows 10 or higher (recommended)

Installation #

Add this to your package's pubspec.yaml file:

dependencies:
  windows_printer: ^0.2.0

Quick Start #

Import the package:

import 'package:windows_printer/windows_printer.dart';

Basic Example: #

// Get available printers
List<String> printers = await WindowsPrinter.getAvailablePrinters();

// Print to default printer
if (printers.isNotEmpty) {
  // Print a PDF file
  final file = File('document.pdf');
  final bytes = await file.readAsBytes();
  await WindowsPrinter.printPdf(data: bytes);
}

Thermal/Receipt Printer Usage #

Built-in ESC/POS Receipt Builder: #

import 'package:windows_printer/windows_printer.dart';

Future<void> printReceipt() async {
  // Create receipt using built-in builder
  final receipt = WPReceiptBuilder(wpPaperSize: WPPaperSize.mm80)
    .header('COFFEE SHOP')
    .subtitle('123 Main Street')
    .separator()
    .item('Coffee', '\$3.50')
    .item('Muffin', '\$2.75')
    .separator()
    .total('\$6.25')
    .footer('Thank you!')
    .drawer() // Open cash drawer
    .build();

  // Print to thermal printer
  await WindowsPrinter.printRawData(
    printerName: 'Your Thermal Printer',
    data: Uint8List.fromList(receipt),
    useRawDatatype: true, // Essential for thermal printers
  );
}

Advanced ESC/POS Generator: #

Future<void> printAdvancedReceipt() async {
  final generator = WPESCPOSGenerator(paperSize: WPPaperSize.mm58);
  
  // Add content with custom styling
  generator.text('RECEIPT', style: const WPTextStyle(
    bold: true, 
    align: WPTextAlign.center, 
    size: WPTextSize.doubleHeightWidth
  ));
  
  generator.separator();
  generator.text('Coffee        \$3.50');
  generator.separator();
  
  // Add barcode
  generator.barcode(WPBarcodeType.code128, '123456789012');
  
  // Add image
  generator.image(imageData, width, height);
  
  // Add QR code
  generator.qrCode('https://example.com/receipt');
  
  // Hardware controls
  generator.openDrawer();
  generator.beep();
  generator.cut();
  
  // Print the generated bytes
  await WindowsPrinter.printRawData(
    data: Uint8List.fromList(generator.getBytes()),
    useRawDatatype: true,
  );
}

Important Notes for Thermal Printers: #

Always use useRawDatatype: true for thermal printers:

// Correct - Direct printer control:
await WindowsPrinter.printRawData(
  data: escPosData,
  useRawDatatype: true,  // RAW mode - data sent directly to printer
);

// Problematic - Windows processes the data:
await WindowsPrinter.printRawData(
  data: escPosData,
  useRawDatatype: false, // TEXT mode - Windows handles formatting
);

Why RAW mode matters:

  • RAW Mode (true): Data is sent directly to the printer without Windows processing. The printer interprets ESC/POS commands directly, giving you full programmatic control.
  • TEXT Mode (false): Windows processes the data as a text document, which can result in formatting issues and blank pages for thermal printers.

Emoji Limitations: Thermal printers use ASCII/Extended ASCII character sets, not Unicode. Emojis will not display correctly - use ASCII alternatives instead.

Using External ESC/POS Package: #

You can also use the popular esc_pos_utils package:

import 'package:esc_pos_utils/esc_pos_utils.dart';

Future<void> printWithEscPosUtils() async {
  final profile = await CapabilityProfile.load();
  final generator = Generator(PaperSize.mm80, profile);
  List<int> bytes = [];

  bytes += generator.text('Hello World');
  bytes += generator.cut();

  await WindowsPrinter.printRawData(
    data: Uint8List.fromList(bytes),
    useRawDatatype: true,
  );
}

Regular Printer Usage #

Rich Text Documents: #

await WindowsPrinter.printRichTextDocument(
  printerName: 'HP LaserJet',
  content: '''##INVOICE##
  
**Company Name**
123 Business Street

**Bill To:**
Customer Name

**Items:**
- **Coffee** - \$3.50
- *Special Sandwich* - \$7.25

**Total: \$10.75**

*Thank you for your business!*
  ''',
  fontName: 'Arial',
  fontSize: 12,
);

PDF Printing: #

// Load PDF file
final file = File('invoice.pdf');
final pdfData = await file.readAsBytes();

await WindowsPrinter.printPdf(
  printerName: 'Office Printer',
  data: pdfData,
  copies: 2,
);

Complete API Reference #

Core Methods #

1. Get Available Printers

List<String> printers = await WindowsPrinter.getAvailablePrinters();

2. Get Printer Properties

Map<String, dynamic> properties = await WindowsPrinter.getPrinterProperties("Printer Name");

3. Get Paper Size Details

Map<String, dynamic> paperDetails = await WindowsPrinter.getPaperSizeDetails("Printer Name");

4. Print Raw Data

await WindowsPrinter.printRawData(
  printerName: "Printer Name", // Optional, uses default if null
  data: Uint8List.fromList([...]), // ESC/POS commands or raw data
  useRawDatatype: true, // true=RAW mode, false=TEXT mode
);

5. Print Rich Text Document

await WindowsPrinter.printRichTextDocument(
  printerName: "Printer Name",
  content: "**Bold** and *italic* text with ##headers##",
  fontName: "Arial", // Optional, default: "Courier New"
  fontSize: 12, // Optional, default: 12
);

6. Print PDF Documents

await WindowsPrinter.printPdf(
  printerName: "Printer Name", // Optional, uses default if null
  data: pdfBytes,
  copies: 1, // Optional, default: 1
);

7. Set Default Printer

bool success = await WindowsPrinter.setDefaultPrinter("Printer Name");

8. Open Printer Properties Dialog

bool success = await WindowsPrinter.openPrinterProperties("Printer Name");

Printer Type Guide #

Printer Type Recommended Method Use Case Important Notes
Thermal/Receipt printRawData() Receipts, labels, POS Must use useRawDatatype: true
Regular Office printRichTextDocument() Documents, reports Windows handles formatting
Any Printer printPdf() Universal documents Works with any printer type

ESC/POS Utilities #

Paper Sizes #

WPPaperSize.mm58  // 58mm thermal paper
WPPaperSize.mm80  // 80mm thermal paper

Text Styling #

const WPTextStyle(
  bold: true,
  italic: true,
  underline: true,
  align: WPTextAlign.center,
  size: WPTextSize.doubleHeightWidth,
  invert: true,
)

Barcode Types #

WPBarcodeType.upca   // 0
WPBarcodeType.upce   // 1
WPBarcodeType.ean13  // 2
WPBarcodeType.ean8   // 3
WPBarcodeType.code39 // 4
WPBarcodeType.itf    // 5
WPBarcodeType.codabar// 6
WPBarcodeType.code93 // 72
WPBarcodeType.code128// 73

Image Printing #

generator.image(imageData, width, height);

Common Issues & Solutions #

Thermal Printer Issues #

// Correct for thermal printers:
await WindowsPrinter.printRawData(
  data: escPosData,
  useRawDatatype: true, // ESSENTIAL - sends data directly to printer
);

// Causes formatting issues:
await WindowsPrinter.printRawData(useRawDatatype: false);

Examples #

Check the /example folder for complete working examples:

  • Basic printer operations
  • Thermal receipt printing
  • Regular document printing
  • ESC/POS usage

License #

This project is licensed under the MIT License - see the LICENSE file for details.

4
likes
150
points
98
downloads

Publisher

verified publishermanasbasnet.com

Weekly Downloads

A Flutter plugin for managing and interacting with printers on Windows platforms.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on windows_printer