【问题标题】:Using Javascript to print labels to client-side DYMO printer使用 Javascript 将标签打印到客户端 DYMO 打印机
【发布时间】:2020-12-21 20:57:10
【问题描述】:

经过多次迭代,我有以下代码。这些都不起作用。我是根据直接来自 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", company);
        label.SetObjectText("lblcustomer", 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>

【问题讨论】:

  • 不要,没必要这么想。您能否将带有companycustomerserial 等的 HTML 添加到您的问题中。让我们移除其中一些 cmets 为新的 cmets 腾出一些空间。 ;)
  • 我用您要求的 HTML 更新了问题。谢谢!

标签: javascript asp.net dymo


【解决方案1】:

我尝试查看最新的文档,但文档太少,而且似乎都已经过时了。下面我做了一个例子,说明你的表单(基本上)应该是什么样子,包裹在 &lt;form&gt; 元素中。

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

下面的代码与您上面尝试的代码基本相同,主要区别在于它现在是异步的,这意味着它在尝试计算昂贵的任务时不会阻塞代码,这很好!

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

【讨论】:

  • 非常感谢!我将在早上尝试这第一件事,如果它有效,我会更新! :)
  • 我在控制台中收到这些错误。 Mixed Content: The page at 'https://localhost:44331/OrderUnitStatus' was loaded over HTTPS, but requested an insecure script 'http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js'. This request has been blocked; the content must be served over HTTPS. 以及 Uncaught ReferenceError: printunitLabel is not defined at HTMLInputElement.onclick (OrderUnitStatus:316)Uncaught ReferenceError: printorderLabel is not defined at HTMLInputElement.onclick (OrderUnitStatus:317)
  • 我仍然不确定我是否按照您所说的 &lt;!-- Look for the latest dymo.label.framework.js script and place it here --&gt; 做正确的事情。我用这个 &lt;script src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js type="text/javascript" charset="UTF-8"&gt;&lt;/script&gt; 替换了那个文本
  • 从脚本中的字符串中删除http: 部分。您的站点是 HTTPS,这意味着它要求所有后续脚本也使用 HTTPS 而不是 HTTP 下载。从字符串中删除协议将导致浏览器检查脚本是否存在 HTTPS 版本或以其他方式回退到 HTTP。
  • 您的 HTML 中有 2 个onclick 属性。从您的输入中删除这些。我上面提供的脚本使用脚本中的addEventListener 来做同样的事情。我这样做是因为我认为这是您从现在开始应该使用事件侦听器的方式。它将使您的 HTML 和 JavaScript 分开,并更容易监听事件。
猜你喜欢
  • 1970-01-01
  • 2015-10-20
  • 2016-03-25
  • 2020-03-19
  • 2018-09-29
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多