ncssar / radiolog

SAR radio log program
Other
13 stars 3 forks source link

pywintypes error 31 (ShellExecute error) when printing #607

Closed caver456 closed 1 year ago

caver456 commented 1 year ago

From the transcript:

183754:PRINT radio log
183754:teamFilterList=['']
183754:generating radio log pdf: C:\Users\SAR 425\Documents\RadioLog Backups\Testing_2022_12_11_183332\Testing_2022_12_11_183332_OP1.pdf
183754:length:8
183754:valid logo file C:\Users\SAR 425\RadioLog\.config\radiolog_logo.jpg
183754:Page number:1
183754:Height:43.199999999999996
183754:Pagesize:(792.0, 612.0)
183755:done drawing printLogHeaderFooter canvas
183755:end of printLogHeaderFooter
Uncaught exception
Traceback (most recent call last):
  File "radiolog.py", line 5084, in accept
  File "radiolog.py", line 2611, in printLog
pywintypes.error: (31, 'ShellExecute', 'A device attached to the system is not functioning.')
183819:PRINT radio log
183819:teamFilterList=['']
183819:generating radio log pdf: C:\Users\SAR 425\Documents\RadioLog Backups\Testing_2022_12_11_183332\Testing_2022_12_11_183332_OP1.pdf
183819:length:8
183819:valid logo file C:\Users\SAR 425\RadioLog\.config\radiolog_logo.jpg
183819:Page number:1
183819:Height:43.199999999999996
183819:Pagesize:(792.0, 612.0)
183819:done drawing printLogHeaderFooter canvas
183819:end of printLogHeaderFooter
Uncaught exception
Traceback (most recent call last):
  File "radiolog.py", line 5084, in accept
  File "radiolog.py", line 2611, in printLog
pywintypes.error: (31, 'ShellExecute', 'A device attached to the system is not functioning.')
183822:PRINT team radio logs
183822:teamFilterList=['TeamAlpha', 'TeamBravo']
183822:generating radio log pdf: C:\Users\SAR 425\Documents\RadioLog Backups\Testing_2022_12_11_183332\Testing_2022_12_11_183332_teams_OP1.pdf
183822:length:6
183822:length:3
183822:valid logo file C:\Users\SAR 425\RadioLog\.config\radiolog_logo.jpg
183822:Page number:1
183822:Height:43.199999999999996
183822:Pagesize:(792.0, 612.0)
183822:done drawing printLogHeaderFooter canvas
183822:end of printLogHeaderFooter
Uncaught exception
Traceback (most recent call last):
  File "radiolog.py", line 5087, in accept
  File "radiolog.py", line 2619, in printTeamLogs
  File "radiolog.py", line 2611, in printLog
pywintypes.error: (31, 'ShellExecute', 'A device attached to the system is not functioning.')
183824:PRINT clue log
183824:appending: ['', 'Radio Log Begins: Sun Dec 11, 2022', '', '1833', '', '', '', '', '']
183824:Nothing to print for specified operational period 1

This was reported by @RadiosPRN.

A quick google of that error shows that a few folks determined that the problem was that no pdf reader application was installed, and/or it was not set as the default application for opening pdf files: https://stackoverflow.com/questions/36022695

Sure enough, if I uninstall Acrobat Reader, then printing from radiolog shows this:

175633:PRINT radio log
175633:teamFilterList=['']
175633:generating radio log pdf: C:\Users\caver\RadioLog\New_Incident_2022_12_11_161621\New_Incident_2022_12_11_161621_OP1.pdf
175633:length:8
175633:valid logo file C:\Users\caver\RadioLog\.config\radiolog_logo.jpg
175633:Page number:1
175633:Height:43.199999999999996
175633:Pagesize:(792.0, 612.0)
175633:done drawing printLogHeaderFooter canvas
175633:end of printLogHeaderFooter
Traceback (most recent call last):
  File "C:\Users\caver\Documents\GitHub\radiolog\radiolog.py", line 5086, in accept
    self.parent.printLog(opPeriod)
  File "C:\Users\caver\Documents\GitHub\radiolog\radiolog.py", line 2613, in printLog
    win32api.ShellExecute(0,"print",pdfName,'/d:"%s"' % win32print.GetDefaultPrinter(),".",0)
pywintypes.error: (31, 'ShellExecute', 'A device attached to the system is not functioning.')

The error syntax is a bit different - not sure why - but the underlying error 31 seems to be the same.

This does match the behavior of what @RadiosPRN reported - you can only save one pdf file at a time, because the print failure for any given doc causes the subsequent pdf saves to be skipped.

So this issue could have two fixes: 1) find a way to print without needing a pdf reader to be installed (and set as the default application for pdfs) 2) if a print fails, continue with the save of the other requested pdfs

caver456 commented 1 year ago

This stackoverflow thread looks helpful too: https://stackoverflow.com/questions/27195594 - it might be easier to embed gsprint than to embed Acrobat Reader, and the thread implies that you can specify the path to the viewer, which should get rid of the need to have a system default viewer.

caver456 commented 1 year ago

The plan:

caver456 commented 1 year ago

@RadiosPRN confirms: installing Acrobat Reader allowed all pdf files to be saved.

caver456 commented 1 year ago

Looks like there are a few choices on how to do the installation-time check. It would be nice to see if there is a default pdf viewer application already installed, but that seems like it might not be that robust, and requires running something with elevated permissions - shouldn't technically be a problem since the installer is running with elevated permissions anyway so as to do the all-users install (which triggers the UAC), but it would be nice to not run any more programs than required:

https://learn.microsoft.com/en-us/answers/questions/28817/registry-location-for-file-extension-associations.html

Instead, we could bypass the default app check, and just check the registry for Acrobat or Acrobat Reader, and offer to install it if not already installed. This offer should also show the explanation of why Acrobat Reader (or, a default pdf viewer application other than a web browser) is needed.

caver456 commented 1 year ago

If Acrobat Reader DC is installed, this registry entry will exist: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Acrobat.exe (or maybe AcroRd32.exe according to an old post).

If this registry entry is not found, show the message (and offer to install if possible).

caver456 commented 1 year ago

Looks like there are several ways to show the info in Inno Setup:

The last option will probably look and feel the best, and shouldn't take too much coding. Can include an image or two - a big PDF icon and a big printer icon should grab the user's attention. https://stackoverflow.com/questions/43696537

caver456 commented 1 year ago

Went with the middle option (MsgBox) instead, since it is a MUCH lighter weight change, and still grabs attention possibly better than a custom page.