javascript - 使用 window.print() 强制重复的 printElement 转到页面底部;

标签 javascript jquery html css

我打算进行重复打印。上半部分是员工文案,下半部分是用户文案。一旦打印出来,我们就把它剪成两半。我们如何做到这一点? window.print(); 可以做到这一点吗?

这是我正在使用的脚本。

    function PrintAppendChangeScheduleButton() {
        printElement(document.getElementById("divID")); //Specify the DIV to be printed.

        function printElement(elem) {
            var forDOMClone = elem.cloneNode(true);
            var $forSECTIONPrint = document.getElementById("forSECTIONPrint"); //For Section Specific Print
            if (!$forSECTIONPrint) {
                var $printSection = document.createElement("div"); //For DIV Specific Print
                $forSECTIONPrint.id = "forSECTIONPrint";
                document.body.appendChild($forSECTIONPrint);
            } else {
                $forSECTIONPrint.innerHTML = "";
                $forSECTIONPrint.appendChild(forDOMClone);
                window.print();
                return true;
            }
        }
    }

我尝试复制 elem.cloneNode(true);,但它没有正确排列。

这就是我现在正在做的事情。

    function PrintAppendChangeScheduleButton() {
        printElement(document.getElementById("divID")); //Specify the DIV to be printed.

        function printElement(elem) {
            var forDOMClone = elem.cloneNode(true);
            var forDOMCloneCUT = elem.cloneNode(true);
            var $forSECTIONPrint = document.getElementById("forSECTIONPrint"); //For Section Specific Print
            if (!$forSECTIONPrint) {
                var $printSection = document.createElement("div"); //For DIV Specific Print
                $forSECTIONPrint.id = "forSECTIONPrint";
                document.body.appendChild($forSECTIONPrint);
            } else {
                $forSECTIONPrint.innerHTML = "";
                $forSECTIONPrint.appendChild(forDOMClone);
                $forSECTIONPrint.appendChild(forDOMCloneCUT);
                window.print();
                return true;
            }
        }
    }

这是当前的打印状态。

current print

这就是我正在寻找的结果。

有没有办法让 javascript 强制 $forSECTIONPrint.appendChild(forDOMCloneCUT); 转到页面的最底部?

duplicate print

最佳答案

我一起破解了一些东西。

首先,我们需要知道一张A4纸的尺寸是210mm x 297mm。我从here得到的.

接下来,我们需要将高度 (297mm) 转换为像素。我们这样做 here并得到 1122.5px

现在我们需要测量你要打印两次的div的高度,看两次div的高度是否小于一张A4纸的尺寸。如果是,那么我们在两个克隆之间创建一个空的 div,并为其指定克隆之后空白区域的高度。

所以这是你修改后的代码:

* {
margin: 0; 
padding: 0
}
<div id='divID' style="border: 2px black solid; padding-bottom: 200px">
    <h1>CONTENT FROM THIS PAGE IS FROM printElement(document.getElementById('div1'))</h1>
</div>
<div id='forSECTIONPrint'></div>
    <script type="text/javascript">
        window.onload = PrintAppendChangeScheduleButton;
        function PrintAppendChangeScheduleButton() {
            printElement(document.getElementById("divID")); //Specify the DIV to be printed.

            function printElement(elem) {
                var forDOMClone = elem.cloneNode(true);
                var forDOMCloneCUT = elem.cloneNode(true);
                var $forSECTIONPrint = document.getElementById("forSECTIONPrint"); //For Section Specific Print
                if (!$forSECTIONPrint) {
                    var $printSection = document.createElement("div"); //For DIV Specific Print
                    $forSECTIONPrint.id = "forSECTIONPrint";
                    document.body.appendChild($forSECTIONPrint);
                } else {
                    $forSECTIONPrint.innerHTML = "";
                    var elemHeight = elem.offsetHeight;
                    elem.style.display = 'none';
                    var emptySpace = document.createElement('div');
                    $forSECTIONPrint.appendChild(forDOMClone);
                    $forSECTIONPrint.appendChild(emptySpace);
                    //if there's any empty space at the bottom of the page, then set the height of the
                    //empty div in between the clones with that space height
                    if (1122.5 - (elemHeight * 2) > 0){
                        setTimeout(function(){
                            emptySpace.style.height = 1122.5 - (elemHeight * 2) + 'px';
                            window.print();
                        },100);
                    }
                    //if there's no empty space, just print right away
                    else {
                        window.print();
                    }
                    $forSECTIONPrint.appendChild(forDOMCloneCUT);
                    return true;
                }
            }
        }
    </script>

关于javascript - 使用 window.print() 强制重复的 printElement 转到页面底部;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260092/

相关文章:

javascript - 如何将对象数组与 sinon.js 匹配?

javascript - 如何根据文本字段中输入的值禁用剩余的输入字段?

javascript - 以编程方式触发更改事件时为事件处理程序传递附加信息

javascript - 如何在 JavaScript 中定位下面的 div 的子元素

html - 如何在表单提交按钮中制作不是图像的按钮外观按钮?

Javascript 在悬停时播放声音。在悬停时停止并重置

javascript - 一个对象有可能也是一个函数吗?

javascript - Google Script 中的 onEdit(e) - 尝试将最后编辑日期放入单元格并在某些工作表(选项卡)上排除此功能

javascript - 单击外部 div - 当 div 具有触发事件的按钮时 - angularjs

javascript - 如何在 <button> 中将 <div> 居中?