javascript / jquery打印方法设置页边距不起作用

时间:2015-11-18 12:27:37

标签: javascript jquery css margin

我有一个打印页面的功能如下:

$("#btn_print").click(function () {

    var divToPrint = document.getElementById('printdiv');
    var newWin = window.open('', 'Print-Window');

    newWin.document.open();
    newWin.document.write('<html><body onload="window.print()" style="font-family:consolas;@page { size:8.5in 11in; margin: 3cm };" >' + divToPrint.innerHTML + '</body></html>');

    newWin.document.close();
    setTimeout(function () {
        newWin.close();
    }, 10);

});

我的意思是Top,Right, Bottom and Left,我需要 3cm 边距空间。

我正在做的就是你所看到的:

@page { 
    size: 8.5in 11in;
    margin: 3cm;
};

但它不起作用。

1 个答案:

答案 0 :(得分:0)

尝试在<style>中使用此功能:

body {
    margin-top: 3cm;
    margin-bottom: 3cm;
    margin-right: 3cm;
    margin-left: 3cm;
}

或者:

body {
    margin: 3cm 3cm 3cm 3cm;
}

如果上述方法不起作用,请尝试:

body {
    padding: 3cm 3cm 3cm 3cm !important;
}