I am trying to load a text file from server in a html page, and print the content. The text file has specific format, so I can not change it. Below is my sample code :
<html>
<head>
<title>Print test</title>
<script>
var url = './text.txt';
function load() {
fetch(url).then(function(resp) {
resp.text().then(function(text) {
var id = document.getElementById("abc");
id.textContent = text;
});
});
}
function print() {
var id = document.getElementById("abc");
var printwindow = window.open('', 'PRINT', 'height=400,width=600');
printwindow.document.write('</head><body >');
printwindow.document.write(id.textContent);
printwindow.document.write('</body></html>');
printwindow.document.close(); // necessary for IE >= 10
printwindow.focus(); // necessary for IE >= 10
printwindow.print();
printwindow.close();
}
</script>
</head>
<body>
<pre id="abc" style="height:85%;overflow:auto; background:white">
</pre>
<button onclick="load()">Load</button>
<button onclick="print()">Print</button>
</body>
A sample text file is following :
text.txt
NAME = ABC
SURNAME = CDE
OCCUPATION = XYZ
PLACE = UUU
When I click the load button, the text is loaded as it is, but when I try to print in, it is garbled, as shown in the following picture:

Can anybody please tell me what I am doing wrong? Thanks