Skip to content

wysaid/print-gateway

Repository files navigation

PrintGateway

.NET License Windows

中文文档

PrintGateway is a .NET print gateway service for Windows that enables document printing via REST API. Supports PDF, images, Office documents, and more, with print queue management, status tracking, and idempotency guarantees.

Features

Core Features

  • 📄 Multi-format Support: PDF, Images (JPG/PNG/BMP/TIFF/GIF/WebP), Office documents (DOC/XLS/PPT)
  • 🖨️ Printer Discovery: Auto-enumerates system printers with default printer selection
  • 📊 Print Queue: Job status tracking (Pending → Printing → Completed/Failed)
  • 💾 Persistence: SQLite-based job storage with recovery after service restart
  • 🔐 Security: Optional API Token authentication with tray-managed security status
  • 🔄 Idempotency: Idempotency key support to prevent duplicate printing
  • 🖥️ Tray Icon: System tray monitoring with one-click service control
  • 🔧 Windows Service: Installable as Windows Service with auto-start on boot
  • 🌐 Web UI: Local web interface for file upload, print settings, and queue management

Print Options

  • Page Orientation: Auto/Portrait/Landscape
  • Color Mode: Auto/Color/Grayscale
  • Paper Size: Auto/A4/Letter/Legal

Quick Start

Prerequisites

  • .NET 10 SDK
  • Windows 10/11
  • LibreOffice or MS Office (optional, for Office document conversion)

1. Build and Run (Recommended)

# Build a local release folder and start the tray app
.\scripts\publish-release.ps1
.\Release\PrintGatewayTray\PrintGatewayTray.exe

After startup:

  • 🖥️ Access Web UI: Double-click tray icon or open browser to http://localhost:7000
  • 📱 LAN Access: Enable Allow LAN Access from the tray menu, then restart the app before using http://<your-ip>:7000
  • 🔐 Authentication: Enable API Token from the tray menu if remote or protected access is needed
  • 🛑 Stop Service: Right-click tray icon → Exit (automatically stops the service)

2. Development Mode

dotnet run --project src/PrintGatewayTray

API Endpoints

Health Check

GET /health
→ 200 { "status": "ok", "uptime": "...", "security": { "authenticationEnabled": false }, "network": { "allowLanAccess": false } }

List Printers

GET /api/printers
→ 200 { "printers": [{ "name": "...", "isDefault": true, "isAvailable": true }], "showVirtual": false }

Submit Print Job

POST /api/jobs
Content-Type: multipart/form-data

Parameters:
- file: File (required)
- printerName: Printer name (optional)
- copies: Number of copies (optional, default 1)
- pageOrientation: Orientation (optional: Auto/Portrait/Landscape)
- colorMode: Color mode (optional: Auto/Color/Grayscale)
- paperSize: Paper size (optional: Auto/A4/Letter/Legal)
- idempotencyKey: Idempotency key (optional)

→ 202 { "jobId": "...", "status": "Pending", ... }

Query Job Status

GET /api/jobs/{id}
→ 200 { "jobId": "...", "status": "Completed", ... }

Cancel Job

DELETE /api/jobs/{id}
→ 200 { "jobId": "...", "status": "Cancelled" }

Web UI Guide

Access

  1. Local Access: Double-click system tray icon or open browser to http://localhost:7000
  2. LAN Access: Access http://<your-ip>:7000 from other devices

LAN access is disabled by default. Enable it from the tray menu and restart the application before testing from another device.

Features

  • 📤 Upload Files: Drag and drop files to upload area, or click to select
    • Supported formats: PDF, JPG, PNG, BMP, TIFF, GIF, WebP, DOC, DOCX, PPT, PPTX, XLS, XLSX
    • Supports multi-file batch upload
  • ⚙️ Print Settings:
    • Printer selection
    • Number of copies (1-99)
    • Page orientation (Auto/Portrait/Landscape)
    • Color mode (Auto/Color/Grayscale)
    • Paper size (Auto/A4/Letter/Legal)
    • Virtual printer visibility (configured via ShowVirtualPrinters in appsettings)
  • 📋 Print Queue: Real-time display of print job status with cancel support
    • Pending (Yellow)
    • Printing (Blue)
    • Completed (Green)
    • Failed (Red)

Security and Firewall Configuration

  • API authentication is optional. When enabled, send X-API-Token or Authorization: Bearer <token> with all /api/* requests.
  • LAN access is disabled by default. After enabling it in the tray menu, open port 7000 in Windows Firewall if remote devices must connect:
# Run as Administrator
New-NetFirewallRule -DisplayName "PrintGateway" -Direction Inbound -LocalPort 7000 -Protocol TCP -Action Allow

AI Integration (OpenClaw Skill)

PrintGateway provides an MCP (Model Context Protocol) server that allows AI assistants to directly use printing capabilities:

cd skills/print-gateway-skill
pip install -r requirements.txt
python mcp_server.py

Add the MCP server to your openclaw configuration to use the print tool.

See: skills/print-gateway-skill/README.md

Testing

Unit Tests

dotnet test

Acceptance Tests

# v1 Test (PDF printing)
.\scripts\openclaw-print-test.ps1

# v2 Test (Multi-format + print options)
.\scripts\openclaw-v2-print-test.ps1

Configuration

Edit src/PrintGateway/appsettings.json:

{
  "PrintGateway": {
    "DefaultPrinter": "Your Printer Name",
    "MaxFileSizeMB": 50,
    "JobRetentionHours": 168,
    "ShowVirtualPrinters": false
  },
  "Network": {
    "AllowLanAccess": false
  },
  "Security": {
    "ApiToken": ""
  },
  "ConnectionStrings": {
    "JobsDb": "Data Source=data/jobs.db"
  }
}

Project Structure

print_service/
├── src/
│   ├── PrintGateway/          # Core service
│   │   ├── Models/            # Data models
│   │   ├── Services/          # Business logic
│   │   │   └── Converters/    # Document converters
│   │   ├── Middleware/        # Middleware
│   │   ├── Endpoints/         # API endpoints
│   │   └── wwwroot/           # Web UI static files
│   │       ├── index.html     # Main page
│   │       ├── css/style.css  # Styles
│   │       └── js/app.js      # Frontend logic
│   │       └── js/i18n.js     # Internationalization
│   └── PrintGatewayTray/      # Tray application
├── tests/
│   └── PrintGateway.Tests/    # Unit tests
├── scripts/                   # PowerShell scripts
├── skills/                    # OpenClaw Skill
│   └── print-gateway-skill/
├── testdata/                  # Test data
├── data/                      # Runtime data (jobs.db, logs)
├── PrintGatewayManager.ps1    # Management script
└── PrintGateway.sln           # Solution file

Supported Document Formats

Type Extensions Notes
PDF .pdf Native support
Images .jpg, .jpeg, .png, .bmp, .tiff, .tif, .gif, .webp Converted to PDF for printing
Office .doc, .docx, .ppt, .pptx, .xls, .xlsx Requires LibreOffice

Troubleshooting

Service Won't Start

# Check port usage
netstat -ano | findstr :7000

# View logs
Get-Content src\PrintGateway\logs\printgateway-*.log -Tail 50

Office Documents Won't Print

Ensure LibreOffice is installed and added to system PATH:

# Check LibreOffice
where soffice

Tray Icon Not Showing

# Restart tray application
.\PrintGatewayManager.ps1 -Action restart

Printer Shows as Offline

  • Check printer physical connection and power
  • Verify printer is set as online in Windows Devices and Printers
  • Jobs will queue automatically and print when printer comes back online

Contributing

Issues and Pull Requests are welcome!

License

MIT License - See LICENSE for details.

Acknowledgments

Thanks to all contributors!

About

A lightweight service designed to facilitate OpenClaw's control of Windows printers, with several built-in skills for easy OpenClaw integration.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors