Sunday, March 29, 2015

Zebra Printer Label Printing with Dynamics AX 2012 R3


Microsoft Dynamics AX 2012 R3 introduces label printing capabilities.  It does this by sending ZPL commands directly to a Zebra printer.

After researching the new capabilities, in tangent with the new warehouse License Plate functionality I was able to derive enough information to design my own label printing functionality.

There are several stages involved in the development process, which is a prerequisite to printing labels directly to Zebra printers.

  • A label designer, which will print ZPLII code and allow for field names where data substitution needs to occur (optional).
  • A table in Dynamics AX to store the design in ZPL format (memo field).
  • A class that does the data substitution against the ZPL code, which then sends the resulting string of data directly to the printer.
  • A printer capable of interpreting ZPL commands.

This article will explain the prerequisites, and give code examples on how to send label output directly to a Zebra printer.

Note:  I do not have a Zebra printer, and even though the printer I do use, doesn't print anything it does recognize that commands are being sent to the printer.  So I am concluding that should I have a Zebra prnter, the code example in this article would work.  If anyone can confirm that would be great.

For examples in this article, I am going to use the ZebraDesigner version 2.5 from Zebra Technologies Corporation



The first step in the process is to create a label design.  Preferably a design that can be used as a template for reuse.  The example I am creating will contain just one barcode.  I won't go into how to create the label, that can be determined by reading the documentation for ZebraDesigner.



The next step is to print the label to a .prn file.  Notice the highlighted fieldname in the text below.  I came up with that field name.  Later in the process that field name will get substituted with data.


Now that we have a 'template', we need to create the infrastructure in Dynamics AX to store the template and act upon it.  To do this a table was created called ZPLDocumentLayout.  It will store three things.

  • A layout Id.
  • A description of the layout.
  • The ZPL code from the template. (This is a memo field in the table)
To support data entry for the table a form called ZPLDocumentLayout was created and data was populated as follows:


Notice the form has a LayoutId of 'ZPL1'.  That is a code that I made up, and will use later in the process to identify which ZPL template data to use for the labels.  Also notice the template data has been inserted into the record. 

Next I created a table called ZPLDocumentLabel to hold the data for the labels.  It is a simple table with one field called BarcodeId, and holds 3 records, whose values are 'Label1', 'Label2', 'Label3'.

Now that the underlying data, and templates exist, a class needs created specifically to do the data substitution for the template.

Create a class called ZPLDocumentPrint with the following methods.



The code for the methods are listed below:

classDeclaration
getFieldList
initMenuFields
new
printDocument

The magic in this whole process is the new CSharp project WHS.DeviceCom, which was introduced in R3.  It allows communicating directly with printers with raw data.  Microsoft.Dynamics.AX.WHS.DeviceCom.Printer::SendStringToPrinter().

translate

What's fascinating about the translate method it substitutes the field names in the ZPL code, with the table field values in the ZPLDocumentLabel table by matching the field names between the ZPL code and table.


The final piece to the process is to bring it altogether and print the label data from the ZPLDocumentLabel table.  I did this using a job, which invokes the ZPLDocumentPrint class.  The code for the job is as follows:

The output can be captured through a debugging process and entered into a form online at Labelary Online ZPL Viewer

The output from this article looks like this:


Please note that you might need to enable pass through mode on the printer properties,  Prior to WHS.DeviceCom, the method I would use would be to send the ZPL string, wrapped in pass through codes ${ and }$ respectively.

15 comments:

Anna Schafer said...

There are several stages involved in the development process, which is a prerequisite to printing labels directly to Zebra printers.etiloga.com.mx

Unknown said...

https://community.dynamics.com/ax/f/33/t/180930 .. Can you please suggest me How can i achieve this .

Hat Man said...

Good job on this. let me ask you this. Is there another place for configuration of the WHS.DeviceCom.Printer because I can print a label as long as I am logged onto the AOS that I set the label printer on, however, from any other server or client, the label will not print

bxvretqwe said...

Hi, I have implemented this code and I have added ${ and }$ symbols at start and end of string but when I print appear the print icon with 0 pendant file in queue and don't print.

Unknown said...

Hi

I want Variants (Size and Color) along with the bar code and the item name.

Can you guide me for the same?

Regards
Wamik

Joe Hodge said...

These are some of my first blogs, and didn't realize the amount of exposure and comments they have had. If anyone has questions, or haven't had your questions answered please feel free to comment, I will follow up in short order.

Unknown said...

Hi Joe,

Thanks for sharing this one. I have implemented it successfully.

But I have one concern regarding 'N' no. of copies like if we want 5 copies of each barcode items needs to be a print from Zebra printer then how we can achieve it?

Kindly provide me solution for this issue.

Thanks & Regards
Pankaj Arora

Unknown said...

Hi Joe,

Thanks for sharing this one. I have implemented it successfully.

But I have one concern regarding 'N' no. of copies like if we want 5 copies of each barcode items needs to be a print from Zebra printer then how we can achieve it?

Kindly provide me solution for this issue.

Thanks & Regards
Pankaj Arora

Unknown said...

This worked well for us. We made some modifications to the methodology, but the core of this code got us up and running. Thank you!

Unknown said...

I need to connect the Zebra label printer to AX2012 R2..Is it possible?? Please suggest your answers.
Thank you all for advance..

subbu said...

Hi,

This is really good article.

But How to print the label with 3 rows 3 columns format. i.e. when i send 9 print requests (9 labels print request) to the Zebra printer, only the first column is getting printed. But not the second and third columns.

Any idea how to set rows and columns option in X++ or Zebra format code?

Thanks
Subbu

yash kothari said...

Hi,

do you Have the xpo File of this for setting Up?

it can be really helpful.

Thanks & Regards
Yash Kothari

saurabh said...

Hi,

I have a ZPL printer connected to a local machine, It is not connected to the AOS server and I am not able to print the label by using the DLL "Microsoft.Dynamics.AX.WHS.DeviceCom.Printer::SendStringToPrinter(PrinterName, str);".
Can you suggest so that I am able to print from locally connected printer.

Thanks in advance.
Saurabh

Modrzycki said...

Thesame:
https://axvigneshvaran.wordpress.com/2020/04/12/zebra-printer-label-printing-with-dynamics-ax-2012-r3-and-dynamics-365-for-finance-operations/

Ana Burden said...

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. renting impresoras murcia