0
votes

I am trying to print data to a pre formatted document. Student will answer the questions in text area, when click on print to PDF I will print the answer with school name and logo. Everything working fine but logo is not previewing and printing. I collected the code. Simple HTML and JavaScript. Thanks in advance.

Here is the complete code

<html>
<head>
    <title>Online Test</title>
</head>
<body>
    <div style="padding: 10px 0;">
        <h1>Online Test</h1>
    </div>
    <!--Data entry section.-->
    <div id="container_data_entry">
         <h2 id="subject"><b>Subject - Computer Science</b></h2> <br />

        <div id="container" style="width:100%;overflow:auto;">
            <ul>
                <li><b>Q No. 1:</b>Question one?</li>
                <li><textarea id="a1"></textarea></li>
            </ul>
            <ul>
                <li><b>Q No. 2:</b>Question two?</li>
                <li><textarea id="a2"></textarea></li>
            </ul>
            <ul>
                <li><b>Q No. 3:</b> Question three?</li>
                <li><textarea id="a3"></textarea></li>
            </ul>
        </div>
        <div style="text-align:center;">
            <input type="button" style="" value="Print to PDF" id="btPrint" onclick="onlineTestApp.printPage();" />
        </div>
    </div>
</body>
<script>
    let onlineTestApp = new function () {
        this.printPage = function () {
            let style = "<style>";
            style = style + "h1, h2 {text-align:center; font:22px Times New Roman; font-weight:bold;}";
            style = style + ".answers {font:18px Calibri; padding:10px 0;}";
            style = style + "</style>";   
            let header ='<img src="logo.png" width="100px" height="100px"/>' +
            '<h1>School Name</h1>' + '<h2>Online Test</h2>';
            let theBody = '';
            // get all textarea (anwsers).
            let ele_tArea = document.getElementsByTagName('textarea');
            for (let i = 0; i <= ele_tArea.length - 1; i++) {
                if (theBody === '') {
                    if (ele_tArea[i].value != '') {
                        theBody = '<p class="answers"> <b>Answer ' + (i + 1) + '</b> - ' + ele_tArea[i].value + '</p>';
                    }
                }
                else {
                    if (ele_tArea[i].value != '') {
                        theBody = theBody + '<p class="answers"> <b>Answer ' + (i + 1) + '</b> - ' + ele_tArea[i].value + '</p>';
                    }
                }
            }
            theBody = header + theBody;
            // Create window object and print the data.
            let  newWin = window.open('', '', '');

            newWin.document.write(style);
            newWin.document.write(theBody);
            newWin.print();
            newWin.close();
        }
    }
</script>
</html>