Friday 19 February 2021

DYMO Label Framework JavaScript Library Async Example: Print a label

 Recently did some work to add label printing to an app I have written for Canoe Slalom.

I found it hard to find any examples, particularly async examples.

So here is my code for you to use/abuse as you wish.

My code displays a spinner at the start, which is removed when the label has finished printing. it also enables a submit button. 

My label description is in a hidden form field called label and the address to print is in a field called address.

 <script src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js" type="text/javascript" charset="UTF-8">  

<script type="text/javascript" charset="UTF-8">

function loader() {

// dymo.label.framework.trace = 1; 

dymo.label.framework.init();

}

function printLabel() {

    $('#spinner').show();

var address = document.getElementById('address').value;

  var textArea = document.createElement('textarea');

  textArea.innerHTML = document.getElementById('label').innerHTML;

  var labelXml = textArea.value;


var label = dymo.label.framework.openLabelXml(labelXml);

label.setObjectText("Text", address);

dymo.label.framework.getPrintersAsync().then(function(printers) {

var printerName = '';

if (printers.length > 0) {

for (var i = 0; i < printers.length; ++i)

{

    var printer = printers[i];

    if (printer.printerType == "LabelWriterPrinter" && printer.isConnected == true)

    {

        printerName = printer.name;

        break;

    }

}

}

if (printerName!='') {

label.printAsync(printerName).then(function() {

  $('#spinner').hide();

    enableButton('submit');

});

}

else {

  $('#spinner').hide();

    alert("Could not find Label Writer printer");

    enableButton('submit');

}

});

}

</script>

Label contains the following


<?xml version="1.0" encoding="utf-8"?><DieCutLabel Version="8.0" Units="twips"><PaperOrientation>Landscape</PaperOrientation><Id>LargeAddress</Id><IsOutlined>false</IsOutlined><PaperName>30321 Large Address</PaperName><DrawCommands><RoundRectangle X="0" Y="0" Width="2025" Height="5020" Rx="270" Ry="270" /></DrawCommands><ObjectInfo><AddressObject><Name>Text</Name><ForeColor Alpha="255" Red="0" Green="0" Blue="0" /><BackColor Alpha="0" Red="255" Green="255" Blue="255" /><LinkedObjectName /><Rotation>Rotation0</Rotation><IsMirrored>False</IsMirrored><IsVariable>True</IsVariable><GroupID>-1</GroupID><IsOutlined>False</IsOutlined><HorizontalAlignment>Left</HorizontalAlignment><VerticalAlignment>Middle</VerticalAlignment><TextFitMode>ShrinkToFit</TextFitMode><UseFullFontHeight>True</UseFullFontHeight><Verticalized>False</Verticalized><StyledText><Element><String xml:space="preserve"></String><Attributes><Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" /><ForeColor Alpha="255" Red="0" Green="0" Blue="0" HueScale="100" /></Attributes></Element></StyledText><ShowBarcodeFor9DigitZipOnly>True</ShowBarcodeFor9DigitZipOnly><BarcodePosition>Suppress</BarcodePosition><LineFonts><Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" /></LineFonts></AddressObject><Bounds X="322" Y="57.9999999999999" Width="4613" Height="1882" /></ObjectInfo></DieCutLabel>