Files
CGW-Printing/README.md
T

284 lines
7.9 KiB
Markdown

# LAAPC Print Service
A Windows service for managing print jobs from Harbor/Clipper legacy applications to modern laser printers with multi-tray support.
## Overview
This service solves the problem of migrating from dot-matrix printers with carbon copy paper to laser printers with colored paper trays. It:
-**Prevents file overwrites** by immediately moving capture files to a queue with GUID-based names
-**Maintains print order** with a persistent queue system
-**Controls printer trays** via Windows print queue API for "carbon copy" simulation
-**Transforms content** with configurable rules per document type
-**Archives prints** for record-keeping
## Architecture
- **PrintService**: Windows service that runs continuously, monitors for print jobs, and processes them
- **PrintServiceCLI**: Command-line tool for Harbor/Clipper to submit print jobs via IPC (named pipes)
## Quick Start
### 1. Configure
Edit `PrintService/appsettings.json`:
```json
{
"AppSettings": {
"CapturesPath": "C:\\Users\\Work\\Desktop\\LAAPC\\Captures",
"DocumentTypes": [
{
"Name": "invoice",
"PrinterName": "Your_Actual_Printer_Name",
"TraySequence": [ 3, 4, 1, 2 ]
}
]
}
}
```
**Important**: Replace `Your_Actual_Printer_Name` with actual Windows printer name(s).
### 2. Build
```powershell
# Build for 32-bit (supports both 32-bit and 64-bit Windows)
dotnet build -c Release -r win-x86
# Or publish self-contained (includes .NET runtime)
dotnet publish -c Release -r win-x86 --self-contained true -o publish/x86
```
### 3. Install Service
```powershell
# Run as Administrator
sc.exe create "LAAPC Print Service" binPath="C:\Path\To\PrintService.exe" start=auto
sc.exe start "LAAPC Print Service"
```
### 4. Test CLI
```powershell
PrintServiceCLI.exe -f "C:\Captures\test.txt" -t invoice -o 12345
```
## Usage
### From Harbor/Clipper
Replace your current Rust CLI calls with:
```
PrintServiceCLI.exe -f <filepath> -t <doctype> [-o <ordernumber>]
```
**Examples**:
```
PrintServiceCLI.exe -f "C:\Captures\inv001.txt" -t invoice
PrintServiceCLI.exe -f "C:\Captures\ord002.txt" -t order -o 600005
PrintServiceCLI.exe -f "C:\Captures\del003.txt" -t delivery
```
### CLI Options
- `-f, --file <path>`: Path to capture file (required)
- `-t, --type <type>`: Document type: invoice, order, delivery, etc. (required)
- `-o, --order <num>`: Optional order number
- `-h, --help`: Show help
## Configuration
### Document Types
Each document type in `appsettings.json` specifies:
```json
{
"Name": "invoice", // Document type identifier
"PrinterName": "HP LaserJet 500", // Windows printer name
"TraySequence": [ 3, 4, 1, 2 ], // Tray order (simulates carbon copy)
"FontName": "Courier New", // Font for rendering
"FontSize": 10.0, // Font size in points
"VerticalOffset": 0, // Adjust vertical positioning (pixels)
"HorizontalOffset": 0, // Adjust horizontal positioning (pixels)
"ArchiveAfterPrint": true, // Archive or delete after printing
"ArchivePath": "C:\\Archive\\Invoices", // Where to archive
"Transformations": [ // Text transformation rules
{
"Pattern": "W1DUPLICATE INVW0", // Regex pattern to find
"Replacement": "", // Replace with (empty = remove)
"Description": "Remove duplicate marker"
}
]
}
```
### Tray Mapping
The `TraySequence` specifies which physical printer trays to use for each page/copy:
- **Example**: `[3, 4, 1, 2]` prints:
- Page 1 → Tray 3 (e.g., Pink paper)
- Page 2 → Tray 4 (e.g., Orange paper)
- Page 3 → Tray 1 (e.g., Blue paper)
- Page 4 → Tray 2 (e.g., Green paper)
**Note**: Tray numbers may need adjustment per printer model. Use the included tray discovery tool (see Troubleshooting).
## Project Structure
```
PrintService/
├── Models/
│ ├── PrintJob.cs - Print job data model
│ ├── DocumentConfig.cs - Document type configuration
│ └── AppSettings.cs - Application settings
├── Services/
│ ├── PrintQueueService.cs - Job queue management
│ ├── FileMonitorService.cs - File system monitoring
│ ├── PrinterService.cs - Windows printer integration
│ ├── DocumentProcessor.cs - Content transformation
│ └── IpcService.cs - Named pipe IPC server
├── Worker.cs - Main service coordinator
├── Program.cs - Service host configuration
└── appsettings.json - Configuration file
PrintServiceCLI/
└── Program.cs - Command-line interface
```
## Troubleshooting
### Service won't start
1. Check Windows Event Viewer → Application logs
2. Verify paths in `appsettings.json` exist
3. Run with elevated privileges
### Printer not found
List installed printers:
```powershell
Get-Printer | Select-Object Name
```
Update `PrinterName` in config to match exactly.
### Wrong trays selected
Tray mapping varies by printer model. To discover available trays, temporarily add logging to `PrinterService.ListPaperSources()` and check service logs.
### Files being overwritten
Ensure Harbor/Clipper is calling the CLI (not writing directly to Captures folder). The service immediately moves files to prevent timestamp collisions.
### Print order issues
Check `queue_state.json` - jobs are processed FIFO. If order is wrong, check file timestamps.
## Monitoring
### Service Status
```powershell
Get-Service "LAAPC Print Service"
sc.exe query "LAAPC Print Service"
```
### Logs
Check Windows Event Viewer or configure file logging in `appsettings.json`.
### Queue State
Inspect `queue_state.json` (location specified in `appsettings.json`) to see pending jobs.
### Folders
- `Queue/` - Files being processed (GUID-named)
- `Archive/` - Completed jobs (if archiving enabled)
- `Errors/` - Failed jobs after max retries
## Uninstall
```powershell
# Run as Administrator
sc.exe stop "LAAPC Print Service"
sc.exe delete "LAAPC Print Service"
```
## Development
### Build for debugging
```powershell
dotnet build -c Debug
```
### Run service locally (not as Windows Service)
```powershell
dotnet run --project PrintService/PrintService.csproj
```
### Watch mode (auto-rebuild on changes)
```powershell
dotnet watch run --project PrintService/PrintService.csproj
```
### VS Code Tasks
Use Command Palette (`Ctrl+Shift+P`) → "Tasks: Run Task":
- `build-all-x86` - Build release for 32-bit
- `publish-all-x86` - Create deployment package
- `run-service` - Run service locally for testing
## Technical Details
### IPC Protocol
The CLI communicates with the service via Windows Named Pipes (`\\.\pipe\PrintServicePipe`):
**Request**:
```json
{
"FilePath": "C:\\Captures\\file.txt",
"DocumentType": "invoice",
"OrderNumber": "12345"
}
```
**Response**:
```json
{
"Success": true,
"Message": "Job {guid} queued successfully"
}
```
### Print Queue Integration
Uses `System.Drawing.Printing.PrintDocument` with per-page `PageSettings.PaperSource` control:
- Content rendered with `Graphics.DrawString()` for pixel-perfect positioning
- Jobs go through Windows print queue (visible in Windows printer UI)
- Survives service restarts via persistent queue state
### File Safety
1. CLI sends command with file path
2. Service immediately moves file to Queue folder with GUID name
3. Original filename preserved in job metadata
4. Prevents timestamp collision overwrites
## License
[Your License Here]
## Support
For issues or questions, contact [Your Contact Info]