2013-04-03 27 views
7

Voglio solo chiamare l'API della stampante da una pagina web. e semplicemente voglio stampare alcune cose in esso. Ho fatto finora.Come stampare nella stampante DymoLabel usando javascript?

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

Ma qui non accadrà nulla, È necessario importare o includere qualcosa? Si prega di dare qualche suggerimento ..

+0

quando si esegue il codice mi mostra ReferenceError Uncaught: DYMO non è definito sample_dymo.html: 40 GetDYMOPrinters sample_dymo.html: 40 onclick – newBike

risposta

6

Il tuo <script> -Tag per incorporare il "DYMO.Label.Framework.latest.js" è all'interno di un altro tag script. Sposta e le tue funzioni dovrebbero essere eseguite:

<!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> 
    ... 
+0

Grazie ho avuto le funzionalità di lavorare ... Do You Know come chiamare la stampante per stampare testo o etichette. Ho installato tutte le cose Dymo. –

+1

Ho cercato un po 'e ho trovato questa pagina di esempio: http://labelwriter.com/software/dls/sdk/samples/js/PreviewAndPrintLabel/PreviewAndPrintLabel.html Guarda PreviewAndPrintLabel.js, in pratica carica un set XML etichetta il testo dell'indirizzo e lo stampa. – robbi5

+0

Grazie mille, lo esaminerò. –

2

Ecco il mio codice per stampare più etichette. Ho aggiunto commenti per il tuo aiuto. Per favore passa questo e se hai dei dubbi chiedimi. Non posso includere il mio codice HTML come riferimento qui.

//---------------------------------------------------------------------------- 
// 
// 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); 
      } 
    } 
+0

puoi dirmi come aggiungere un codice a barre a quanto sopra? –

+0

per favore controlla questo URL: http://developers.dymo.com/2010/07/21/how-to-set-data-on-your-label-using-the-sdk/ –

+0

Sto impostando un modello XML per stampa la mia etichetta Dymo. Sta stampando un codice a barre che è l'indirizzo, credo. Come faccio a stampare un valore specifico, come un ID che posso passare alla funzione di stampa? –

Problemi correlati