Twitter Bootstrap: Print content of modal window

前端 未结 10 941
走了就别回头了
走了就别回头了 2020-11-28 02:19

I\'m developing a site using Bootstrap which has 28 modal windows with information on different products. I want to be able to print the information in an open modal window.

相关标签:
10条回答
  • 2020-11-28 02:53

    I was facing two issues Issue 1: all fields were coming one after other and Issue 2 white space at the bottom of the page when used to print from popup.

    I Resolved this by

    making display none to all body * elements most of them go for visibility hidden which creates space so avoid visibility hidden

        @media print {
            body * {
               display:none;
            width:auto;
            height:auto;
            margin:0px;padding:0px; 
            }
            #printSection, #printSection * {
                display:inline-block!important;
            }
            #printSection {
                position:absolute;
                left:0;
                top:0;  
                margin:0px; 
                page-break-before: none;
                page-break-after: none;
                page-break-inside: avoid;      
            }
    #printSection .form-group{
    
          width:100%!important;
          float:left!important;
          page-break-after: avoid;
        }
    #printSection label{
            float:left!important;
            width:200px!important;
            display:inline-block!important;
          }
    
    #printSection .form-control.search-input{
            float:left!important;
            width:200px!important;
            display: inline-block!important;
          }
    }
    
    0 讨论(0)
  • 2020-11-28 02:55

    Here's an option using a JQuery extension I made based on the code by waspinator in the comments of the accepted answer:

    jQuery.fn.extend({
        printElem: function() {
            var cloned = this.clone();
            var printSection = $('#printSection');
            if (printSection.length == 0) {
                printSection = $('<div id="printSection"></div>')
                $('body').append(printSection);
            }
            printSection.append(cloned);
            var toggleBody = $('body *:visible');
            toggleBody.hide();
            $('#printSection, #printSection *').show();
            window.print();
            printSection.remove();
            toggleBody.show();
        }
    });
    
    $(document).ready(function(){
        $(document).on('click', '#btnPrint', function(){
            $('.printMe').printElem();
        });
    });
    

    JSFiddle: http://jsfiddle.net/95ezN/1227/

    This can be useful if you don't want to have this applied to every single print and just do it on your custom print button (which was my case).

    0 讨论(0)
  • 2020-11-28 03:00

    @media print{
    	body{
    		visibility: hidden; /* no print*/
    	}
    	.print{
    		
    		visibility:visible; /*print*/
    	}
    }
    <body>
      <div class="noprint"> <!---no print--->
      <div class="noprint"> <!---no print--->
      <div class="print">   <!---print--->
      <div class="print">   <!---print--->
    
    
    </body>

    0 讨论(0)
  • 2020-11-28 03:01

    I would suggest you try this jQuery plugin print element

    It can let you just print the element you selected.

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