system.print.getDefaultPrinterName() returns null

I have a gateway where we need to print labels and reports. We are able to do so if we run system.print.getPrinterNames() and print to one of the returned values but would prefer to print to the default printer (at least for the reports). When we run system.print.getDefaultPrinter(), the returned value is null. If I RDP into the gateway box, I can confirm that a default printer is set up on the box. I can even change the default to the Microsoft PDF driver and the returned value is still null.
Anyone have any ideas what we're missing?

I'm assuming Windows...

Generally, the user that is running the Ignition service doesn't have enough permissions to obtain that information.

You will need to change the service user to a user that has the proper permission and then restart the Ignition service.

Yes, Windows Server 2019 is the OS. We have already setup up the user and the Ignition service is configured to use that user. Printing works and we can get the printer names from system.print.getPrinterNames(). It's only the default printer we cannot retrieve.

How/where are you running each system.print function?

We are running the code on a button event on a Perspective view.

This works:

printers = system.print.getPrinterNames()[0]
self.view.custom.defaultPrinter = printers 

This returns null:

defaultPrinter = system.print.getDefaultPrinter()
self.view.custom.defaultPrinter = defaultPrinter

There's a bunch of possible failure cases for lookupDefaultPrintService, which is the underlying Java method the system function is calling. Unfortunately, you're at the mercy of your OS and Java's interaction with each other.

It looks like the JDK shells out to a native method:

Which itself apparently calls out to the Windows registry:

Have you restarted the gateway service since you set up the default? The service is independent of what you do on the desktop.

Yes, that was one of the first things we tried.

Just want to confirm, but when you RDP into the machine to check the default printer, are you using the exact same account as the Ignition service? Default printers are on a per-user basis I believe, so if one hasn't been set up for the user yet, it may not have one or is set to the PDF printer which is probably going nowhere.

2 Likes

Sometimes I can't see the obvious. That was it! Thank you, sir!

Thanks for pointing that out. I've changed the solution to the correct place.