I currently faced an issue with jsPDF for uniCode support in table generation, to skip this problem I've used browser print feature, by just creating a new html with table and show it as a popup and call popup_window.print()
for it like below:
//exporter.ts
const temp = document.createElement("div");
temp.innerHTML = tableHtml(
tableHead,
tableBody,
fileName
);
const popup_window: any = window.open(location.origin, fileName, "x=y");
popup_window.document.write(temp.innerHTML);
setTimeout(() => {
popup_window.print();
popup_window.close();
}, 1000);
// tableHtml.ts
const tableHtml = (
headers: string[],
records: GeneralOBJ[],
title: string,
direction = "rtl"
) => `<!DOCTYPE html>
<html>
<body>
<main id="main" style="direction: ${direction};">
<h1>${title}</h1>
<div class="app_table">
<table class="table">
<thead>
<tr>
${headers
.map(header => "<th class='col' dir='auto'>" + header + "</th>")
.join("")}
</tr>
</thead>
<tbody>
${records
.map(
record =>
"<tr>" +
Object.values(record)
.map(td => "<td dir='auto'>" + td + "</td>")
.join("") +
"</tr>"
)
.join("")}
</tbody>
</table>
</div>
</main>
</body>
</html>`;
It's working very well, but as you know there are security problems like xss
could happen here. So, I want to use vue for generating this peice of html and then add it as temp.innerHTML
, have any idea how to this?