# Print Labels from Client

**URL:** <https://forum.inductiveautomation.com/t/print-labels-from-client/94399>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [August 9, 2024, 9:01am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399 "2024-08-09T09:01:14Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 9, 2024, 9:01am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/1 "2024-08-09T09:01:14Z")

</div>

Hi team,

I need to print labels automatically from a Vision client without requiring a button click. I've implemented the logic in the change event of a client tag. The printers available on the client systems are also configured on the gateway server.

The same logic works as expected with my local Ignition instance, but it's not functioning correctly when I try it through a Vision client button, tag, script console, or gateway event.

In summary, I want to print a report from the client using the client's default printer based on a specific condition. Additionally, I'd like guidance on how to handle printing to a USB printer.

Script used:

```python
from javax.print import PrintServiceLookup as PSL

printerList = PSL.lookupPrintServices(None, None)

defaultPrinter = PSL.lookupDefaultPrintService()

for printer in printerList:
    print printer.getName()

print 'Default printer : ' + defaultPrinter.getName()
defprin =defaultPrinter.getName()
#system.gui.messageBox(defprin)
settings = {'primaryPrinterName':defprin, 'backupPrinterName': 'none', 'copies': 1, 'printBothSides': True,
'collate': True, 'useRaster': False, 'rasterDPI': 1200, 'useAutoLandscape': False, 'pageOrientation': 'portrait'}
system.report.executeAndDistribute(path='Report', project = 'CMS', parameters=None, action='print', actionSettings=settings)

```

**EDIT:**  
When i try to pass the printer name directly, it is working but in gateway it shows defaultPrinter as none with below error

```python
<type 'exceptions.AttributeError'>, 'NoneType' object has no attribute 'getName'

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 9, 2024, 11:10am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/2 "2024-08-09T11:10:47Z")

</div>

`system.report.executeAndDistribute` only ever runs in gateway scope, and can only reach printers directly available to the gateway. (The gateway service user, specifically.)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 9, 2024, 3:26pm UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/3 "2024-08-09T15:26:36Z")

</div>

You can use `executeReport` instead, to get the report bytes as a PDF, then pass that to the local printer directly using a modified form of the code Jordan posted here:

> [@Printing to a local client printer in a Vision client](https://forum.inductiveautomation.com/t/printing-to-a-local-client-printer-in-a-vision-client/63822/6):
>
> I had this done up yesterday, but then got yanked to go to to one of our sister plants. roll_eyes I prefer looking up the printer by name, because somehow, somewhere, someone will add another printer and make it the default. from javax.print import DocFlavor, SimpleDoc, PrintServiceLookup from java.io import ByteArrayInputStream # Get all available printers printServices = PrintServiceLookup.lookupPrintServices(None, None) # Initialize print service variable printService = None # Check fo…

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 10, 2024, 2:24am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/4 "2024-08-10T02:24:55Z")

</div>

> [@Dharani\_T](#):
>
> `from javax.print import PrintServiceLookup as PSL`

This is not how I print in Vision. My printing normally looks something like this:

```python
objectToBePrinted = # Get this somehow

# Pass the object to be printed into the createPrintJob utility function
job = system.print.createPrintJob(objectToBePrinted)

# Decide whether or not to show the print dialog
# ...to give the user the opertunity to access options like changing printers or printing multiples
job.setShowPrintDialog(False)

# Set the margines to zero in an effort to maximize print size
job.setMargins(0)

# Set the print orientation: 0 = Landscape, 1 = Portrait
job.setOrientation(1)

# Execute the printer call
job.print()

```

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 12, 2024, 4:59am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/5 "2024-08-12T04:59:34Z")

</div>

Hi,  
I have tried the suggested method, why am i getting the below error?

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/8/c/8c78f75416b64e1eb5062a06c67dc7c3ca1214fc.png)  
Pl Note: I have set Microsoft print to pdf as default.  
I am also getting below error in the printer queue, if i set the default printer  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/d/1de57eec1de35d6dec3baa6780f0e90544c2af15.png)

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 12, 2024, 9:31am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/6 "2024-08-12T09:31:30Z")

</div>

I have tried this, but how to pass objectToBePrinted from report? I am stuck at this point.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 12, 2024, 11:33am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/7 "2024-08-12T11:33:06Z")

</div>

Justin's code only works for actual vision components or windows, not for arbitrary binary objects. You must use the print service for arbitrary content, and you need to send the arbitrary content through a printer driver that understands it. PDF may not be supported.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 13, 2024, 4:13am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/8 "2024-08-13T04:13:40Z")

</div>

Hi,

Now i have tried with different approach and it is working properly. Only prerequisite of this script is to have Adobe reader. Do u see any issue here?

```python
import os
import sys
import traceback
from java.io import FileOutputStream

# Cache the Acrobat path to avoid searching each time
acrobat_path = None

def find_acrobat_executable():
    global acrobat_path
    if acrobat_path:
        return acrobat_path
    
    acrobat_dirs = [
        r"C:\Program Files\Adobe",
        r"C:\Program Files (x86)\Adobe"
    ]
    possible_executables = ['acrord32.exe', 'acrobat.exe', 'Acrobat.exe','AcroRd32.exe']

    for acrobat_dir in acrobat_dirs:
        if not os.path.exists(acrobat_dir):
            continue

        for root, dirs, files in os.walk(acrobat_dir):
            for file in files:
                if file in possible_executables:
                    acrobat_path = os.path.join(root, file)
                    return acrobat_path
    
    return None

def printLabel():
    pdfBytes = system.report.executeReport(path="XXX", project='XXX', fileType="pdf")
    
    tempFilePath = system.file.getTempFile("pdf")
    with open(tempFilePath, "wb") as tempFile:
        tempFile.write(pdfBytes)
    
    print_success = False
    
    try:
        os_name = system.util.getProperty("os.name")
        
        if os_name.startswith("Windows"):
            acrobat_path = find_acrobat_executable()
            system.tag.writeBlocking('[client]Print', ['Print Started'])
            
            if acrobat_path:
                # Update command to use /t for printing
                command = '"{}" /t "{}"'.format(acrobat_path, tempFilePath)
                print("Command to be executed: {}".format(command)) # Debug print
                
                result = os.system(command)
                print("Result of command execution: {}".format(result)) # Debug print
                
                if result == 0:
                    system.tag.writeBlocking('[client]Print', ['Printed successfully'])
                    print_success = True
# else:
# system.gui.messageBox("Failed to print. Command returned non-zero result.")
            else:
                system.tag.writeBlocking('[client]Print', ['Adobe Reader or Acrobat executable not found.'])
                
    except Exception as e:
        exType, ex, tb = sys.exc_info()
        iMessage = "ERROR in 'Script -' line {}: {}, {}".format(traceback.tb_lineno(tb), exType, ex)
        system.gui.messageBox("Failed to Print, make sure Adobe Reader or Acrobat is running properly.\n" + iMessage)
    
    finally:
        for exe in ['acrord32.exe', 'acrobat.exe', 'Acrobat.exe','AcroRd32.exe']:
            os.system('taskkill /F /IM {}'.format(exe))

```

**EDIT:**  
My actual requirement is,

After every transaction, labels should be printed attached to each client at a high speed (automatic print). Printers here will be different for each client ( 1 printer for A5 pdf, 1 label printer)  
I think there must be some simple solution for this.  
I require your feedback on this @justinedwards.jle @paul-griffith @pturmel .  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 19, 2024, 3:29pm UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/9 "2024-08-19T15:29:07Z")

</div>

> [@Dharani\_T](#):
>
> Only prerequisite of this script is to have Adobe reader.

That's a significant caveat on its own. You're depending both on Acrobat being installed _and_ it continuing to support this immediate print command line flag. I would attempt to accomplish this task entirely using Java's built in APIs.

I haven't ever done the exact task you're trying to accomplish, and I don't have the time to come up with an example script. Someone else may be more generous with their time.

I'll also gently remind you that this forum is _not_ an official support channel, and there are no service level agreements nor any obligation of support. I didn't really have anything to offer when you first posted, so direct messaging me _in addition_ to the tag on the post was not necessary or appreciated. You are welcome to try to contact [the support department officially](https://support.inductiveautomation.com/hc/en-us) for help with this script, though it's not really their purview, either - you haven't found any bug in Ignition, you're just trying to accomplish a piece of integration work.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 20, 2024, 8:16am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/10 "2024-08-20T08:16:12Z")

</div>

> [@Dharani\_T](#):
>
> I require [...] @justinedwards.jle

Cool. I plan on taking Thursday off, but with all have to get done at home on that day, I probably won't get to this.

> [@Dharani\_T](#):
>
> it is working properly [...] Do u see any issue here? [...] feedback

If I were on your team, and this code was working, I probably wouldn't touch it, but my inclinations are to refactor the code to get rid of that global variable and the try/except clause.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 20, 2024, 8:43am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/11 "2024-08-20T08:43:06Z")

</div>

Also I tried your code with container print logic and it is working properly.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 21, 2024, 10:09am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/12 "2024-08-21T10:09:33Z")

</div>

Hi,

I am facing dimension issue here,  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/8/288105f01624007859dd8d137187a9cb860027a2.png)  
Size of this container is 220 \* 140.  
Printed label:

 ![Untitled](https://us1.discourse-cdn.com/inductiveautomation/original/3X/0/e/0e7e07e119075c3f18790d103b3c949fab0c65a1.jpeg)

Script used:

```python
objectToBePrinted =printObject
		
		# Pass the object to be printed into the createPrintJob utility function
		job = system.print.createPrintJob(objectToBePrinted)
		
		# Decide whether or not to show the print dialog
		# ...to give the user the opertunity to access options like changing printers or printing multiples
		job.setShowPrintDialog(False)
		

		
		# Set the print orientation: 0 = Landscape, 1 = Portrait
		job.setOrientation(1)
		job.setPageWidth(2.95)
		job.setPageHeight(1.96)
		job.setPrinterName(printer)
		job.setFitToPage(True)
		job.setMargins(0.2)
	
		if printer is not None:
			# Execute the printer call
			job.print()
		
			
			return 1
		else:
			
			return 2

```

Label size should be 7.5 cm width \* 5cm height. Can someone help with this?

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 22, 2024, 5:39am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/13 "2024-08-22T05:39:28Z")

</div>

Adjusting `setPageWidth` doesn't help? It's possible that `setFitToPage` will need to be false for those settings to have effect.

Also, it is possible to set the margins seperately:

> **[system.print.createPrintJob | Ignition User Manual](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-print/system-print-createPrintJob)**
>
> Provides a general printing facility for printing the contents of a window or component to a printer.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 22, 2024, 8:30am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/14 "2024-08-22T08:30:27Z")

</div>

No, it didnt help. I have set `setFitToPage` to false and got the same output.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [August 22, 2024, 8:35am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/15 "2024-08-22T08:35:10Z")

</div>

Have you tried adjusting `job.setRightMargin(x.xx)`?  
...or if arbitrarily moving that one way or the other doesn't help, are you printing a container? Perhaps keep the container size what it is, and resize the internal component a little bit to get the margin you need.

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 22, 2024, 8:37am UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/16 "2024-08-22T08:37:29Z")

</div>

Yes, i tried adjusting margin but again it is getting cutoff.  
Am trying to print a container and i have set layout - relative and maintain aspect ratio

---

<div class="post-metadata">

**Author:** ![Dharani\_T](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@Dharani\_T](https://forum.inductiveautomation.com/u/Dharani_T)\
**Post date:** [August 28, 2024, 12:01pm UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/17 "2024-08-28T12:01:45Z")

</div>

I have achieved the print with correct dimension using ZPL.

> [@Printing a label from a Zebra ZT230 using USB connection](https://forum.inductiveautomation.com/t/printing-a-label-from-a-zebra-zt230-using-usb-connection/72462/19):
>
> ZPL2 Guide (plus others) 'Playgound' Phil mentioned:

But i am looking for other options so that we need not depend on specific printer.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 26, 2025, 2:37pm UTC](https://forum.inductiveautomation.com/t/print-labels-from-client/94399/18 "2025-03-26T14:37:04Z")

</div>

Split this topic on March 26, 2025, 2:37pm UTC

2 posts were split to a new topic: [Question about printing from Vision](https://forum.inductiveautomation.com/t/question-about-printing-from-vision/104240)
