javascript 将 div 拉伸到 A4 大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20446252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:30:59  来源:igfitidea点击:

Stretch div to A4 size

javascripthtml

提问by Hwende

I have the following function which works great.

我有以下功能很好用。

<script type="text/javascript">
function printDiv(printpage)
{
var headstr = "<html><head><title></title></head><body>";
var newstr = document.all.item(printpage).innerHTML;
var oldstr = document.body.innerHTML;
document.body.innerHTML = headstr+newstr;
window.print();
document.body.innerHTML = oldstr;
return false;
}
</script>

However my div is about half of the paper size. Is there a way to make my div stretch to a full paper size when the function is called?

但是我的 div 大约是纸张大小的一半。有没有办法在调用函数时让我的 div 拉伸到完整的纸张大小?

回答by souvickcse

You can set the div size by css to fit it in a A4 size paper CSS:

您可以通过 css 设置 div 大小以适应 A4 大小的纸张 CSS:

   body {
    margin: 0;
    padding: 0;
    background-color: #FAFAFA;
    font: 12pt "Tahoma";
}
* {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}
.page {
    width: 21cm;
    min-height: 29.7cm;
    padding: 2cm;
    margin: 1cm auto;
    border: 1px #D3D3D3 solid;
    border-radius: 5px;
    background: white;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
}
.subpage {
    padding: 1cm;
    border: 5px red solid;
    height: 237mm;
    outline: 2cm #FFEAEA solid;
}

@page {
    size: A4;
    margin: 0;
}
@media print {
    .page {
        margin: 0;
        border: initial;
        border-radius: initial;
        width: initial;
        min-height: initial;
        box-shadow: initial;
        background: initial;
        page-break-after: always;
    }
}

HTML:

HTML:

<div class="book">
<div class="page">
    <div class="subpage">Page 1/2</div>    
</div>
<div class="page">
    <div class="subpage">Page 2/2</div>    
</div>
</div>

DEMO:http://jsfiddle.net/2wk6Q/3/

演示:http: //jsfiddle.net/2wk6Q/3/