talesluna / rust-printers

A rust library to printing APIs implementation for unix (cups) and windows (winspool).
https://crates.io/crates/printers
MIT License
45 stars 23 forks source link

[bug] win10,win11 cannot print #9

Closed cheng990629 closed 1 year ago

cheng990629 commented 1 year ago

打印服务器没有接受请求。作业已中止

image image
jonahgeorge commented 1 year ago

I've run into this as well- I made further progress by ensuring that both "LPD Print Service" and "LPR Port Monitor" features are enabled.

CleanShot 2022-12-31 at 15 45 14@2x

However, it appears to still have issues:

> lpr -S 127.0.0.1 -P "HP" "C:\\Users\\jonah\\AppData\\Local\\Temp\\8ea19b4b-22df-4063-ab20-1c56ec8abb81"
Error: print server did not accept request.  Job aborted.

Initially, it seems that "HP" is too short of a system name. Was able to determine that the DriverName parsing from wmic has a bug when names have whitespace:

     Running `target\debug\examples\list_printers.exe`       
Printer { id: "793c6ff0-8bb6-542a-baba-e82974ceac12", name: "Name", system_name: "DriverName" }
Printer { id: "ed0e594f-e80c-59ca-9a76-d973e6d7a586", name: "Software", system_name: "Microsoft" }
Printer { id: "9928f21e-8775-5504-a46b-4066026b9b4c", name: "LaserJet", system_name: "HP" }       
Printer { id: "ed0e594f-e80c-59ca-9a76-d973e6d7a586", name: "XPS", system_name: "Microsoft" }     
Printer { id: "ed0e594f-e80c-59ca-9a76-d973e6d7a586", name: "Print", system_name: "Microsoft" }   
Printer { id: "ed0e594f-e80c-59ca-9a76-d973e6d7a586", name: "Shared", system_name: "Microsoft" }  
Printer { id: "8aec0343-6abb-537b-b8cd-e05e22e9d0b4", name: "CL-S400DTZ", system_name: "Citizen" }
> wmic printer get DriverName,Name
DriverName                         Name
Microsoft Software Printer Driver  OneNote for Windows 10
HP LaserJet M109-M112 PCLm-S       NPI642118 (HP LaserJet M110w)
Microsoft XPS Document Writer v4   Microsoft XPS Document Writer
Microsoft Print To PDF             Microsoft Print to PDF
Microsoft Shared Fax Driver        Fax
Citizen CL-S400DTZ                 Citizen CL-S400DTZ

Note: HP LaserJet M109-M112 PCLm-S vs HP

Interestingly, neither of these work:

> lpr -S 127.0.0.1 -P "NPI642118 (HP LaserJet M110w)" "C:\\Users\\jonah\\AppData\\Local\\Temp\\8ea19b4b-22df-4063-ab20-1c56ec8abb81"
Error: print server did not accept request.  Job aborted.
> lpr -S 127.0.0.1 -P "HP LaserJet M109-M112 PCLm-S" "C:\\Users\\jonah\\AppData\\Local\\Temp\\8ea19b4b-22df-4063-ab20-1c56ec8abb81"
Error: print server did not accept request.  Job aborted.

Taking a step back, tried to determine whether this is an issue with this library or my hardware configuration. Looked into alternative ways of printing via CLI on Windows and encountered Get-Printer and Out-Printer.

PS C:\Users\jonah> Get-Printer | Format-Table

Name                           ComputerName    Type         DriverName                PortName        Shared   Published  DeviceType
----                           ------------    ----         ----------                --------        ------   ---------  ----------
OneNote for Windows 10                         Local        Microsoft Software Pri... Microsoft.Of... False    False      Print
NPI642118 (HP LaserJet M110w)                  Local        HP LaserJet M109-M112 ... WSD-9a219156... False    False      Print
Microsoft XPS Document Writer                  Local        Microsoft XPS Document... PORTPROMPT:     False    False      Print
Microsoft Print to PDF                         Local        Microsoft Print To PDF    PORTPROMPT:     False    False      Print
Fax                                            Local        Microsoft Shared Fax D... SHRFAX:         False    False      Print
Citizen CL-S400DTZ                             Local        Citizen CL-S400DTZ        USB001          False    False      Print
PS C:\Users\jonah> Get-Content -Path "C:\\Users\\jonah\\AppData\\Local\\Temp\\8ea19b4b-22df-4063-ab20-1c56ec8abb81" |  Out-Printer -Name "NPI642118 (HP LaserJet M110w)"

The following working on both of my test devices.


@talesluna Would you accept patch which switches the Windows portion to these PowerShell commands or are there intricacies of LPR that I'm not aware of that this solution would omit?

jonahgeorge commented 1 year ago

@cheng-io This branch has the changes above applied and I'm now able to print on Windows (it does change system_name in the returned list of printers due to what Out-Printer requires): https://github.com/jonahgeorge/rust-printers/tree/jonahgeorge-patch-1

Let me know if you have success with it

jonahgeorge commented 1 year ago

Upon further review, I believe the correct way to accomplish this is with the winspool functions within winapi: https://github.com/retep998/winapi-rs/blob/0.3/src/um/winspool.rs

talesluna commented 1 year ago

Thanks for contributing @cheng-io

This issue are fixed by PR #13 (tag v1.2.0)

Captura de Tela 2023-02-09 às 02 34 22
talesluna commented 1 year ago

@jonahgeorge Thanks for your proposal with powershell. The latest version has been adjusted based on your fork 👍 Feel welcome to open the PR. I opened this project as a simple utility and I can't always handle it quickly.