printing an image using javascript in zebra gc420t

b bhargav venkatesh 3 years 7 months ago
141 1 0

Hello,
I want to print an image using javascript. Which API should i use for printing

var available_printers = null;
var selected_category = null;
var default_printer = null;
var selected_printer = null;
var format_start = "^XA^LL200^FO80,50^A0N36,36^FD";
var format_end = "^FS^XZ";
var default_mode = true;

function setup_web_print()
{
$('#printer_select').on('change', onPrinterSelected);
showLoading("Loading Printer Information...");
default_mode = true;
selected_printer = null;
available_printers = null;
selected_category = null;
default_printer = null;

BrowserPrint.getDefaultDevice('printer', function(printer)
{
default_printer = printer
if((printer != null) && (printer.connection != undefined))
{
selected_printer = printer;
var printer_details = $('#printer_details');
var selected_printer_div = $('#selected_printer');

selected_printer_div.text("Using Default Printer: " + printer.name);
hideLoading();
printer_details.show();
$('#print_form').show();

}
BrowserPrint.getLocalDevices(function(printers)
{
available_printers = printers;
var sel = document.getElementById("printers");
var printers_available = false;
sel.innerHTML = "";
if (printers != undefined)
{
for(var i = 0; i printers.length; i++)
{
if (printers[i].connection == 'usb')
{
var opt = document.createElement("option");
opt.innerHTML = printers[i].connection + ": " + printers[i].uid;
opt.value = printers[i].uid;
sel.appendChild(opt);
printers_available = true;
}
}
}

if(!printers_available)
{
showErrorMessage("No Zebra Printers could be found!");
hideLoading();
$('#print_form').hide();
return;
}
else if(selected_printer == null)
{
default_mode = false;
changePrinter();
$('#print_form').show();
hideLoading();
}
}, undefined, 'printer');
},
function(error_response)
{
showBrowserPrintNotFound();
});
};

function showBrowserPrintNotFound()
{
showErrorMessage("An error occured while attempting to connect to your Zebra Printer. You may not have Zebra Browser Print installed, or it may not be running. Install Zebra Browser Print, or start the Zebra Browser Print Service, and try again.");

};

function sendData(photoURL)
{

var test;
$.ajax({
            url: 'db/zpl.php?photo='+photoURL,
            cache: false,
            contentType: false,
            processData: false,
            type: 'GET',
            success: function(data) {
            test=data;
            console.log("From ZPL:"+data);

            }

        });

var mob="",lap="",other="";
if($("#emobile").val()==="on"){
mob="Mobile";
}

if($("#elaptop").val()==="on"){
lap="Laptop";
}

    if($("#eother").val()==="on"){
other="Other";
}

showLoading("Printing...");
checkPrinterStatus( function (text){
if (text == "Ready to Print")
{

selected_printer.send("^XA N ^XZ");
// selected_printer.send("^XA~DGE:ZLOGO.GRF,2580,030,"+test+"^XZ");
//

selected_printer.send("~DYE:SAMPLE,A,GRF,5000,030"+test+",A");

//selected_printer.send("^XA^BY2^FO165,13^BCN,49,N,N,N^SN00095C00001,1,Y^FS^A0N,28,28^FO490,47^FD17/10/2017^FS^A0N,26,24^FO157,162^FDCOLGATE GYE^FS^A0N,26,24^FO499,13^FDFEC.EMIS^FS^A0N,66,60^FO157,90^SN00099C00001,1,Y^FS^FO150,180^XGE:SAMPLE.GRF,1,1^FS^PQ1,1,1,y^FS^XZ");

// selected_printer.send("^XA^FO100,100^IMR:SAMPLE.GRF^FS^FO100,200^IMR:SAMPLE.GRF^FS^FO100,300^IMR:SAMPLE.GRF^FS^FO100,400^IMR:SAMPLE.GRF^FS^FO100,500^IMR:SAMPLE.GRF^FS^XZ");

//This is the part where i send the parameters and print it
selected_printer.send("^XA^FX Second section with recipient address and permit information. ^CFA,30 ^FO50,30^FDVisitor Name:"+$("#user").val()+"^FS ^FO50,70^FDCompany:"+$("#compName").val()+"^FS  ^FO50,110^FDTo Meet:"+$("#toMeet").val()+"^FS ^FO50,150^FDPurpose:"+$("#reason").val()+"^FS ^FO50,190^FDAuthorise^FS ^FO50,220^FDto Carry:"+mob+" "+lap+" "+other+"^FS ^FO500,50^XGR:SAMPLE.GRF,1,1^FS^XZ");

//selected_printer.send("^XA ^FX Second section with recipient address and permit information. ^CFA,30 ^FO50,30^FDJohn Doe^FS ^FO50,70^FD100 Main Street^FS ^FO50,110^FDSpringfield TN 39021^FS ^FO50,150^FDUnited States (USA)^FS ^XZ");
}
else
{
printerError(text);
}
});
};

function checkPrinterStatus(finishedFunction)
{
selected_printer.sendThenRead("~HQES",
function(text){
var that = this;
var statuses = new Array();
var ok = false;
var is_error = text.charAt(70);
var media = text.charAt(88);
var head = text.charAt(87);
var pause = text.charAt(84);
// check each flag that prevents printing
if (is_error == '0')
{
ok = true;
statuses.push("Ready to Print");
}
if (media == '1')
statuses.push("Paper out");
if (media == '2')
statuses.push("Ribbon Out");
if (media == '4')
statuses.push("Media Door Open");
if (media == '8')
statuses.push("Cutter Fault");
if (head == '1')
statuses.push("Printhead Overheating");
if (head == '2')
statuses.push("Motor Overheating");
if (head == '4')
statuses.push("Printhead Fault");
if (head == '8')
statuses.push("Incorrect Printhead");
if (pause == '1')
statuses.push("Printer Paused");
if ((!ok) && (statuses.Count == 0))
statuses.push("Error: Unknown Error");
finishedFunction(statuses.join());
}, printerError);
};
function hidePrintForm()
{
$('#print_form').hide();
};
function showPrintForm()
{
$('#print_form').show();
};
function showLoading(text)
{
$('#loading_message').text(text);
$('#printer_data_loading').show();
hidePrintForm();
$('#printer_details').hide();
$('#printer_select').hide();
};
function printComplete()
{
hideLoading();
alert ("Printing complete");
}
function hideLoading()
{
$('#printer_data_loading').hide();
if(default_mode == true)
{
showPrintForm();
$('#printer_details').show();
}
else
{
$('#printer_select').show();
showPrintForm();
}
};
function changePrinter()
{
default_mode = false;
selected_printer = null;
$('#printer_details').hide();
if(available_printers == null)
{
showLoading("Finding Printers...");
$('#print_form').hide();
setTimeout(changePrinter, 200);
return;
}
$('#printer_select').show();
onPrinterSelected();
}

function onPrinterSelected()
{
selected_printer = available_printers[$('#printers')[0].selectedIndex];
}

function showErrorMessage(text)
{
$('#main').hide();
$('#error_div').show();
$('#error_message').html(text);
}

function printerError(text)
{
showErrorMessage("An error occurred while printing. Please try again." + text);
}

function trySetupAgain()
{
$('#main').show();
$('#error_div').hide();
setup_web_print();
//hideLoading();
}

Please Register or Login to post a reply

1 Replies

E Efkan YILMAZ

Hi, I'm not sure we understand the question.  Are you asking the best technology or tools to print using Javascript, or specifically which line of this code send the print job to the printer?

CONTACT
Can’t find what you’re looking for?