【问题标题】:Change Window.print() paper orientation更改 Window.print() 纸张方向
【发布时间】:2016-12-25 20:03:12
【问题描述】:

我想更改窗口打印的纸张模式(方向)。我想以编程方式更改它,但我找不到任何东西。

window.print()

但我不知道,我该怎么做。

@media print{@page {size: landscape}}

我不需要它。

    function printWindow()
    {
        window.print({
        /*
        some code here?
        */
        });
}

【问题讨论】:

  • 我可能是错的,但我不认为这是你能做到的。纸张大小和方向由用户决定。

标签: javascript jquery css node.js twitter-bootstrap


【解决方案1】:

您需要为文档注入样式。

var css = '@page { size: landscape; }',
    head = document.head || document.getElementsByTagName('head')[0],
    style = document.createElement('style');

style.type = 'text/css';
style.media = 'print';

if (style.styleSheet){
  style.styleSheet.cssText = css;
} else {
  style.appendChild(document.createTextNode(css));
}

head.appendChild(style);

window.print();

//don't forget to find and remove style if you don't want all you documents stay in landscape

https://jsfiddle.net/vc4jjhpn/

【讨论】:

    【解决方案2】:

    更改页面打印方向的最简单方法是使用以下 CSS

    @page {
        size: 25cm 35.7cm;
        margin: 5mm 5mm 5mm 5mm; /* change the margins as you want them to be. */
    }
    

    【讨论】:

      【解决方案3】:

      这个对我有用,正在生成 A1 销售汇总表

      var head = '<html><head>' 
            + $("head").html() 
            + ' <style>body{background-color:white !important;}@page { size: 84.1cm 59.4cm;margin: 1cm 1cm 1cm 1cm; }</style></head>';
      
      //Additional code here......
      

      只需注入当前的头部 css,这样你的数据就不会丢失之前的样式

      【讨论】:

        猜你喜欢
        • 2018-04-20
        • 1970-01-01
        • 2019-08-28
        • 2020-07-06
        • 1970-01-01
        • 1970-01-01
        • 2014-03-16
        • 2017-08-28
        • 1970-01-01
        相关资源
        最近更新 更多