打印到dymo打印机时出现XMLHttpRequest错误

时间:2019-12-05 17:43:35

标签: javascript xml dymo

尝试从JavaScript打印到我的Dymo打印机时收到以下错误:

DYMO.Label.Framework.latest.js:60 [不推荐使用]不赞成在主线程上使用同步XMLHttpRequest,因为它对最终用户的体验有不利影响。

我尝试了多种打印方式,每次仍然出现此错误。

我是XML的新手,也许这就是为什么我遇到麻烦了。

这是我尝试过的代码之一。

HTML:

<div class="print_label">
Name:
Phone:
</div>

JavaScript:

function escapeXml(xmlStr)
{
    var result = xmlStr;
    var findReplace = [[/&/g, "&amp;"], [/</g, "&lt;"], [/>/g, "&gt;"], [/"/g, "&quot;"]];

    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()
{


    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.
            var elements = document.getElementsByClassName("print_label");
            for(i=0;i<elements.length;i++)
            {               
                ////get each Id
                //labelid = hidn_ids_array[i];                    
                var textTextArea = document.getElementById(elements[i].id);// text area id

                if(textTextArea.innerHTML !='')
                {
                    var lines = textTextArea.innerHTML.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);
        }
}

0 个答案:

没有答案