In the system.print.createPrintJob function, it seems I can pass most arguments to the job that can also be set in the printer dialog.
However, I don’t seem able to set the number of copies. I want to calculate the number of boxes based on the weight, and then print the same sheet for every box produced to accompany it.
Is there some way I can achieve this? java.awt.print.PrinterJob does seem to have such a function available. Can I get in from the Ignition printerjob to the awt printer job, or is there some other base class used?
Printing the sheets in a loop seems tedious, as I want to also provide an option for when the default printer is offline, so they can select a different printer. If I loop, I either can’t pass the number of copies, or I get a huge number of print dialogs.
This example prints 4 copies only showing the dialog once.
printJob = system.print.createPrintJob(self.parent.getComponent('Power Table'))
printJob.print()
# Stop printing if user pressed cancel!
if printJob..getPrinterName():
printJob.setShowPrintDialog(False)
for i in range(3):
printJob.print()
Just realized my previous post doesn’t work as expected
This code does however. The example prints a power table component. You might need to modify a little to print something else.
from javax.swing.JTable import PrintMode
from javax.print.attribute import HashPrintRequestAttributeSet
from javax.print.attribute.standard import Copies
from java.awt.print import PrinterJob
printable = self.parent.getComponent('Power Table').getTable().getPrintable(PrintMode.FIT_WIDTH, None, None)
job = PrinterJob.getPrinterJob()
job.setPrintable(printable)
aset = HashPrintRequestAttributeSet();
aset.add(Copies(2))
job.printDialog(aset) # Or just job.print(aset) to print without the dialog.
Apart from that, you always need to execute print() after printDialog(), as printDialog only seems to modify the aset, and return True when the user chose to print.
Then I added some other options, like the possibility to select a default printer, and changing the orientation.
from javax.swing.JTable import PrintMode
from javax.print.attribute import HashPrintRequestAttributeSet
from javax.print.attribute.standard import Copies
from java.awt.print import PrinterJob
from javax.print.attribute.standard import OrientationRequested
from com.inductiveautomation.factorypmi.application.script.builtin.PrintUtilities import ComponentPrinter
printable = ComponentPrinter(event.source.parent.getComponent('Container'), True, 1)
job = PrinterJob.getPrinterJob()
job.setPrintable(printable)
aset = HashPrintRequestAttributeSet();
aset.add(Copies(2))
aset.add(OrientationRequested.LANDSCAPE);
services = job.lookupPrintServices()
print [s.getName() for s in services] # find the correct service in the list to select a printer
job.setPrintService(services[8])
if job.printDialog(aset):
job.print(aset)