我正在使用dymo打印机,这是他们提供的用于启用网页打印的代码。 我编辑它有3个文本框,所以我可以一次打印3个标签。我的问题是:当我按下打印按钮时,它只打印第一个文本框。
我需要打印所有3个测试盒。此外,如果可以编辑代码,如果其中一个框留空,则脚本将跳过该代码并仅打印包含文本的文本框。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Print a Label</title>
<script src = "http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js" type="text/javascript" charset="UTF-8"> </script>
<script src = "PrintLabel.js" type="text/javascript" charset="UTF-8"> </script>
</head>
<body>
<h1>DYMO Label Framework JavaScript Library Samples: Print Label test 2</h1>
<table>
<tr>
<td><p>
<div id="textDiv">
<label for="textTextArea">Label text:</label><br/>
<textarea name="textTextArea" id="textTextArea" rows='5' cols='40'></textarea>
</div>
</p></td>
<td><p>
<div id="textDiv">
<label for="textTextArea">Label text:</label><br/>
<textarea name="textTextArea" id="textTextArea" rows='5' cols='40'></textarea>
</div>
</p></td>
<td><p>
<div id="textDiv">
<label for="textTextArea">Label text:</label><br/>
<textarea name="textTextArea" id="textTextArea" rows='5' cols='40'></textarea>
</div>
</p></td>
</tr>
</table>
<div id="printDiv">
<button id="printButton">Print</button>
</div>
</body>
</html>
在这里烦恼是PrintLable.js
//----------------------------------------------------------------------------
//
// $Id: PreviewAndPrintLabel.js 11419 2010-04-07 21:18:22Z vbuzuev $
//
// Project -------------------------------------------------------------------
//
// DYMO Label Framework
//
// Content -------------------------------------------------------------------
//
// DYMO Label Framework JavaScript Library Samples: Print label
//
//----------------------------------------------------------------------------
//
// Copyright (c), 2010, Sanford, L.P. All Rights Reserved.
//
//----------------------------------------------------------------------------
(function()
{
// called when the document completly loaded
function onload()
{
var textTextArea = document.getElementById('textTextArea');
var printButton = document.getElementById('printButton');
// prints the label
printButton.onclick = function()
{
try
{
// open label
var labelXml = '<?xml version="1.0" encoding="utf-8"?>\
<DieCutLabel Version="8.0" Units="twips">\
<PaperOrientation>Landscape</PaperOrientation>\
<Id>Address</Id>\
<PaperName>30252 Address</PaperName>\
<DrawCommands/>\
<ObjectInfo>\
<TextObject>\
<Name>Text</Name>\
<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
<BackColor Alpha="0" Red="255" Green="255" Blue="255" />\
<LinkedObjectName></LinkedObjectName>\
<Rotation>Rotation0</Rotation>\
<IsMirrored>False</IsMirrored>\
<IsVariable>True</IsVariable>\
<HorizontalAlignment>Left</HorizontalAlignment>\
<VerticalAlignment>Middle</VerticalAlignment>\
<TextFitMode>ShrinkToFit</TextFitMode>\
<UseFullFontHeight>True</UseFullFontHeight>\
<Verticalized>False</Verticalized>\
<StyledText/>\
</TextObject>\
<Bounds X="332" Y="150" Width="4455" Height="1260" />\
</ObjectInfo>\
</DieCutLabel>';
var label = dymo.label.framework.openLabelXml(labelXml);
// set label text
label.setObjectText("Text", textTextArea.value);
// select printer to print on
// for simplicity sake just use the first LabelWriter printer
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0)
throw "No DYMO printers are installed. Install DYMO printers.";
var printerName = "";
for (var i = 0; i < printers.length; ++i)
{
var printer = printers[i];
if (printer.printerType == "LabelWriterPrinter")
{
printerName = printer.name;
break;
}
}
if (printerName == "")
throw "No LabelWriter printers found. Install LabelWriter printer";
// finally print the label
label.print(printerName);
}
catch(e)
{
alert(e.message || e);
}
}
};
// register onload event
if (window.addEventListener)
window.addEventListener("load", onload, false);
else if (window.attachEvent)
window.attachEvent("onload", onload);
else
window.onload = onload;
} ());
答案 0 :(得分:1)
您需要在以下行中创建一些javascript函数。
var process_labels = function(label_DOM_element){
var textTextArea = label_DOM_element.getElementById('textTextArea');
print_label(textTextArea);
}
然后你需要将标签JS更改为一个函数,它是一个触发而不是加载到body load上的函数。你还需要允许传入params。
所以而不是
// prints the label
printButton.onclick = function()
将此更改为类似的函数:
var print_label = function(textTextArea){
{
// open label
var labelXml = '<?xml version="1.0" encoding="utf-8"?>\
<DieCutLabel Version="8.0" Units="twips">\
<PaperOrientation>Landscape</PaperOrientation>\
<Id>Address</Id>\
<PaperName>30252 Address</PaperName>\
<DrawCommands/>\
<ObjectInfo>\
<TextObject>\
<Name>Text</Name>\
<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
<BackColor Alpha="0" Red="255" Green="255" Blue="255" />\
<LinkedObjectName></LinkedObjectName>\
<Rotation>Rotation0</Rotation>\
<IsMirrored>False</IsMirrored>\
<IsVariable>True</IsVariable>\
<HorizontalAlignment>Left</HorizontalAlignment>\
<VerticalAlignment>Middle</VerticalAlignment>\
<TextFitMode>ShrinkToFit</TextFitMode>\
<UseFullFontHeight>True</UseFullFontHeight>\
<Verticalized>False</Verticalized>\
<StyledText/>\
</TextObject>\
<Bounds X="332" Y="150" Width="4455" Height="1260" />\
</ObjectInfo>\
</DieCutLabel>';
ETC ETC...
}
我不会为你写完整件事,但基本上你会有一个按钮点击功能,它会检查'textDiv'的所有元素并将它们传递给process_label
函数。
有一些工作涉及但你的javascrtipt目前只期望1个元素,所以你需要创建一个可以按需调用的函数。
答案 1 :(得分:0)
这是我打印多个标签的代码。我已经为您的帮助添加了评论。请仔细阅读,如果您有任何疑问请问我。我不能在此处提供我的HTML供您参考。
//----------------------------------------------------------------------------
//
// PrintMultipleLabel.js 2014-11-07 : Vineesh K S
//
// Content -------------------------------------------------------------------
//
// DYMO Label Framework JavaScript Library :
// Print Single or multiple label
// Mark-up Added
//
//----------------------------------------------------------------------------
//
// Copyright (c), 2010, Sanford, L.P. All Rights Reserved.
//
//----------------------------------------------------------------------------
function escapeXml(xmlStr)
{
var result = xmlStr;
var findReplace = [[/&/g, "&"], [/</g, "<"], [/>/g, ">"], [/"/g, """]];
for(var i = 0; i < findReplace.length; ++i)
result = result.replace(findReplace[i][0], findReplace[i][1]);
return result;
}
// call this function on onclick function of print button
function printLabel()
{
//comma separated values of record IDs
var hidn_ids_array = $('#hidn_ids').val().split(",");
// if text area is null
var labelPrint_val = $('#labelPrint').val();
if(labelPrint_val == ""){
alert("Please enter values to print label");
$( "#labelPrint" ).focus();
return;
}
try
{
// open label
var labelXml = '<?xml version="1.0" encoding="utf-8"?>\
<DieCutLabel Version="8.0" Units="twips">\
<PaperOrientation>Landscape</PaperOrientation>\
<Id>Address</Id>\
<PaperName>30252 Address</PaperName>\
<DrawCommands/>\
<ObjectInfo>\
<TextObject>\
<Name>Text</Name>\
<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
<BackColor Alpha="0" Red="255" Green="255" Blue="255" />\
<LinkedObjectName></LinkedObjectName>\
<Rotation>Rotation0</Rotation>\
<IsMirrored>False</IsMirrored>\
<IsVariable>True</IsVariable>\
<HorizontalAlignment>Center</HorizontalAlignment>\
<VerticalAlignment>Middle</VerticalAlignment>\
<TextFitMode>ShrinkToFit</TextFitMode>\
<UseFullFontHeight>True</UseFullFontHeight>\
<Verticalized>False</Verticalized>\
<StyledText/>\
</TextObject>\
<Bounds X="332" Y="150" Width="4455" Height="1260" />\
</ObjectInfo>\
</DieCutLabel>';
var label = dymo.label.framework.openLabelXml(labelXml);
if (!label)
{
alert("Load label before printing");
return;
}
// set data using LabelSet and text markup
var labelSet = new dymo.label.framework.LabelSetBuilder();
var textMarkup = '';
var fontSize = 18; // sets font size of first line
// loop started for adding multiple record.
$.each(hidn_ids_array,function(i)
{
////get each Id
labelid = hidn_ids_array[i];
var textTextArea = document.getElementById('labelPrint'+labelid);// text area id
if(textTextArea.value !='')
{
var lines = textTextArea.value.split('\n');
// adding markup
var boldLinesCount = lines.length <= 3 ? 1 : 2;
// if no. of lines is more than 3 then apply style to first 2 lines.
if (lines.length > 0)
{
textMarkup = '<b><font family="Arial" size="' + fontSize + '">';
textMarkup += escapeXml(lines.slice(0, boldLinesCount).join('\n'));
textMarkup += '</font></b><br/>';
textMarkup += escapeXml(lines.slice(boldLinesCount).join('\n'));
}
/////////////add record to printer object////////////////
//alert(textMarkup);
var record = labelSet.addRecord();
record.setTextMarkup('Text', textMarkup); // set label text
}
});
// select printer to print on
var printers = dymo.label.framework.getPrinters();
if (printers.length == 0)
throw "No DYMO printers are installed. Install DYMO printers.";
var printerName = "";
for (var i = 0; i < printers.length; ++i)
{
var printer = printers[i];
if (printer.printerType == "LabelWriterPrinter")
{
printerName = printer.name;
break;
}
}
if (printerName == "")
throw "No LabelWriter printers found. Install LabelWriter printer";
// print the label
label.print(printerName, null, labelSet.toString());
}
catch(e)
{
alert(e.message || e);
}
}