Ghostscript 9.55至10.05支票打印脚本迁移故障求助
问题:Ghostscript 10.05迁移后支票打印脚本报错无法打印
我有一个多年前编写的用于打印支票的PowerShell脚本,正尝试将其从Ghostscript 9.55版本迁移至10.05版本。脚本核心功能为打印支票,原代码关键片段如下:
$AccountingScalingArgs = "<< /BeginPage{0.985 0.985 scale 4.59 5.94 translate} >> setpagedevice" # 原打印调用示例(会计支票部分) $res = & $PDFParser -sPAPERSIZE=letter -dNORANGEPAGESIZE -dBATCH -dNOPAUSE -c "mark /Duplex false /OutputFile (%printer%$($Printer)) /UserSettings << /DocumentName ($($FilePath.Replace('\','\\'))) >> (mswinpr2) finddevice putdeviceprops setdevice $($AccountingScalingArgs)" -f $FilePath
迁移后,使用旧的finddevice、putdeviceprops和setdevice函数时,Ghostscript频繁返回错误:
/typecheck in --setpagedevice--
尝试修改脚本后不再报错,但完全无法打印支票,修改后的打印调用示例:
# 修改后的会计支票打印调用 $res = & $PDFParser -sPAPERSIZE=letter -dNORANGEPAGESIZE -dBATCH -dNOPAUSE -c "mark /Duplex false /OutputFile (%printer%$($Printer)) /UserSettings << /DocumentName ($($FilePath.Replace('\','\\'))) >> (mswinpr2) $($AccountingScalingArgs)" -f $FilePath
解决方案
Ghostscript 10.x版本对设备配置的语法做了调整,旧的finddevice/putdeviceprops/setdevice流程已被弃用,需要改用直接通过setpagedevice传递设备参数的方式,同时修正设备选择和参数传递的逻辑:
关键修复点
- 移除旧设备配置函数:删除
finddevice、putdeviceprops、setdevice这些废弃函数 - 正确构造设备参数字典:将设备名称(
mswinpr2)、输出路径、双面打印、用户设置等参数整合到同一个字典中,传递给setpagedevice - 调整缩放参数的位置:确保
AccountingScalingArgs的BeginPage字典正确合并到设备配置中
修复后的完整函数代码
$AccountingScalingArgs = "<< /BeginPage{0.985 0.985 scale 4.59 5.94 translate} >>" function DoCheckPrint { param( [string]$CheckQueue, [array]$Files, [string]$Printer) $PrintedFiles = @() if($CheckQueue -eq "Accounting"){ # ACCOUNTING CHECKS foreach($File in $Files){ Write-Log (" Printing $($File.Pages) check(s): $($File)") try { # Convert PCL to pdf then print. $FilePath = $File.FullName.Substring(0,$File.FullName.IndexOf('.pcl'))+'.pdf' $res = & $PCLParser -o "$($FilePath)" -sDEVICE=pdfwrite -f "$($File)" if($LASTEXITCODE -ne 0){ Write-Log(' !! Error converting last accounting check, skipping !!') Write-log $res continue } } catch { Write-Log(" !! Error converting pcl check, skipping: $($_)") continue } try{ # 构造完整的设备配置字典,合并缩放参数 $deviceConfig = "<< /Device (mswinpr2) /Duplex false /OutputFile (%printer%$($Printer)) /UserSettings << /DocumentName ($($FilePath.Replace('\','\\'))) >> $($AccountingScalingArgs) >> setpagedevice" $res = & $PDFParser -sPAPERSIZE=letter -dNORANGEPAGESIZE -dBATCH -dNOPAUSE -c "$deviceConfig" -f $FilePath if($LASTEXITCODE -ne 0){ Write-Log(' !! Error printing last accounting check !!') Write-log $res } else { $PrintedFiles += $File ## 删除临时PDF文件 try{ Write-log "Deleting tmp pcl=>pdf file: $($FilePath)" Remove-Item -Path $FilePath } catch { Write-Log("!! Error deleting file: $($_)") } } } catch { Write-Log(" !! Error printing check: $($_)") } } } else { # PAYROLL CHECKS foreach($File in $Files){ Write-Log (" Printing $($File.Pages) check(s): $($File)") $FilePath = $File.FullName $CheckFileNameStripped = $File.Name.Substring($PayrollChecksPrefix.Length) $RelatedSummaryFile = "$($SummaryPath)\$($PayrollSummaryPrefix)$($CheckFileNameStripped)" try { if(Test-Path -Path $RelatedSummaryFile){ Write-log (" Printing summary: $($RelatedSummaryFile)") # 工资单摘要打印的设备配置 $summaryDeviceConfig = "<< /Device (mswinpr2) /Duplex true /OutputFile (%printer%$($SummaryPrinter)) /UserSettings << /DocumentName ($($RelatedSummaryFile.Replace('\','\\'))) >> >> setpagedevice" $res = & $PDFParser -sPAPERSIZE=letter -dNORANGEPAGESIZE -dBATCH -dNOPAUSE -c "$summaryDeviceConfig" -f $RelatedSummaryFile if($LASTEXITCODE -ne 0){ Write-Log(' !! Error printing summary file !!') Write-log $res } else { $PrintedFiles += $RelatedSummaryFile } } else { Write-Log(" !! Couldn't find summary file: $($RelatedSummaryFile) !!") } } catch { Write-Log(" !! Error printing summary: $($_)") } try{ # 工资单支票打印的设备配置 $payrollDeviceConfig = "<< /Device (mswinpr2) /Duplex false /OutputFile (%printer%$($Printer)) /UserSettings << /DocumentName ($($FilePath.Replace('\','\\'))) >> >> setpagedevice" $res = & $PDFParser -sPAPERSIZE=letter -dNORANGEPAGESIZE -dBATCH -dNOPAUSE -c "$payrollDeviceConfig" -f $FilePath if($LASTEXITCODE -ne 0){ Write-Log(' !! Error printing last payroll check !!') Write-log $res } else { $PrintedFiles += $File } } catch { Write-Log(" !! Error printing check: $($_)") } } } return [array]$PrintedFiles }
修复说明
- 将
AccountingScalingArgs修改为仅包含BeginPage字典,不再包含setpagedevice,避免重复调用 - 为每个打印场景构造完整的设备配置字典,包含
/Device指定打印设备(mswinpr2)、/Duplex设置双面、/OutputFile指定打印机、/UserSettings传递文档名称 - 所有参数整合到同一个字典后调用
setpagedevice,符合Ghostscript 10.x的语法要求 - 移除了无效的
.setdevice等错误修改
内容的提问来源于stack exchange,提问作者Thomas Sirveaux
相关产品推荐
相关产品推荐

