Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different page orientation for printing HTML

Tags:

Code:

I try to make the following simple HTML page work:

<html>
  <head>
    <style type="text/css">     
    @media print
    {               
      @page port { size: portrait; }
      .portrait { page: port; }

      @page land { size: landscape; }
      .landscape { page: land; }                

      .break { page-break-before: always; }
    }
    </style>
  </head>
  <body>
    <div class="landscape">
      <p>This is a landscape page.</p>
    </div>
    <div class="portrait break">
      <p>This is a portrait page.</p>
    </div>
  </body>
</html>

Question:

I want to print the first div's content onto the first page, with landscape orientation, and the second one with portrait mode. However, all browsers (Chrome, Opera, Safari, IE10) print two portrait pages. Did I miss something or do none of the browsers support this kind of feature yet? In the latter case is there any alternative to achieve that result?

like image 284
tungi52 Avatar asked Feb 15 '13 21:02

tungi52


People also ask

How can you print a page in different orientation?

Change the page orientation for a documentGo to Layout > Orientation, and then select Landscape. Go to File > Print. Under Settings, make sure the orientation box says Landscape Orientation.

How do I change page orientation in HTML?

href = "landscape.


2 Answers

A quick and dirty hack would be to rotate the div that is meant to be in landscape by 90 degrees using CSS3 or filters. The following would work:

-webkit-transform: rotate(-90deg);
-moz-transform:rotate(-90deg);
filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);

There is currently no easy way to do this in any other way, as the size CSS directive is only implemented by one browser (Opera), but is nevertheless part of the current drafts ( Is @Page { size:landscape} obsolete? for the deprecation, http://www.w3.org/TR/css3-page/#page-size for the spec).

The next cheapest hack is what I mentioned above: lay your HTML out on a portrait...and rotate by 90 degrees using CSS3.

like image 52
Sébastien Renauld Avatar answered Sep 24 '22 05:09

Sébastien Renauld


The size property is not used (anymore), so I wouldn't rely on that. The most pragmatic way would be to generate PDF's on the server before printing.

The rotating solution provided by Seéastien would also work, but only in browsers that support it.

like image 25
Reinier Kaper Avatar answered Sep 20 '22 05:09

Reinier Kaper