Using Javascript to print labels to client-side DYMO printer

2.3k views Asked by At

I have the following code, after multiple iterations. None of which work. I got this based on examples directly from DYMO as well as other questions posted here. Still nothing is working. When I click the buttons, no reaction whatsoever.

<input id="btnUnitPrint" type="button" value="Print Unit Label" onclick = "printunitLabel()"/><br />
<input id="btnOrderPrint" type="button" value="Print Order Label" onclick = "printorderLabel()"/><br />
<script type="text/javascript">  
    function printunitLabel() {

        var printer = dymo.label.framework.getLabelWriterPrinters()[0].modelName;
        printerName = printer.name; 

        var label = DYMO.Label.Framework.Label.Open("UnitLabel.label");
        var company = document.getElementById('<%= txtcompany.Text %>').value;
        var customer = document.getElementById('<%= txtcustomer.Text %>').value;
        var serial = document.getElementById('<%= txtserial.Text %>').value;
        var unit = document.getElementById('<%= txtunit_manuf.Text + txtunit_model.Text %>').value;
        var warranty = document.getElementById('<%= ddlWarranty.Text %>').value;
        label.SetObjectText("lblcompany", company);
        label.SetObjectText("lblcustomer", customer);
        label.SetObjectText("lblserial", serial);
        label.SetObjectText("lblunit", unit);
        label.SetObjectText("txtWarranty", warranty);

        label.print(printerName);
    }  
    function printorderLabel() {

        var printer = dymo.label.framework.getLabelWriterPrinters()[0].modelName;
        printerName = printer.name;

        var label = DYMO.Label.Framework.Label.Open("OrderLabel.label");
        var company = document.getElementById('<%= txtcompany.Text %>').value;
        var customer = document.getElementById('<%= txtcustomer.Text %>').value;
        var order = document.getElementById('<%= txtorder_id.Text %>').value;
        label.SetObjectText("lblcompany", company);
        label.SetObjectText("lblcustomer", customer);
        label.SetObjectText("lblorder", order);
        label.Print(printer);

        label.print(printerName);
    } 
</script>

UPDATE, this panel contains all of the variables I am looking to pass to the label template.

<asp:Panel ID="confirmation_panel" runat="server" Height="457px" Style="margin-right: 0px" Width="1040px" BorderStyle="Double" BorderWidth="2px">
    <br />
    <table style="width: 469px; height: 385px;">
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="lblorder_id" runat="server" Text="Order ID:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtorder_id" runat="server" ReadOnly="True" Width="231px" Height="22px" ></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="lblunit_id" runat="server" Text="Unit ID:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtunit_id" runat="server" ReadOnly="True" Width="231px" Height="22px" ></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="Label5" runat="server" Text="Company Name:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtcompany" runat="server" ReadOnly="True" Width="231px" Height="22px" NullDisplayText=""></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="Label6" runat="server" Text="Client Name:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtcustomer" runat="server" ReadOnly="True" Width="231px" Height="22px" ></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="lblunit_manuf" runat="server" Text="Unit Manufacturer:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtunit_manuf" runat="server" ReadOnly="True" Width="231px" Height="22px" ></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="lblunit_model" runat="server" Text="Unit Model:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtunit_model" runat="server" ReadOnly="True" Width="231px" Height="22px" ForeColor="Red"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="lblserial" runat="server" Text="Serial Number:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtserial" runat="server" ReadOnly="True" Width="231px" Height="22px" ForeColor="Red"></asp:TextBox>
            </td>
        </tr>
         <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="lblWUserial" runat="server" Text="Whole Unit Serial Number:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:TextBox ID="txtWUserial" runat="server" ReadOnly="True" Width="231px" Height="22px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="modal-sm" style="width: 1231px">
                <asp:Label ID="lblwarranty" runat="server" Text="Warranty Status:"></asp:Label>
            </td>
            <td style="height: 34px; width: 326px;">
                <asp:DropDownList ID="ddlWarranty" Width="231px" Height="22px" runat="server">
                    <asp:ListItem>Warranty Expired</asp:ListItem>
                    <asp:ListItem>Warranty Active</asp:ListItem>
                    <asp:ListItem>N/A</asp:ListItem>
                </asp:DropDownList>
            </td>
        </tr>
        </table>
</asp:Panel>
1

There are 1 answers

7
Emiel Zuurbier On

I've tried to look at the most recent documentation, but there is so little and it all seems very outdated. Below here I've made an example of what your form (basically) should look like, wrapped in a <form> element.

You are going to need the latest version of the dymo.label.framework.js framework to load the functionality of DYMO into the browser.

The code below basically does the same thing as what you tried above with the key difference that it is now asynchronous, meaning it won't block the code when it tries to calculate expensive tasks, which is good!

The creation of the label with dymo.label.framework.open is still kind of a guess. I've seen examples with dymo.label.framework.openLabelXml and one with an image so it is up to you which one is appropriate for you. Again with limited documentation it is hard to know what it does.

The example below is based on this article on the DYMO dev site. I hope that this will

<form id="label-form">
  <input type="text" name="company">
  <input type="text" name="customer">
  <input type="text" name="serial">
  <input type="text" name="unit">
  <input type="text" name="warranty">
  <input id="btnUnitPrint" type="button" value="Print Unit Label"/>
  <input id="btnOrderPrint" type="button" value="Print Order Label"/>
</form>
<!-- Look for the latest dymo.label.framework.js script and place it here -->
<script src="dymo.label.framework.js" type="text/javascript" charset="UTF-8"></script>
<script>
const labelForm = document.getElementById('label-form');
const printUnitButton = document.getElementById('btnUnitPrint');
const printOrderButton = document.getElementById('btnOrderPrint');

function startupCode() {
   
   // Gets all the printers, asynchronously.
   dymo.label.framework.getPrintersAsync().then(function(printers) {

     // Check for available printers.
     if (printers.length === 0) {
       throw Error('No DYMO printers are installed. Install DYMO printers.');
     }
      
     // Select the first printer in the list that has the
     // type of "LabelWriterPrinter" and use that as the printer.
     const printer = printers.find(printer => printer.printerType === "LabelWriterPrinter");

     // Get the name of that printer.
     const printerName = printer.name;

     // Create the label.
     // You'll need to check on your own if this is the way to
     // properly create the label.
     const label = dymo.label.framework.open('OrderLabel.label');

     const printUnit = function printUnit() {
       const formData = new FormData(labelForm);
       const customer = formData.get('customer');
       const company = formData.get('company');
       const serial = formData.get('serial');
       const unit = formData.get('unit');
       const warranty = formData.get('warranty');
       label.setObjectText('lblcompany', company);
       label.setObjectText('lblcustomer', customer);
       label.setObjectText('lblserial', serial);
       label.setObjectText('lblunit', unit);
       label.setObjectText('txtWarranty', warranty);
       label.print(printerName);
     }

     const printOrder = function printOrder() {
       const formData = new FormData(labelForm);
       const customer = formData.get('customer');
       const company = formData.get('company');
       const order = formData.get('order');
       label.setObjectText('lblcompany', company);
       label.setObjectText('lblcustomer', customer);
       label.setObjectText('lblorder', order);
       label.print(printerName);
     }

     // Print unit on printUnitButton click.
     printUnitButton.addEventListener('click', printUnit);
 
     // Print order on printOrderButton click.
     printOrderButton.addEventListener('click', printOrder);   

  });

}

function frameworkInitHelper() {
  dymo.label.framework.init(startupCode);
}
</script>