How to print a PDF from a gateway script

How to print a PDF from a gateway script based on a tags value.

import system

if system.tag.read ("Test123").value ==1:
	job =system.print.createPrintJob('\\amifile\Departments\Work Instructions and Procedures\Forms\QD 711 Daily Air Compressors Checklist.PDF')
	job.setPrinterName('PLC-Office')
	job.setShowPrintDialog(0)
	job.setPageHeight(5)
	job.setPageWidth(5)
	job.setMargins(.5)
	job.setOrientation(1)
	job.print()
	
	```

You tag path must include the tag provider when called in the GW scope.

I added the tag provider but it still doesn’t print

import system

if system.tag.read ("[default]Test123").value ==1:
	job =system.print.createPrintJob('\\amifile\Departments\Work Instructions and Procedures\Forms\QD 711 Daily Air Compressors Checklist.PDF')
	job.setPrinterName('PLC-Office')
	job.setShowPrintDialog(0)
	job.setPageHeight(5)
	job.setPageWidth(5)
	job.setMargins(.5)
	job.setOrientation(1)
	job.print()
	
	

Does the Ignition gateway server have a Printer installed on it called PLC-Office ? Also, you’ll have to make sure the Ignition service has access to the shared file as well. You didn’t specify much about your setup so I’ll assume you’re using Windows 10; out the box Ignition installs will use the SYSTEM user which probably won’t be able to read the PDF in the file path. Another thing to point out is the file path for every \ you need an extra one to escape it.

new path:

\\\\amifile\\Departments\\Work Instructions and Procedures\\Forms\\QD 711 Daily Air Compressors Checklist.PDF

I have printed the same file path from the pdf viewer. I will check on the access for the printer.

There’s a big difference here. Another thing to note is the createPrintJob function required a component object and your passing a filepath.

https://docs.inductiveautomation.com/display/DOC79/system.print.createPrintJob

I agree with you. Is there documentation for printing from the gateway and not a component? I was testing the script how it was to see if it could work.

I haven’t tried to print a file from the network before but I don’t think it will work the way your doing it. the createPrintJob function is looking for a component. Your passing it a string which I’m sure is the location of the file you want to print but I doubt it will open the file from the string.

The easiest way I could see doing it is by using the PDF viewer. You could have it hidden on a screen or open a popup with it to load the PDF and print it, then close it again. Easiest I would think would be having the object hidden on the screen. Using the .loadPDFBytes function to put your PDF into the viewer, then use the .print function to print it. You would need to use the system.file.readFileAsBytes() function to turn your pdf into the bytes needed by the PDF viewer. If you set the showDialog in your .print call to false, I would assume that it will use the clients default printer.

I’ve only ever printed Ignition reports from the gateway. Not a file per se.

I have printed from the PDF viewer as will a components (tables etc)

I will keep digging

I looked through the gateway logs. I get a error saying print has no attribute

Caused by: org.python.core.PyException: Traceback (most recent call last): File "", line 4, in AttributeError: 'com.inductiveautomation.ignition.common.script.Imm' object has no attribute 'print'

... 13 common frames omitted

If your still using:

job =system.print.createPrintJob('\\amifile\Departments\Work Instructions and Procedures\Forms\QD 711 Daily Air Compressors Checklist.PDF')

Then it will keep giving you that error. ‘\amifile\Departments\Work Instructions and Procedures\Forms\QD 711 Daily Air Compressors Checklist.PDF’ is a string instead of an object so it won’t have a print attribute.

These system.print functions are scoped to Vision Client only.

I don’t know if you can print from the gateway scope without using report module’s system.report.executeAndDistribute on a report.

1 Like

So I can print from a PDF viewer from a script

Yes. Yes?

Was that a question? :slight_smile:

1 Like

so how do I go about doing that? I have a window with the pdf viewer on it already with the same file path

You can’t do this from the gateway. You have to use the .print() method on the component.

https://docs.inductiveautomation.com/display/DOC79/PDF+Viewer

How do I print from the gateway script?

You can’t, at least not within Ignition’s supported methods. The supported method is to use the Reporting Module.

You might be able to dig around in Java’s print engine to do this, but I haven’t seen anyone do so. Start here:
https://docs.oracle.com/javase/8/docs/technotes/guides/jps/index.html