服务内部不可用的文件类型的标准应用程序



我创建了一个服务,它使用套接字接收来自其他计算机的PDF文件,然后将其打印到连接的打印机上。此操作的代码是用Python编写的。

我通过手动运行Python脚本测试了这个应用程序,一切都如预期的那样工作。脚本创建一个套接字,接受PDF文件,将其推送到其队列,然后打印该文件。

我已经为这个脚本创建了一个Windows服务,使用NSSM -非吸吮服务管理器。

服务也可以很好地运行我的Python脚本,只是,当它试图打印到打印机时,我得到一个错误,没有与PDF文件相关的应用程序。这很奇怪,因为我确实有一个标准的PDF程序(adobeacrobatreader),它在手动运行脚本时确实有效。

Python脚本执行PowerShell命令设置默认打印机,然后使用Adobe打印文件(打印到默认打印机)。

下面是我脚本中负责打印的代码片段:

cmd_set_default_printer = "powershell.exe (New-Object -ComObject WScript.Network).SetDefaultPrinter('{0}')".format(printer_data['name'])
cmd_print_file = "powershell.exe Start-Process -FilePath '{0}' -Verb Print".format(item['file'])
cmd_close_acrobat = "powershell.exe Stop-Process -Name Acrobat -Force"
cmd_delete_file = "powershell.exe Remove-Item -Path '{0}'".format(item['file'])
self.logger.info('[+] Printing file {0}'.format(item['file']))
p = subprocess.Popen(cmd_set_default_printer, stdout=subprocess.PIPE)
p_out = p.communicate()
if p.returncode != 0: # non-zero return code means a failure
self.logger.error('[!] An error occured: {0}'.format(p_out))
self.db.set_item_status(item['id'], self.db.STATUS_FAILED)
continue
time.sleep(2)
p = subprocess.Popen(cmd_print_file, stdout=subprocess.PIPE)
p_out = p.communicate()
if p.returncode != 0:
self.logger.error('[!] An error occured: {0}'.format(p_out))
self.db.set_item_status(item['id'], self.db.STATUS_FAILED)
continue
time.sleep(5)
self.logger.info('[+] OK. Deleting file {0}'.format(item['file']))
p = subprocess.Popen(cmd_close_acrobat, stdout=subprocess.PIPE)
p_out = p.communicate()
p = subprocess.Popen(cmd_delete_file, stdout=subprocess.PIPE)
p_out = p.communicate()

当运行服务并向其推送文件时,我得到一个错误。这些是我的日志:

2023-01-16 15:13:20,589 - server_logger - INFO - [*] Listening as 0.0.0.0:50001
2023-01-16 15:13:20,620 - server_logger - INFO - [*] Connected to database
2023-01-16 15:20:40,916 - server_logger - INFO - [+] ('192.168.1.252', 44920) is connected.
2023-01-16 15:20:40,916 - server_logger - INFO - [+] Receiving new file... saving as wbcfaolzropovcui.pdf
2023-01-16 15:20:40,916 - server_logger - INFO - [+] Queue file for printing...
2023-01-16 15:20:40,942 - server_logger - INFO - [+] Queued.
2023-01-16 15:20:40,942 - server_logger - INFO - [+] Done receiving, closing socket.
2023-01-16 15:20:40,942 - server_logger - INFO - [+] Socket closed.
2023-01-16 15:20:41,309 - server_logger - INFO - [+] Printing file C:.cloudspotecosyerp-printingpython_backendprint_queuewbcfaolzropovcui.pdf
2023-01-16 15:20:44,012 - server_logger - ERROR - [!] An error occured: (b"Start-Process : This command cannot be run due to the error: Er is geen toepassing gekoppeld aan het opgegeven bestand nvoor deze bewerking.nAt line:1 char:1n+ Start-Process -FilePath 'C:\.cloudspot\ecosyerp-printing\python_backe ...n+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~n    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationExceptionn    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommandn n", None)

错误是荷兰语,但翻译成:

No application is associated with the specified file for this operation

这让我挠头,因为当我不运行脚本作为一个服务,但直接从CMD,然后它工作没有问题。

是否有任何原因导致服务内部不能工作而外部可以工作?

感谢Mathias R. Jessen,他向我指出该服务不是在本地帐户下运行,而是在系统帐户下运行。此系统帐户没有PDF文件的默认应用程序。

我能够解决这个问题,让服务在一个帐户下运行,这个帐户为PDF文件设置了默认应用程序。

从服务中打印不工作,但这是adobeacrobatreader的问题。我切换到FoxIt PDF Reader,并能够使用该服务打印,没有任何问题。

最新更新