1. Use scenarios
After the user does something in the browser, he will automatically print the receipt.
2. Test method
2.1 JavaScript implementation
After trying many methods, a pop-up box will eventually appear, allowing the user to choose the printer. Not meeting our needs.
2.2 lodop
It has relatively powerful functions, but it is charged. Not considered yet.
2.3 PAZU
It has a powerful feature, free license. Very good!
Details: http://www.4fang.net/article/tech/pazu_tprinter.html
Implementation process:
2.3.1 Print the details page
<!doctype html><html><head><meta charset="utf-8"><title>Small ticket</title><style>body {margin: 0px;padding: 0px;font-size: 11px;}hr{width: 100%; border: 1px dashed black;}/*********************************************/.table .title{font-size:14px;}.table{width:100%;}.table .left{text-align:right;}</style></head><body><table><tr><td align="center">ABC School</td></tr><tr><td align="center">Small Ticket</td></tr><tr><td><hr size="1" /></td></tr></table><table><caption><col><col></caption><tbody><td>Sign-in time: </td><td>October 19, 2015 15:30</td></tr><td>Teacher name: </td><td>Zhou Shen</td></tr><tr><td>Class: </td><td>Children's class</td></tr><tr><td>School name: </td><td>ABC College</td></tr><tr><td colspan="2"><hr size="1" /></td></tr></tbody></table><table><caption><col><col></caption><tbody><td>Card information:</td><td>Quotes/60 times</td></tr><td>Card balance:</td><td>32 times</td></tr><tr><td>Expiration date:</td><td>Indefinite</td></tr><tr><td>Activation date:</td><td>2015-09-08</td></tr><tr><td colspan="2"><hr size="1" /></td></tr></tbody></table><table><tr><td align="center">Thank you for your patronage! <br/>Please keep the receipt. If you have any questions, please show it, thank you! </td></tr><tr><td align="center"><img src="code.png"/><br/>Scan the code to check the details</td></tr></table></body></html>2.3.2 Business Page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head lang="en"><meta charset="UTF-8"><title></title><script src="printReceipt.js"></script></head><body><iframe frameborder="0" scrolling="scrolling" src="receipt.html" name="myifrm" id="myifrm" style="height: 0;width: 323px;"></iframe><input name="Button1" type="button" value="Try printing!" onclick="print(1);"/></body><script>initPrintActiveX();</script></html>
2.3.3 JS introduction
/*** Created by hery on 2016/7/25.*/function initPrintActiveX(){var div_ = document.createElement('div');div_.style="display:none";var object_=document.createElement('object');object_.setAttribute("classid","clsid:AF33188F-6656-4549-99A6-E394F0CE4EA4");object_.setAttribute("codebase","http://www.4Fang.net/4ff/sc_setup.exe");object_.setAttribute("id","pazu");object_.setAttribute("name","pazu");var param_=document.createElement('param');param_.setAttribute("name","License");param_.setAttribute("value","2AE816BA3A24A9BA3F01162E7BF420F4");object_.appendChild(param_);div_.appendChild(object_);document.getElementsByTagName("body")[0].appendChild(div_);}//Preselected paper var strDefaultPaper = 'A4';//1.Load the printer and paper list function init() {listPrinters();//listPapers();//2.Page loading prints immediately//demo();}//list all printers function listPrinters() {var ps = pazu.TPrinter.getPrinters();//get is a string separated by a carriage return line break// alert(ps);var pa = ps.split("/r/n");for (var i = 0; i < pa.length; i++) {//alert(pa[i]);var Op = document.createElement("option");Op.text = pa[i];Op.value = pa[i];try {printers.add(Op);} catch (ex) {alert(ex.message)}}}}function print(num) {/*Skip IE printing selection prompt*////The default printer name is set to: GP-58120 Seriesvar isPromtUser = false;/*Page settings*/var sPaper = "A4";// var sPrinter = printers.value;var sPrinter = "GP-58120 Series";pazu.TPrinter.marginTop = 1; //The margin of the property is pazu.TPrinter.marginBottom = 14; //The margin of the property is pazu.TPrinter.marginLeft = 7; //Property left margin pazu.TPrinter.marginRight = 8; //Property right margin pazu.TPrinter.footer = ""; //Property footer pazu.TPrinter.header = ""; //Property header pazu.TPrinter.orientation = 1; //Property integer: Paper direction 1 = portrait 2 = horizontal pazu.TPrinter.paperName = sPaper; //Property paper size name pazu.TPrinter.printName = sPrinter; //Property printer name pazu.TPrinter.isPrintBackground = false; //Property whether the property prints the background true / falsepazu.TPrinter.isZoomOutToFit = true; // Whether the attribute is scaled to fit the size print true / false//pazu.TPrinter.printTemplate = sPT; //The URL of the attribute printing template pazu.TPrinter.copies = num; //The number of copies of the attribute printing//pazu.TPrinter.range = range.value; //The attribute page range pazu.TPrinter.isCopyByCopy = false; //The attribute is complete and print the next copy true //Pazu.TPrinter.getDefaultPrinter //The method gets the object of the default printer//pazu.TPrinter.printToDefaultPrinter //The method sends the string to be printed to the default printer (with getDefaultPrinter (with getDefaultPrinter Use) //pazu.TPrinter.getPaperForms //Method returns a list of all paper formats, split in vbCrlf //pazu.TPrinter.getPrinters //Method returns a printer list, split in vbCrlf //Pazu.TPrinter.createPaper //Method creates custom paper according to the specified width and height, please see the example //pazu.TPrinter.doPrint //Method executes printing //Pazu.TPrinter.doPrint_ //Method executes printing but does not perform page parameter setting //pazu.TPrinter.doPreview //Method prints preview //Pazu.TPrinter.doPageSetup //Method executes page parameter setting //Pazu.TPrinter.showPageSetup //Method pops up the page settings window //pazu.TPrinter.writeHTMLtoOfficeFile method exports HTML to Office EXCEL or Word format file //To specify which Frame to print, just use javascript to get focus //Note: In this way, it cannot be previewed, and can only be printed immediately. Otherwise, the preview sees the entire web page, not the specified framewindow.frames['myifrm'].focus();//pazu.TPrinter.doPreview();//Print preview pazu.TPrinter.doPrint(isPromtUser);}String.prototype.trim = function () {return this.replace(/(^/s*)|(/s*$)/g, "");}function isNum(n) {if (isNaN(n))return false;return true;}The above is a browser-based automatic ticket machine printing implementation solution (js version) introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!