Javascript 更改 Window.print() 纸张方向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39025840/
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
Change Window.print() paper orientation
提问by Somomo1q
I want to change paper mode (orientation) on the window print. I want to change it programatically but i could not find anything.
我想在窗口打印上更改纸张模式(方向)。我想以编程方式更改它,但我找不到任何东西。
window.print()
窗口.打印()
But i do not know , how can i do it.
但我不知道,我该怎么做。
@media print{@page {size: landscape}}
I dont need it.
我不需要它。
function printWindow()
{
window.print({
/*
some code here?
*/
});
}
回答by Denis Matafonov
You need to inject style to your document.
您需要为文档注入样式。
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
回答by shyamm
the simplest way to change page print orientation is by using following CSS
更改页面打印方向的最简单方法是使用以下 CSS
@page {
size: 25cm 35.7cm;
margin: 5mm 5mm 5mm 5mm; /* change the margins as you want them to be. */
}
回答by NINSIIMA WILBER
This one worked for me, was generating an A1 sales summary sheet
这个对我有用,正在生成 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......
Just inject the current head css so that your data does not lose the previous styling
只需注入当前的头部 css,这样你的数据就不会丢失之前的样式