18910140161

HTML-在javascript打印预览中未显示图像-堆栈溢出

顺晟科技

2022-10-19 11:25:16

7

我正试图将数据打印到预先格式化的文档中。学生将在文本区回答问题,当点击打印到PDF时,将打印出带有学校名称和徽标的答案。一切都很好,但标志是不预览和打印。我收集了代码。简单的HTML和JavaScript。提前感谢。

以下是完整代码

<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>

顺晟科技:

您正在加载图像之前调用打印。您可以使函数异步和添加之前,您的打印和它的工作。我不知道您决定检查图像是否已加载的最佳方法。您可以使用while循环,每200毫秒左右检查一次要加载的图像。

下面是一个me测试的示例,它使用来自Google的随机图像进行测试。

<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>
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航