Good evening, I've been doing tests all day and I can't get this to work well for me. In my success
ajax I want it to print a ticket on a thermal printer, I get it to print but my biggest problem at the moment is that it always makes a minimum height of the ticket, that is, it seems that it does it by pages instead of the height of the div that contains the information. What I need is that when the text ends, it finishes printing.
searching the internet i found this
<div id="seleccion">Este texto es lo que se imprimirá cuando se pulse el enlace.</div>
<a href="javascript:imprSelec('seleccion')" >Imprimir texto</a>
<script language="Javascript">
function imprSelec(nombre) {
var ficha = document.getElementById(nombre);
var ventimp = window.open(' ', 'popimpr');
ventimp.document.write( ficha.innerHTML );
ventimp.document.close();
ventimp.print( );
ventimp.close();
}
</script>
If I do it as indicated there, at the moment the text ends, it cuts the paper of the thermal printer.
Ok but I don't want it in a button, I want that when my AJAX response arrives, it prints the ticket. Well if I do the following in my success:
var ficha = document.getElementById('ticketIMP');
var ventimp = window.open(' ', 'popimpr');
ventimp.document.write(ficha.innerHTML);
ventimp.document.close();
ventimp.print( );
ventimp.close();
It seems that the line var ventimp = window.open(' ', 'popimpr');
is not supported for ajax responses in browsers. So if I put it when I click the finish order button before the ajax, it does print the ticket but I can't get it to take styles, not even center the image.
I tried another way, that in this case if it takes the styles but instead it doesn't cut the paper when the text is finished, it always does the same height, as if it were printing by pages.
var printContents = document.getElementById("ticketIMP").innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
At the moment, as I can't get it to work, I print ticketIMP
that it is a simple html, once I get it to work, I will mount the ticket with a variable that brings me the products in the cart.
<div class="ticket ticketIMP text-center contenidoOculto col-12" id="ticketIMP" style="align-content: center; text-align: center; width: 200px;">
<img src="http://ruta/images/logo.png" style="width:140px;";
</div>
My function for when I press finish order
$('.ejecutarEfectivo').on('click', function(e) {
var zona_id = document.getElementById('zonas').value;
var pago = $(this).attr("data-id")
let url = '/pago-efectivo';
$.ajax({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
type: "GET",
url: url,
data: {
'carrito' : carrito,
'total' : total,
'zona_id' : zona_id,
'pago' : pago,
},
success: function (result){
$('#modalEfectivo').modal("hide");
//Aquí es donde en un futuro quiero imprimir el contenido de la variable carrito, de momento aquí es donde ejecuto las pruebas de arriba para que me imprima el contenido de `ticketIMP`
total = 0;
carrito= [];
$('#elPedido').empty();
estado();
actulizaTotal();
$("#formEfectivo")[0].reset();
window.location.hash = '';
}
},
error: function(e) { alert("Error!") }
});
});
Browsers block the use of
window.open(...)
when it does not come from a direct user action. This is why your print doesn't work insidesuccess
of your$.ajax(...)
, as that may end up executing after it$('.ejecutarEfectivo').on('click', ...)
has finished executing (due to asynchrony in javascript).To fix this you should use the parameter
async: false
inside your$.ajax(...)
thing that runs synchronously.This synchronous operation
$.ajax(...)
brings its problems, since the browser will not respond during the execution of the request, in addition to not being compatible with requests to other domains (Cross-domain requests) nor withdataType: "jsonp"
(as I see, neither is your case ).I hope it helps you!