0

I am using a dymo printer and this is the code supplied by them to enable web print. I have edited it to have 3 text boxes so I can print 3 labels at once. My issue is: when I press the print button, it only prints the first text box.

What I need is it to print all 3 test boxes. Plus if the code can be edited that if one of the boxes is left blank that the script will skip that one and only print the text boxes that have text in them.

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

SORRY HERE IS THE 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;

} ());

2 Answers2

1

You will need to create some javascript function along the following lines.

var process_labels = function(label_DOM_element){

        var textTextArea = label_DOM_element.getElementById('textTextArea');  

        print_label(textTextArea);
}

Then you will need to change the label JS to be a function which is triggers not which is loaded on body load.. Also you will need to allow for params to be passed in.

So instead of

// prints the label printButton.onclick = function()

Change this to be a function simular to :

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

I wont write the whole thing out for you but basically then you will have a button click function which will check for all the elements of 'textDiv' and pass each of them into the process_label function.

There is a bit of work involved but your javascrtipt currently is only expecting 1 element, so you will need to create a function out of it which can be called on demand.

Pogrindis
  • 7,755
  • 5
  • 31
  • 44
0

Here is my Code to print multiple label. I have added comments for your help. Please go through this and If you have any doubts ask me. I cant include my HTML for your reference here.

//----------------------------------------------------------------------------
//
//  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, "&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()
    {
        //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);
            }
    }
V A S
  • 3,338
  • 4
  • 33
  • 39