Landscape printing from HTML

前端 未结 16 1813
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 06:31

I have a HTML report, which needs to be printed landscape because of the many columns. It there a way to do this, without the user having to change the document settings?

相关标签:
16条回答
  • 2020-11-22 06:56

    You can try the following:

    @page {
        size: auto
    }
    
    0 讨论(0)
  • 2020-11-22 06:57

    I created a blank MS Document with Landscape setting and then opened it in notepad. Copied and pasted the following to my html page

    <style type="text/css" media="print">
       @page Section1
        {size:11 8.5in;
        margin:.5in 13.6pt 0in 13.6pt;
        mso-header-margin:.5in;
        mso-footer-margin:.5in;
        mso-paper-source:4;}
    div.Section1
        {page:Section1;}
    </style>
    
    
    
    <div class="Section1"> put  text / images / other stuff  </div>
    

    The print preview shows the pages in a landscape size. This seems to be working fine on IE and Chrome, not tested on FF.

    0 讨论(0)
  • 2020-11-22 06:58

    In your CSS you can set the @page property as shown below.

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

    The @page is part of CSS 2.1 specification however this size is not as highlighted by the answer to the question Is @Page { size:landscape} obsolete?:

    CSS 2.1 no longer specifies the size attribute. The current working draft for CSS3 Paged Media module does specify it (but this is not standard or accepted).

    As stated the size option comes from the CSS 3 Draft Specification. In theory it can be set to both a page size and orientation although in my sample the size is omitted.

    The support is very mixed with a bug report begin filed in firefox, most browsers do not support it.

    It may seem to work in IE7 but this is because IE7 will remember the users last selection of landscape or portrait in print preview (only the browser is re-started).

    This article does have some suggested work arounds using JavaScript or ActiveX that send keys to the users browser although it they are not ideal and rely on changing the browsers security settings.

    Alternately you could rotate the content rather than the page orientation. This can be done by creating a style and applying it to the body that includes these two lines but this also has draw backs creating many alignment and layout issues.

    <style type="text/css" media="print">
        .page
        {
         -webkit-transform: rotate(-90deg); 
         -moz-transform:rotate(-90deg);
         filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=3);
        }
    </style>
    

    The final alternative I have found is to create a landscape version in a PDF. You can point to so when the user selects print it prints the PDF. However I could not get this to auto print work in IE7.

    <link media="print" rel="Alternate" href="print.pdf">
    

    In conclusion in some browsers it is relativity easy using the @page size option however in many browsers there is no sure way and it would depend on your content and environment. This maybe why Google Documents creates a PDF when print is selected and then allows the user to open and print that.

    0 讨论(0)
  • 2020-11-22 06:59

    You can also use the non-standard IE-only css attribute writing-mode

    div.page    { 
       writing-mode: tb-rl;
    }
    
    0 讨论(0)
  • 2020-11-22 07:00

    If you want to see landscape on the screen before you print, as well as printing, then in your css, you can set the width to 900px, and the height to 612px.

    OP didn't mention A4 size. I assume it's Letter size in my numbers above.

    0 讨论(0)
  • 2020-11-22 07:03

    The size property is what you're after as mentioned. To set both the the orientation and size of your page when printing, you could use the following:

    /* ISO Paper Size */
    @page {
      size: A4 landscape;
    }
    
    /* Size in mm */    
    @page {
      size: 100mm 200mm landscape;
    }
    
    /* Size in inches */    
    @page {
      size: 4in 6in landscape;
    }
    

    Here's a link to the @page documentation.

    0 讨论(0)
提交回复
热议问题