仅打印div时如何在javascript中强制打印机设置(纸张尺寸)?

时间:2019-01-31 05:45:28

标签: javascript asp.net printing

我的项目的输出中包含正在循环的表。我需要将每个表都打印在4x6英寸的纸上,但是js打印设置始终使用字母大小。单击打印预览按钮时,我已经需要4x6in格式的打印预览。预先感谢。

我已经尝试过CSS,但是JavaScript无法适应CSS设置。

function printDiv(divID) {

    //Get the HTML of div
    var divElements = document.getElementById(divID).innerHTML;
    //Get the HTML of whole page
    var oldPage = document.body.innerHTML;

    //Reset the page's HTML with div's HTML only
    document.body.innerHTML =
        "<html><head><title></title></head><body>" +
        divElements + "</body>";

    //document.getElementById("pagebreak").style.pageBreakBefore = "always";

    //Print Page
    window.print();

    //Restore orignal HTML
    document.body.innerHTML = oldPage;

}

1 个答案:

答案 0 :(得分:0)

添加此样式表

<style>
@page 
{
    size:  auto;   /* auto is the initial value */
    margin: 0mm;  /* this affects the margin in the printer settings */
}

html
{
    background-color: #FFFFFF; 
    margin: 0px;  /* this affects the margin on the html before sending to printer */
}

body
{
    /*border: solid 1px black ;*/
    margin: 5mm 5mm 5mm 5mm; /* margin you want for the content */
}
</style>