How to change Page Orientation to Landscape in JavaScript window.print()

The window.print() method is the easiest solution to print HTML content in JavaScript. The Window print() method opens the browser’s default print dialog to print the current document. You can use this method to print dynamic HTML content using JavaScript.

By default, the page orientation is set to portrait in the print dialog. You can change page orientation in the print dialog by adding CSS to the document header. In this code snippet, we will show you how to print the HTML content of the current document using JavaScript and set page orientation to landscape in window.print().

Print specific DIV content with landscape orientation in JavaScript:

var divToPrint = document.getElementById("printableContent");
var divToPrintHTML = '<style type="text/css" media="print">@page { size: landscape; }</style>'+divToPrint.outerHTML;

newWin = window.open();
newWin.document.write(divToPrintHTML);

newWin.print();
newWin.close();

Leave a reply

keyboard_double_arrow_up