How to get to print in DymoLabel printer using javascript?
Asked Answered
R

2

8

I just want to call the API's of Printer from a web page . and simply wanna print some stuff in it . I have done so far .

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
    <head>
        <title>Sample DYMO Label Plug-In</title>


            <script src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js"
            type="text/javascript" charset="UTF-8">


            function OnLoad()
            {
                //GetCurrentPlugin();
                GetDYMOPrinters();

                //GetPaperTray();
                //GetMRUList();
                //GetObjectNames();
                //GetLabelImage();
            }


            function GetDYMOPrinters()
            {
                alert(" Testing 1");

                var printers = dymo.label.framework.getPrinters();
                if (printers.length == 0)
                throw "No DYMO printers are installed. Install DYMO printers.";
                alert("dfdsfd");
                var printerName = "";
                for (var i = 0; i < printers.length; ++i)
                {
                    var printer = printers[i];
                    if (printer.printerType == "LabelWriterPrinter")
                    {
                        printerName = printer.name;
                        break;
                    }
                }

            var label = DYMO.Label.Framework.Label.Open("MyText.label");
            label.SetObjectText("NameTxt", "John Smith");

            alert(" Testing 2");   // Here this alert also not working .
            label.print("DYMO LabelWriter 310");
            }
     </script>

    </head>
    <body onload="OnLoad()">
         <form action="" method="post" id="DYMOLabel">
        <center>
        <h2>DYMO Label  Example</h2>
         <input type=button value="Get DYMO Printers" onClick="GetDYMOPrinters()">
     </center>
    </form>




    </body>
</html>

But here nothing will happen , Is I need to import or include anything.? please give some suggestion ..

Respondent answered 3/4, 2013 at 11:2 Comment(1)
when I run your code it shows me Uncaught ReferenceError: DYMO is not defined sample_dymo.html:40 GetDYMOPrinters sample_dymo.html:40 onclickLenin
C
5

Your <script>-Tag for embedding the "DYMO.Label.Framework.latest.js" is inside another script tag. Move it out and your functions should run:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
    <title>Sample DYMO Label Plug-In</title>
    <!-- LabelWriter-API first -->
    <script src="http://labelwriter.com/software/dls/sdk/js/DYMO.Label.Framework.latest.js"></script>
    <!-- your script second -->
    <script>
     ...
Curiel answered 3/4, 2013 at 11:8 Comment(3)
Thanks I got the functionalities to work ... Do You know how to call the printer to print some text or labels. I have installed all Dymo stuff.Respondent
I searched a little and found this sample Page: labelwriter.com/software/dls/sdk/samples/js/… Look at the PreviewAndPrintLabel.js, basically it loads a label-XML sets the address text and prints it.Curiel
Thank you very much , I will go through that.Respondent
S
2

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);
            }
    }
Shellacking answered 7/11, 2014 at 6:27 Comment(3)
please check this URL : developers.dymo.com/2010/07/21/…Shellacking
I am setting an XML template to print my Dymo label. It is printing a barcode that is the address, I think. How do I set it print a specified value, like an ID that I can pass to the print function?Historicism
you should pass that value into the text area (here : #labelPrint )Shellacking

© 2022 - 2024 — McMap. All rights reserved.