I am trying to generate the print functionality of some "text only" information using the "window.print" function.
In both Microsoft Edge and Internet Explorer (IE9 and above) printing a selected text works.
However, in Google Chrome, "although the preview shows the text to print", when sent to the printer, it only shows the empty page.
For the requirement, we are using a "no ink" thermal printer and we only need to print text.
The following code is just a sample of how we are building "in a new page" the information to be printed; but, as I mentioned, in Google Chrome it doesn't print anything.
/* Imprimir el ejemplo. */
function printThisDocument() {
try {
var mywindow = window.open('', 'my div', 'height=600,width=1000');
mywindow.document.write('<html moznomarginboxes mozdisallowselectionprint><head><title>Sticker #1</title>');
mywindow.document.write('</head><body>');
mywindow.document.write(document.getElementById('divOculto').innerHTML.trim());
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
setTimeout(function(){ mywindow.print(); mywindow.close(); }, 500);
} catch (ex) {
alert('Hubo un error al imprimir. Intente de nuevo.');
console.log(ex);
}
}
I have tried selecting "in preview" different paper sizes, but only with the "Letter" size the printout is generated; in this case, I mean that the printer "runs", but does not print text. When selecting other sizes, the printer does not use paper or anything, it does not activate.
The same thing happens if I select any text on any page, that is, I select a text and with a right click I select the "Print" option.
I read that the print preview image in Google Chrome is just that, an image , and since the thermal printer doesn't print images, then it doesn't print anything.
What can be the cause of Google Chrome not being able to print text?
The accepted answer may also be the one that describes working alternatives to plain text printing from the browser .
I ended up implementing QZ Tray 2.0.4 the free version .
By modifying the code available on the test page, I was able to correctly implement the printing of stickers through the browser 1 .
I have adapted the following code from the test page, but there is more information on the official page depending on the type of print to be generated :
1 Tests on Google Chrome Version 60.0.3112.90 (Official Build) (64 bits).