微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用Javascript将标签打印到客户端DYMO打印机

如何解决使用Javascript将标签打印到客户端DYMO打印机

经过多次迭代,我得到了以下代码。都不起作用。我是根据直接来自DYMO的示例以及此处发布的其他问题得到的。仍然没有任何工作。当我单击按钮时,没有任何反应。

<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",customer);
        label.SetobjectText("lblorder",order);
        label.Print(printer);

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

更新,此面板包含我要传递给标签模板的所有变量。

<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>

解决方法

我试图查看最新的文档,但是篇幅太小,而且看起来都过时了。在下面,我用<form>元素包装了一个表单(基本上)的外观示例。

您将需要最新版本的dymo.label.framework.js框架才能将DYMO的功能加载到浏览器中。

以下代码基本上与您在上方所做的操作相同,不同之处在于它现在是异步的,这意味着它在尝试计算昂贵的任务时不会阻塞代码,这很好!

使用label创建dymo.label.framework.open仍然是一种猜测。我看过带有dymo.label.framework.openLabelXml的示例和带有图片的示例,因此由您自己决定哪个适合您。同样,由于文档有限,很难知道它的作用。

下面的示例基于DYMO开发人员网站上的this article。我希望这会

<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',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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。