updated print module

This commit is contained in:
2025-09-22 21:53:08 +03:00
parent 921ea43834
commit b49a22832d
4 changed files with 223 additions and 25 deletions

View File

@@ -96,6 +96,51 @@ function Invoke-PrintPDF {
}
}
# Print local PDF file function
function Invoke-PrintLocalPDF {
param(
[string]$PdfPath,
[string]$PrinterName = "default",
[int]$Copies = 1
)
try {
Write-ServiceLog "Local print request: File=$PdfPath, Printer=$PrinterName, Copies=$Copies"
# Check if file exists
if (!(Test-Path $PdfPath)) {
throw "PDF file not found: $PdfPath"
}
# Get default printer if needed
if ($PrinterName -eq "default" -or [string]::IsNullOrEmpty($PrinterName)) {
$defaultPrinter = Get-WmiObject -Class Win32_Printer | Where-Object { $_.Default -eq $true }
$PrinterName = $defaultPrinter.Name
}
Write-ServiceLog "Using printer: $PrinterName"
# Print using Windows shell
$printJob = Start-Process -FilePath $PdfPath -Verb Print -PassThru -WindowStyle Hidden
Start-Sleep -Seconds 2
Write-ServiceLog "Print job sent successfully to printer: $PrinterName"
return @{
success = $true
message = "Print job sent successfully"
printer = $PrinterName
timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
}
}
catch {
Write-ServiceLog "Print error: $($_.Exception.Message)"
return @{
success = $false
error = $_.Exception.Message
}
}
}
# HTTP Response function
function Send-HttpResponse {
param(
@@ -179,7 +224,7 @@ function Start-PrintService {
Send-HttpResponse -Context $context -Body ($printersData | ConvertTo-Json -Depth 3)
}
"^/print/(pdf|silent)$" {
"^/print/(pdf|silent|file)$" {
if ($method -eq "POST") {
try {
# Read request body
@@ -190,8 +235,16 @@ function Start-PrintService {
# Parse JSON
$printData = $body | ConvertFrom-Json
# Print PDF
$result = Invoke-PrintPDF -PdfUrl $printData.pdf_url -PrinterName $printData.printer_name -Copies $printData.copies
# Print PDF - handle both URL and local file path
if ($printData.pdf_path -and (Test-Path $printData.pdf_path)) {
Write-ServiceLog "Using local PDF file: $($printData.pdf_path)"
$result = Invoke-PrintLocalPDF -PdfPath $printData.pdf_path -PrinterName $printData.printer_name -Copies $printData.copies
} elseif ($printData.pdf_url) {
Write-ServiceLog "Using PDF URL: $($printData.pdf_url)"
$result = Invoke-PrintPDF -PdfUrl $printData.pdf_url -PrinterName $printData.printer_name -Copies $printData.copies
} else {
throw "Either pdf_path or pdf_url must be provided"
}
if ($result.success) {
Send-HttpResponse -Context $context -Body ($result | ConvertTo-Json)
@@ -219,7 +272,7 @@ function Start-PrintService {
$errorResponse = @{
success = $false
error = "Endpoint not found"
available_endpoints = @("/health", "/printers", "/print/pdf", "/print/silent")
available_endpoints = @("/health", "/printers", "/print/pdf", "/print/silent", "/print/file")
}
Send-HttpResponse -Context $context -StatusCode 404 -Body ($errorResponse | ConvertTo-Json)
}