使用nodejs从json生成.html文件



我使用Node.js进行自动化工作,我有JSON数据,我想在每隔一段时间后从它们生成html文件。例如,我有一个从数据库检索所有JSON数据的函数,现在我想让它自动生成.html文件到给定JSON数据的路径。我该怎么做呢?有任何节点库或任何例子来做吗?这是我的json:

[
{
id: '5ffee71aef57d4cf197729a5',
date_checked: '2021-02-11T07:56:39.042Z',
title: 'Notification with API',
status: 'created',
reach: null,
sent: null,
delivered: null,
views: null,
clicks: null,
unsubscribers: null
},
{
id: '5ffee71aef57d4cf197729a5',
date_checked: '2021-02-11T07:56:39.042Z',
title: 'Notification with API',
status: 'created',
reach: null,
sent: null,
delivered: null,
views: null,
clicks: null,
unsubscribers: null
}
]

这个脚本为json创建简单的html您可以展开列数组,以添加您希望在表中出现的其他字段。

const fs = require('fs')
const columns = [];
for (let name in json[0]){
if(!json[0].hasOwnProperty(name)) continue;
columns.push(name);
}
const html = '<html><body><table><thead><tr>';
for (let item of columns) {
html += '<th>' + item + '</th>';
}
html += '</tr></thead><tbody>';
for (let item of json) {
html += '<tr>';
for (let name of columns) {
html += '<td>' + item[name] +'</td>';
}
html += '</tr>';
}
html += '</tbody></table></body></html>';
fs.writeFileSync(new Date().getTime().toString() + '.html', html);

我创建了一个示例html模型来生成html文件。

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('&nbsp;'.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>`;

最新更新