Skip to content

Commit

Permalink
Base64 encode library
Browse files Browse the repository at this point in the history
  • Loading branch information
erikn69 committed Nov 11, 2024
1 parent afdb6f1 commit 8ea3c05
Show file tree
Hide file tree
Showing 3 changed files with 3,593 additions and 194 deletions.
29 changes: 21 additions & 8 deletions ZplEscPrinter/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,27 +149,30 @@ async function zpl(data){
}
}
}
async function escpos(data){
try{ data = atob(data.trim()); }catch(e){}
async function escpos(data,b64){
let dataAux = data;
try{ dataAux = atob(data.trim()); }catch(e){}

if (!data || !data.trim().length) {
if (!dataAux || !dataAux.trim().length) {
console.warn(`esc/pos = '${data}', seems invalid`);
return;
}

const factor = configs.unit === '4' ? 1 : (configs.unit === '3' ? 379.921465 : (configs.unit === '2' ? 37.9921465 : 96.5));
const width = Math.round(parseFloat(configs.width) * factor * 1000) / 1000;

//console.log(data);
let dataAux = data.replace("\u001B@", '').trim();
//console.log(dataAux);
dataAux = dataAux.replace("\u001B@", '').trim();
if (!dataAux) { //empty
await displayEscPosLabel(btoa('<html style="background-color:#f8f8f8;"><head></head><body style="background-color:white;border:1px #dee2e6 solid;"><div style="text-align:center;"><b>--- EMPTY / NO DATA ---</b><br/><br/><div style="width:100px;display:block;margin-left:auto;margin-right:auto;"><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 115.19 123.38" style="enable-background:new 0 0 115.19 123.38" xml:space="preserve"><style type="text/css">.st0{fill-rule:evenodd;clip-rule:evenodd;stroke:#000000;stroke-width:0.5;stroke-miterlimit:2.6131;}</style><g><path class="st0" d="M93.13,79.5c12.05,0,21.82,9.77,21.82,21.82c0,12.05-9.77,21.82-21.82,21.82c-12.05,0-21.82-9.77-21.82-21.82 C71.31,89.27,81.08,79.5,93.13,79.5L93.13,79.5z M8.08,0.25h95.28c2.17,0,4.11,0.89,5.53,2.3c1.42,1.42,2.3,3.39,2.3,5.53v70.01 c-2.46-1.91-5.24-3.44-8.25-4.48V9.98c0-0.43-0.16-0.79-0.46-1.05c-0.26-0.26-0.66-0.46-1.05-0.46H9.94 c-0.43,0-0.79,0.16-1.05,0.46C8.63,9.19,8.43,9.58,8.43,9.98v70.02h0.03l31.97-30.61c1.28-1.18,3.29-1.05,4.44,0.23 c0.03,0.03,0.03,0.07,0.07,0.07l26.88,31.8c-4.73,5.18-7.62,12.08-7.62,19.65c0,3.29,0.55,6.45,1.55,9.4H8.08 c-2.17,0-4.11-0.89-5.53-2.3s-2.3-3.39-2.3-5.53V8.08c0-2.17,0.89-4.11,2.3-5.53S5.94,0.25,8.08,0.25L8.08,0.25z M73.98,79.35 l3.71-22.79c0.3-1.71,1.91-2.9,3.62-2.6c0.66,0.1,1.25,0.43,1.71,0.86l17.1,17.97c-2.18-0.52-4.44-0.79-6.78-0.79 C85.91,71.99,79.13,74.77,73.98,79.35L73.98,79.35z M81.98,18.19c3.13,0,5.99,1.28,8.03,3.32c2.07,2.07,3.32,4.9,3.32,8.03 c0,3.13-1.28,5.99-3.32,8.03c-2.07,2.07-4.9,3.32-8.03,3.32c-3.13,0-5.99-1.28-8.03-3.32c-2.07-2.07-3.32-4.9-3.32-8.03 c0-3.13,1.28-5.99,3.32-8.03C76.02,19.44,78.86,18.19,81.98,18.19L81.98,18.19z M85.82,88.05l19.96,21.6 c1.58-2.39,2.5-5.25,2.5-8.33c0-8.36-6.78-15.14-15.14-15.14C90.48,86.17,87.99,86.85,85.82,88.05L85.82,88.05z M100.44,114.58 l-19.96-21.6c-1.58,2.39-2.5,5.25-2.5,8.33c0,8.36,6.78,15.14,15.14,15.14C95.78,116.46,98.27,115.78,100.44,114.58L100.44,114.58z"/></g></svg></div></body><html>'));
} else if(dataAux.length===5&&(dataAux.startsWith("\u001Bp")||dataAux.startsWith("\u0010\u0014"))) { //pulse
await displayEscPosLabel(btoa('<html style="background-color:#f8f8f8;"><head></head><body style="background-color:white;border:1px #dee2e6 solid;"><div style="text-align:center;"><b>----- CASH REGISTER PULSE -----</b><br/><br/><div style="width:100px;display:block;margin-left:auto;margin-right:auto;"><svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 490 490" style="enable-background:new 0 0 490 490;" xml:space="preserve"><g><g><g><polygon points="409.1,116.7 367.1,116.7 367.1,87.5 409.1,87.5 409.1,0 262.5,0 262.5,87.5 304.9,87.5 304.9,116.7 79.3,116.7 79.3,206.5 409.1,206.5 "/><path d="M414.9,225.9H75.1L4.3,384.6h481.4L414.9,225.9z M110.4,367.1H76.6c-5.4,0-9.7-4.3-9.7-9.7c0-5.4,4.3-9.7,9.7-9.7h33.8c5.4,0,9.7,4.3,9.7,9.7C120.2,362.8,115.5,367.1,110.4,367.1z M127.6,332.5H98c-5.4,0-9.7-4.3-9.7-9.7s4.3-9.7,9.7-9.7h29.6c5.4,0,9.7,4.3,9.7,9.7S133,332.5,127.6,332.5z M140.8,297.9h-26.1c-5.4,0-9.7-4.3-9.7-9.7s4.3-9.7,9.7-9.7h26.1c5.4,0,9.7,4.3,9.7,9.7S146.2,297.9,140.8,297.9z M151.7,262.9h-23.3c-5.4,0-9.7-4.3-9.7-9.7s4.3-9.7,9.7-9.7h23.3c5.4,0,9.7,4.3,9.7,9.7S157.1,262.9,151.7,262.9z M211.6,367.1h-33.8c-5.4,0-9.7-4.3-9.7-9.7c0-5.4,4.3-9.7,9.7-9.7h33.8c5.4,0,9.7,4.3,9.7,9.7C221.3,362.8,216.6,367.1,211.6,367.1z M215.8,332.5h-29.6c-5.4,0-9.7-4.3-9.7-9.7s4.3-9.7,9.7-9.7h29.6c5.4,0,9.7,4.3,9.7,9.7S220.9,332.5,215.8,332.5z M218.9,297.9h-26.1c-5.4,0-9.7-4.3-9.7-9.7s4.3-9.7,9.7-9.7h26.1c5.4,0,9.7,4.3,9.7,9.7S224.4,297.9,218.9,297.9z M221.7,262.9h-23.3c-5.4,0-9.7-4.3-9.7-9.7s4.3-9.7,9.7-9.7h23.3c5.4,0,9.7,4.3,9.7,9.7S227.1,262.9,221.7,262.9z M268.3,243.4h23.3c5.4,0,9.7,4.3,9.7,9.7s-4.3,9.7-9.7,9.7h-23.3c-5.4,0-9.7-4.3-9.7-9.7S262.9,243.4,268.3,243.4z M261.3,288.2c0-5.4,4.3-9.7,9.7-9.7h26.1c5.4,0,9.7,4.3,9.7,9.7s-4.3,9.7-9.7,9.7H271C265.6,297.9,261.3,293.2,261.3,288.2z M264.4,322.8c0-5.4,4.3-9.7,9.7-9.7h29.6c5.4,0,9.7,4.3,9.7,9.7s-4.3,9.7-9.7,9.7h-29.6C269.1,332.5,264.4,328.2,264.4,322.8z M312.7,367.1h-33.8c-5.4,0-9.7-4.3-9.7-9.7c0-5.4,4.3-9.7,9.7-9.7h33.8c5.4,0,9.7,4.3,9.7,9.7C322.4,362.8,317.7,367.1,312.7,367.1z M328.6,253.2c0-5.4,4.3-9.7,9.7-9.7h23.3c5.4,0,9.7,4.3,9.7,9.7s-4.3,9.7-9.7,9.7h-23.3C332.9,262.9,328.6,258.6,328.6,253.2z M339.5,288.2c0-5.4,4.3-9.7,9.7-9.7h26.1c5.4,0,9.7,4.3,9.7,9.7s-4.3,9.7-9.7,9.7h-26.1C343.8,297.9,339.5,293.2,339.5,288.2z M352.7,322.8c0-5.4,4.3-9.7,9.7-9.7H392c5.4,0,9.7,4.3,9.7,9.7s-4.3,9.7-9.7,9.7h-29.6C357,332.5,352.7,328.2,352.7,322.8z M413.8,367.1H380c-5.4,0-9.7-4.3-9.7-9.7c0-5.4,4.3-9.7,9.7-9.7h33.8c5.4,0,9.7,4.3,9.7,9.7C423.5,362.8,418.8,367.1,413.8,367.1z"/><path d="M0,404.1V490h490v-85.9H0z M245,465.5c-12.8,0-23.3-10.5-23.3-23.3s10.5-23.3,23.3-23.3c12.8,0,23.3,10.5,23.3,23.3S257.8,465.5,245,465.5z"/></g></g></g></svg></div></body><html>'));
} else if(dataAux.length<=4&&(dataAux.startsWith("\u001DV")||dataAux.startsWith("\u001De")||dataAux.startsWith("\u001Bi"))) { //cut-paper
await displayEscPosLabel(btoa('<html style="background-color:#f8f8f8;"><head></head><body style="background-color:white;border:1px #dee2e6 solid;"><div style="text-align:center;"><b>-------- PAPER CUT --------</b><br/><br/><div style="width:100px;display:block;margin-left:auto;margin-right:auto;"><svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="60" id="svg2"> <defs id="defs4"> <marker refX="0" refY="0" orient="auto" id="Scissors" style="overflow:visible"> <path d="M 9.0898857,-3.6061018 C 8.1198849,-4.7769976 6.3697607,-4.7358294 5.0623558,-4.2327734 l -8.2124046,3.0779029 c -2.3882933,-1.3067135 -4.7482873,-0.9325372 -4.7482873,-1.5687873 0,-0.4973164 0.4566662,-0.3883222 0.3883068,-1.6831941 -0.065635,-1.2432767 -1.3635771,-2.1630796 -2.5903987,-2.0816435 -1.227271,-0.00735 -2.499439,0.9331613 -2.510341,2.2300611 -0.09143,1.3063864 1.007209,2.5196896 2.306764,2.6052316 1.5223406,0.2266616 4.218258,-0.6955566 5.482945,1.57086006 -0.9422847,1.73825774 -2.6140244,1.74307674 -4.1255107,1.65607034 -1.2548743,-0.072235 -2.7620933,0.2873979 -3.3606483,1.5208605 -0.578367,1.1820862 -0.0112,2.8646022 1.316749,3.226412 1.3401912,0.4918277 3.1806689,-0.129711 3.4993722,-1.6707242 0.2456585,-1.187823 -0.5953659,-1.7459574 -0.2725074,-2.1771537 0.2436135,-0.32536 1.7907806,-0.1368452 4.5471053,-1.3748244 L 5.6763468,4.2330688 C 6.8000164,4.5467672 8.1730685,4.5362646 9.1684433,3.4313614 L -0.05164093,-0.05372222 9.0898857,-3.6061018 z m -18.3078016,-1.900504 c 1.294559,0.7227998 1.1888392,2.6835702 -0.1564272,3.0632889 -1.2165179,0.423661 -2.7710269,-0.7589694 -2.3831779,-2.0774648 0.227148,-1.0818519 1.653387,-1.480632 2.5396051,-0.9858241 z m 0.056264,8.0173649 c 1.3508301,0.4988648 1.1214429,2.7844356 -0.2522207,3.091609 -0.9110594,0.3163391 -2.2135494,-0.1387976 -2.3056964,-1.2121394 -0.177609,-1.305055 1.356085,-2.4841482 2.5579171,-1.8794696 z" id="schere" style="marker-start:none" /> </marker> </defs> <g transform="translate(0,-992.36218)" id="layer1"> <g transform="matrix(4.2610846,-1.2351263,1.2351263,4.2610846,-1337.7659,-2994.9736)" id="g4593"> <path d="m 59.731665,956.40057 c -0.609784,-1.39286 -2.303491,-1.83556 -3.698902,-1.71226 l -8.742604,0.69566 c -1.935736,-1.91425 -4.307466,-2.2049 -4.132136,-2.81651 0.137044,-0.47806 0.545993,-0.24745 0.837105,-1.51102 0.279511,-1.21323 -0.714709,-2.45509 -1.916471,-2.71488 -1.177727,-0.34526 -2.659814,0.20827 -3.027677,1.45195 -0.447882,1.23061 0.273869,2.69969 1.499535,3.14003 1.400938,0.6374 4.246607,0.49379 4.837778,3.02096 -1.384807,1.41129 -2.993148,0.95525 -4.422137,0.4551 -1.186382,-0.41524 -2.734347,-0.48487 -3.649629,0.53589 -0.881717,0.97694 -0.800158,2.7506 0.376675,3.46434 1.15277,0.8421 3.093263,0.7518 3.824279,-0.64172 0.563472,-1.07414 -0.09119,-1.84242 0.337995,-2.16796 0.32384,-0.24563 1.759155,0.36194 4.749906,-0.0686 l 7.684693,5.46398 c 0.993719,0.61119 2.316503,0.97947 3.577814,0.19164 l -7.902726,-5.8909 9.766502,-0.89574 z m -17.075242,-6.87194 c 1.045256,1.05155 0.403306,2.90727 -0.994512,2.90157 -1.286163,0.072 -2.454591,-1.49318 -1.718425,-2.65375 0.516476,-0.97737 1.997384,-0.96769 2.712937,-0.24782 z m -2.155235,7.72245 c 1.161058,0.85179 0.310724,2.98566 -1.0944,2.9024 -0.962958,0.053 -2.089598,-0.7434 -1.882399,-1.80058 0.188897,-1.30347 1.988129,-2.01427 2.976799,-1.10182 z" id="path4599" style="marker-start:none"/></g></g></svg></div></body><html>'));
} else {
await $.post(atob('aHR0cHM6Ly90ZXN0LnJ1Ynlrcy5jb20vZXNjcG9zL2Jhc2U2NGh0bWwucGhw'), {esc: btoa(data), width: width}).done(function (response) {
await $.post(atob('aHR0cHM6Ly90ZXN0LnJ1Ynlrcy5jb20vZXNjcG9zL2Jhc2U2NGh0bWwucGhw'), {
esc: b64 ? data : base64EncodeUnicode(dataAux), width: width
}).done(function (response) {
//console.log('Data Loaded: ' + response);
displayEscPosLabel(response)
});
Expand All @@ -181,7 +184,7 @@ async function escpos(data){
}
async function displayEscPosLabel (data){
let frame = $('<iframe class="label-esc w-100"></iframe>');
frame.attr('srcdoc', atob(data));
frame.attr('srcdoc', base64DecodeUnicode(data));
$('#label-esc').prepend(frame);
frame.on('load',function(){
//console.log(frame.contents().find("body").height());
Expand Down Expand Up @@ -407,7 +410,7 @@ function initEvents() {
if ($('#isZpl').is(':checked')) {
zpl(base64);
} else {
escpos(base64);
escpos(base64, true);
}
}
});
Expand Down Expand Up @@ -462,6 +465,16 @@ function initConfigs(context) {
}
}
}
function base64EncodeUnicode(str) {
let bytes = new TextEncoder().encode(str);
let binary = String.fromCharCode(...bytes);
return btoa(binary);
}
function base64DecodeUnicode(base64) {
let binary = atob(base64);
let bytes = Uint8Array.from(binary, char => char.charCodeAt(0));
return new TextDecoder().decode(bytes);
}
// Prototype for string/number datatypes
String.prototype.format = function () {
let s = this, i = arguments.length;
Expand Down
6 changes: 3 additions & 3 deletions ZplEscPrinter/manifest.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"manifest_version": 2,
"name": "Zpl Printer",
"short_name": "Zpl Printer",
"name": "Zpl - Esc/Pos Printer",
"short_name": "Zpl/Esc Printer",
"version": "1.6",
"description": "Printer emulator for zpl rendering engine.",
"author": "Simon Binkert",
"author": "ErikN",
"app": {
"background": {
"scripts": [ "js/background.js" ],
Expand Down
Loading

0 comments on commit 8ea3c05

Please sign in to comment.