How to print raw BOCA FGL commands from Javascript
Product JSPrintManager Published 07/30/2018 Updated 01/15/2019 Author Neodynamic
Overview
FRIENDLY GHOST LANGUAGE (FGL) is Boca's industry standard language that allows developers to print data, graphics, boxes and bar codes anywhere on a ticket or label in virtually any orientation.
BOCA FGL commands are very simple and text plain! The main advantage of using raw BOCA FGL 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. No matter which Web Platform/Framework you use (ASP.NET, PHP, Django, Ruby On Rails (RoR), Express.js, AngularJS/SPA, etc.), you'll be able to use raw printing feature with the help of our JSPrintManager solution that was specially designed for this kind of printing needs.
In this walkthrough, you'll learn how to print raw FGL commands from Javascript directly to the client printer without displaying a print dialog at all. You'll be able to print BOCA FGL 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 BOCA FGL commands that we'll use in this article will print out a ticket passport that will look like this:
A Sample Ticket Passport printed from Javascript and created by using BOCA FGL 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! - By using your favorite Web Development IDE or Text Editor, create a new HTML file like index.html
Copy/paste the following snipped codes:HTML Code
<div style="text-align:center"> <h1>Print BOCA FGL commands from Javascript</h1> <hr /> <label class="checkbox"> <input type="checkbox" id="useDefaultPrinter" /> <strong>Print to Default printer</strong> </label> <p>or...</p> <div id="installedPrinters"> <label for="installedPrinterName">Select an installed Printer:</label> <select name="installedPrinterName" id="installedPrinterName"></select> </div> <br /><br /> <button type="button" onclick="print();">Print Now...</button> </div>
Script References
- Download JSPrintManager.js
- Copy all of these *.js files to the same folder where your html page is located and the add the following script references to your page.
<script src="JSPrintManager.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.5/bluebird.min.js"></script> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
Script Code
<script> //WebSocket settings JSPM.JSPrintManager.auto_reconnect = true; JSPM.JSPrintManager.start(); JSPM.JSPrintManager.WS.onStatusChanged = function () { if (jspmWSStatus()) { //get client installed printers JSPM.JSPrintManager.getPrinters().then(function (myPrinters) { var options = ''; for (var i = 0; i < myPrinters.length; i++) { options += '<option>' + myPrinters[i] + '</option>'; } $('#installedPrinterName').html(options); }); } }; //Check JSPM WebSocket status function jspmWSStatus() { if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Open) return true; else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Closed) { alert('JSPrintManager (JSPM) is not installed or not running! Download JSPM Client App from https://neodynamic.com/downloads/jspm'); return false; } else if (JSPM.JSPrintManager.websocket_status == JSPM.WSStatus.Blocked) { alert('JSPM has blocked this website!'); return false; } } //Do printing... function print(o) { if (jspmWSStatus()) { //Create a ClientPrintJob var cpj = new JSPM.ClientPrintJob(); //Set Printer type (Refer to the help, there many of them!) if ($('#useDefaultPrinter').prop('checked')) { cpj.clientPrinter = new JSPM.DefaultPrinter(); } else { cpj.clientPrinter = new JSPM.InstalledPrinter($('#installedPrinterName').val()); } //Set content to print... //Create BOCA FGL commands for sample label var cmds = ''; cmds += '<RL><RC360,10><F3><HW1,1>GHOSTWRITER WORLD'; cmds += '<RC380,76><F6><HW1,1><BS26,44>ALL<F2>'; cmds += '<F6><BS26,44>THREE<F2> <F6><BS26,44>PARKS'; cmds += '<RC348,130><F6><HW1,1><BS42,44>PASSPORT'; cmds += '<RC324,240><RL><F6><HW2,2>6'; cmds += '<RC210,240><HW1,1>DAY'; cmds += '<RC230,290><F3><HW1,1>ADMIT ONE'; cmds += '<RC230,320><F6><HW1,1>GUEST'; cmds += '<F1><RC230,370><F1><HW1,1>VERY SMALL PRINT'; cmds += '<RC24,530><LT2><BX340,50>'; cmds += '<RC25,528><LT2><VX338>'; cmds += '<RC216,550><HW1,1><F2>DAY 1'; cmds += '<RC24,580><LT2><BX340,50>'; cmds += '<RC216,600>DAY 2'; cmds += '<RC24,630><LT2><BX340,50>'; cmds += '<RC216,650>DAY 3'; cmds += '<RC24,680><LT2><BX340,50>'; cmds += '<RC216,700>DAY 4'; cmds += '<RC24,730><LT2><BX340,50>'; cmds += '<RC25,780><LT2><VX338>'; cmds += '<RC216,760>DAY 5'; cmds += '<RC340,400><RL><F6><BS36,44><HW1,1>DAY GUEST'; cmds += '<RC260,450><F3><HW1,1>$112.00'; cmds += '<RC240,482><F3>PLUS TAX'; cmds += '<RC280,1010><F3><HW1,1>12345678'; cmds += '<RC60,990><NL10><X2>*01000407*'; cmds += '<RC360,820><F9><HW1,1>VALID ONLY ON DATE STAMPED'; cmds += 'NONTRANSFERABLE NONREFUNDABLE'; cmds += '<RC280,870><F3><HW1,1>01000407'; cmds += '<RC20,1079><RR><F3><HW1,1>GHOSTWRITER WORLD'; cmds += '<p>'; cpj.printerCommands = cmds; //Send print job to printer! cpj.sendToClient(); } } </script>
- That's it! Run your website and test it. Click on Print Now... to print the BOCA FGL 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.