I created a sample html model for generating html files.
htmlkit.ts
const { isUndefined } = require("lodash");
module.exports = class HTMLKit {
data = [];
curr_x = 0;
curr_y = 0;
fontSize = 'medium';
constructor() { }
x_pos = (spaces) => {
this.data.push(' '.repeat(spaces));
this.curr_x = spaces;
}
y_pos = (lines) => {
this.data.push('<div></div>'.repeat(lines));
this.curr_y = lines;
}
text = (t_data, spaces, lines, fontSize = null) => {
this.data.push(t_data);
}
td = (t_data, t_class = "") => {
this.data.push(`<td class='${t_class}'>${t_data}</td>`)
}
tr = (action) => {
this.data.push((action == 'start') ? '<tr>' : '</tr>');
}
tbl = (action, def = [], tbl_class) => {
var thString = ``;
def.forEach(th => {
thString += `<th class='${th}'></th>`
});
this.data.push((action == 'start') ? `<table class='${tbl_class}'>${thString}` : '</table>');
}
wrap = (action, w_class) => {
this.data.push((action == 'start') ? `<div class='${w_class}'>` : `</div>`)
}
setGlobFontSize = (font_size_index) => {
const fontSizeArr = ['small', 'medium', 'large']
this.fontSize = fontSizeArr[font_size_index];
}
getGlobFontSize = () => {
return `<style>html, body, div, span{font-size: ${this.fontSize} !IMPORTANT;}</style>`;
}
}
html.generator.js
const doc = new DOC();
const styles = STYLES.tr_styles;
doc.text(`<html>${styles}<body>`)
data.forEach(el => {
var p_mode = getPaymentMode(el.paymentmode);
var t_type = getTransType(el.transtype);
doc.setGlobFontSize(0)
doc.wrap('start', 'container')
doc.tbl('start', ["col-1", "col-2", "col-3"], 'tbl-class')
doc.tr('start')
doc.td('some data here')
doc.tr('end')
doc.tbl('end')
doc.wrap('close', 'container')
});
doc.text(`</body></html>`)
textData = doc.data.join('');
fs.writeFileSync(f_name, textData, (err) => {
if (err) throw err;
// console.log('HTML Generated')
});
return { "status": true, "error": "OK" };
html.models.js
module.exports.fd_styles = `<style type="text/css"> @page{size:8in 6in;margin:0}body{font-size:small;margin:0;padding:0}div.container{width:21cm;height:15.24cm;padding:0}table{column-gap:10px;padding:0 45px 0 60px;width:100%;font-size:small}th.dummy-8{width:8%}th.dummy-10{width:10%}th.dummy-20{width:20%}th.dummy-15{width:15%}th.dummy-0-5{width:20%}th.dummy-25{width:25%}th.dummy-30{width:30%}td{height:25px}thead{height:0}.details{position:relative;top:calc(70px + 20px)}.details-1{position:relative;top:calc(70px + 20px)}.details-2{position:relative;top:calc(70px + 15px)}.details-3{position:relative;top:calc(70px + 10px)}.details-4{position:relative;top:calc(70px)}.details-5{position:relative;top:calc(70px + 10px)}.details-6{position:relative;top:calc(70px + 15px)}.center{text-align:center} </style>`;