2016-08-18 118 views
10

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

window.print()

但我不知道,我怎么能做到这一点。

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

我不需要它。

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

我可能是错的,但我不认为这是什么,你可以做。纸张大小和方向取决于用户。 – Brad

回答

12

您需要为文档注入样式。

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/

+0

并完成。谢谢,我知道了:) –

1

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

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