Muhammed Almaz Muhammed Almaz - 3 months ago 15
Javascript Question

Change Window.print() paper orientation

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?
*/
});
}

Answer

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

https://jsfiddle.net/vc4jjhpn/