Last active
November 25, 2016 15:29
-
-
Save devniel/3202bae82cdce23fe0e5ac6b67f460be to your computer and use it in GitHub Desktop.
Printing with Cordova, ZBTPrinter plugin , JsBarcode, Jquery and html2canvas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Extract from my printing strategy with Cordova and my Zbtprinter plugin fork | |
| // https://github.com/devnieL/zbtprinter | |
| var printer = { | |
| address : "[MAC ADDRESS]" | |
| } | |
| $.get('./view/Printing/Templates/Receipt.html', function(template) { | |
| function textToBase64Barcode(text) { | |
| var canvas = document.createElement("canvas"); | |
| JsBarcode(canvas, text, { | |
| displayValues: false | |
| }); | |
| return canvas.toDataURL("image/png"); | |
| } | |
| var vars = { | |
| driver: "Daniel Flores", | |
| barcode: textToBase64Barcode("xxx") | |
| }; | |
| var rendered = Mustache.render(template, vars); | |
| $('<iframe id="print"/>') | |
| .appendTo(document.body) | |
| .contents().find('body').html(rendered); | |
| html2canvas($("#print").contents().find('body'), { | |
| onrendered: function(canvas) { | |
| var myImage = canvas.toDataURL("image/png"); | |
| myImage = myImage.substring(22, myImage.length); | |
| var parts = 10; | |
| var len = myImage.length; | |
| var lenByPart = Math.ceil(myImage.length / 10); | |
| var data = []; | |
| for (var i = 0; i < parts; i++) { | |
| data.push(myImage.substring(lenByPart * i, lenByPart * i + lenByPart)); | |
| } | |
| if (typeof cordova != "undefined") { | |
| console.log("ZBTPrinter printing ..."); | |
| cordova.plugins.zbtprinter.print(printer.address, data, | |
| function(success) { | |
| console.log("ZBTPrinter priting success ... ", success); | |
| $("#print").remove(); | |
| return callback(); | |
| }, | |
| function(fail) { | |
| console.log("ZBTPrinter printing error ... ", fail); | |
| $("#print").remove(); | |
| return callback({ | |
| message: "No se puedo realizar la impresión, inténtelo nuevamente. Detalles : '" + fail + ".'" | |
| }); | |
| }); | |
| } else { | |
| window.open("data:image/png;base64," + data.join("")); | |
| $("#print").remove(); | |
| return callback(); | |
| } | |
| } | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment