0

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: enter image description here

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

4
  • The content is INSIDE the <pre> tag Commented Feb 7, 2019 at 10:38
  • I added Below it worked for me Commented Feb 7, 2019 at 10:46
  • function print() { var id = document.getElementById("abc"); var printwindow = window.open('', 'PRINT', 'height=400,width=600'); printwindow.document.write('</head><body >'); printwindow.document.write("<pre>" + id.textContent + "</pre>"); printwindow.document.write('</body></html>'); printwindow.document.close(); // necessary for IE >= 10 printwindow.focus(); // necessary for IE >= 10 printwindow.print(); printwindow.close(); } Commented Feb 7, 2019 at 10:53
  • Modify your function print() as I did Below Commented Feb 7, 2019 at 10:54

2 Answers 2

2

You are losing the formatted text because when you open the pop-up, you are appending the text without"pre" tag, which keep the formatted aspect.

You just need to append "pre" tag in the pop-up :

printwindow.document.write('</head><body ><pre>');
printwindow.document.write(id.textContent);
printwindow.document.write('</pre></body></html>');
Sign up to request clarification or add additional context in comments.

Comments

1
<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("<pre>" + id.textContent + "</pre>");
            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>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.