How to print raw Godex EZPL commands from Blazor
Product JSPrintManager for Blazor Published 07/07/2021 Updated 07/07/2021 Author Neodynamic
Overview
The EZPL (EZ Programming Language) is a high-level label definition and printer control language for Godex printers. There are three types of commands in EZPL:
- Setup commands: It includes printer control instructions, configuration instructions and image downloading instructions.
- Control commands: It includes commands that cancontrol the printer to take action immediately, such as cleaning memory, feedinglabel.
- Label Format commands: Define the format of datathat will be presentedon the label, such as Line, Rectangle, Barcode, Text and image.
- The syntax of commands contains capital letters as the ID for each function.
- The lower case letters in command represent parameters.
- Control and Setup commands use the tilde (~) and caret (^) as prefix.
- Label Format commands have no prefix.
- The comma (,) is the delimiter to separate each parameter, and the CR (Carriage Return) signifies the end of every command.
Godex EZPL commands are very simple and text plain! The main advantage of using raw Godex EZPL commands for printing instead of using the built-in browser javascript printing (window.print();) is that the printing performance will be way faster; a factor that is key in the aforementioned scenarios. You'll be able to use raw printing feature with the help of our JSPrintManager for Blazor solution that was specially designed for this kind of printing needs.
In this walkthrough, you'll learn how to print raw Godex EZPL commands from Blazor directly to the client printer without displaying a print dialog at all. You'll be able to print Godex EZPL commands to the Default client printer as well as to any other installed printer at the client machine. This solution works with any popular browser like Chrome, Firefox, IE/Edge & Safari on Windows, Linux, Raspberry Pi and Mac systems!
The Godex EZPL commands that we'll use in this article will print out a simple label that will look like this:
A Sample Label printed from Blazor and created by using Godex EZPL commands
Follow up these steps
- Be sure you install in your dev machine JSPrintManager (JSPM) (Available for Windows, Linux, Raspberry Pi & Mac)
This small app must be installed on each client that will print from your website! - In your Blazor project...
- Add a NuGet reference to the JSPrintManager Razor Component
- Add the JSPrintManager service...
- Add the following statement at the top of your
Startup
file
using Neodynamic.Blazor;
- For Blazor Server
Add the following line in theStartup's ConfigureServices
method
services.AddJSPrintManager();
- For Blazor WebAssembly
Add the following line in theProgram's Main
method
builder.Services.AddJSPrintManager();
- For Blazor Server
- Add the following statement at the top of your
- Add the following statement in the
_Imports.razor
file
@using Neodynamic.Blazor
- Add a new Razor Page and copy/paste the following code. Please read the source code comments to understand the printing logic!
@page "/" @inject JSPrintManager JSPrintManager <div> <strong>JSPM </strong><span>WebSocket Status </span> @if (JSPrintManager.Status == JSPMWSStatus.Open) { <span class="badge badge-success"> <i class="fa fa-check" /> Open </span> } else if (JSPrintManager.Status == JSPMWSStatus.Closed) { <span class="badge badge-danger"> <i class="fa fa-exclamation-circle" /> Closed! </span> <div> <strong>JSPrintManager (JSPM) App</strong> is not installed or not running! <a href="https://neodynamic.com/downloads/jspm" target="_blank">Download JSPM Client App...</a> </div> } else if (JSPrintManager.Status == JSPMWSStatus.Blocked) { <span class="badge badge-warning"> <i class="fa fa-times-circle" /> This Website is Blocked! </span> } else if (JSPrintManager.Status == JSPMWSStatus.WaitingForUserResponse) { <span class="badge badge-warning"> <i class="fa fa-user-circle" /> Waiting for user response... </span> } </div> @if (JSPrintManager.Status == JSPMWSStatus.Open) { @if (JSPrintManager.Printers == null) { <hr /> <div class="spinner-border text-info" role="status"> <span class="sr-only">Please wait...</span> </div> <strong><em>Getting local printers...</em></strong> } else { <div> <h1>Print Zebra ZPL commands from Blazor</h1> <hr /> <EditForm Model="@MyPrinter"> <div class="form-check"> <InputCheckbox class="form-check-input" @bind-Value="UseDefaultPrinter" />Print to <strong>Default Printer?</strong><br /> </div> <p>or...</p> <p>Select an <strong>Installed Printer</strong>:</p> <InputSelect @bind-Value="MyPrinter.PrinterName" class="form-control form-control-sm"> @foreach (var p in JSPrintManager.Printers) { <option value="@p">@p</option> } </InputSelect> </EditForm> <br /><br /> <input type="button" value="Print Now..." @onclick="DoPrint" /> </div> } } @code { // An Installed Printer instance private InstalledPrinter MyPrinter { get; set; } = new(); // Use default printer? private bool UseDefaultPrinter = false; // Printing... private void DoPrint() { // Status = Open means that JSPM Client App is up and running! if (JSPrintManager.Status == JSPMWSStatus.Open) { // Create a ClientPrintJob var cpj = new ClientPrintJob(); // Set target Printer cpj.ClientPrinter = UseDefaultPrinter ? new DefaultPrinter() : MyPrinter; // Create Godex EZPL commands for sample label var CR = (char)0x0D; var cmds "^Q100,0,0" + CR; cmds += "^W102" + CR; cmds += "^H5" + CR; cmds += "^P1" + CR; cmds += "^S4" + CR; cmds += "^AD" + CR; cmds += "^C1" + CR; cmds += "^R0" + CR; cmds += "~Q+0" + CR; cmds += "^O0" + CR; cmds += "^D0" + CR; cmds += "^E15" + CR; cmds += "~R200" + CR; cmds += "^L" + CR; cmds += "Dy4-me-dd" + CR; cmds += "Th:m:s" + CR; cmds += "Lo,294,12,297,633" + CR; cmds += "BG,428,32,2,5,100,0,1,12345678901212345" + CR; cmds += "BQ,148,184,3,7,100,1,1,CODE128" + CR; cmds += "BH,288,212,3,7,100,1,1,12345678901" + CR; cmds += "AG,358,175,1,1,0,0,GODEX EZPL" + CR; cmds += "BA,448,461,2,6,95,0,1,CODE39" + CR; cmds += "AB,14,22,2,2,0,0,SOME TEXT" + CR; cmds += "Lo,296,164,799,165" + CR; cmds += "Lo,296,268,799,269" + CR; cmds += "AA,306,38,1,1,0,0,EAN13 ADD5" + CR; cmds += "W449,296,5,1,M,8,4,17,0" + CR; cmds += "QRCODE 0123456789" + CR; cmds += "XRB591,297,4,0,21" + CR; cmds += "DATAMATRIX 0123456789" + CR; cmds += "E" + CR; // Set the RAW commands to send to the printer... cpj.PrinterCommands = cmds; // PRINT IT!!! JSPrintManager.SendClientPrintJob(cpj); } } protected override void OnAfterRender(bool firstRender) { if (firstRender) { // Handle OnGetPrinters event... JSPrintManager.OnGetPrinters += () => { if (JSPrintManager.Printers != null && JSPrintManager.Printers.Length > 0) { // Display installed printers... StateHasChanged(); } else { Console.WriteLine("No printers found..."); } }; // Handle OnStatusChanged event to detect any WSS status change JSPrintManager.OnStatusChanged += () => { StateHasChanged(); // Status = Open means that JSPM Client App is up and running! if (JSPrintManager.Status == JSPMWSStatus.Open) { //Try getting local printers... JSPrintManager.TryGetPrinters(); } }; // Start WebSocket comm JSPrintManager.Start(); } base.OnAfterRender(firstRender); } }
- That's it! Run your website and test it. Click on Print Now... to print the Godex EZPL Commands without print dialog. You can print it to the Default client printer or you can get a list of the installed printers available at the client machine.
NOTE You can also print directly to any LPT Parallel Port, RS232 Serial Port or IP/Ethernet Printer although these scenarios have not been considered in this article for simplicity. For further details on those scenarios, please contact our tech support.