twitter-bootstrap 如何打印特定的引导程序 Div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41159676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:26:48  来源:igfitidea点击:

How to Print the Particular bootstrap Div

javascriptjqueryhtmlajaxtwitter-bootstrap

提问by Muhammad Haris

I would like to printa bootstrap div.

我想打印一个引导 div

It is properly displayed in web browser but when I click on the print button then at print preview, the elements are floating and are not properly displayed.

它在网络浏览器中正确显示,但是当我点击打印按钮然后在打印预览时,元素是浮动的并且没有正确显示。

what should I do to solve this problem ?

我该怎么做才能解决这个问题?

Div which I want to print with following this styleenter image description here

我想按照这种风格打印的 Div在此处输入图片说明

What I get the output at printing time which I dont want see on school name at the topenter image description here

我在打印时得到的输出,我不想在顶部的学校名称上看到在此处输入图片说明

Button which call the print function:

调用打印函数的按钮:

<button class="btn btn-default" onclick="printDiv('printableArea')"><i class="fa fa-print" aria-hidden="true" style="    font-size: 17px;"> Print</i></button>

Print Function:

打印功能:

function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}

Div which i Want to print

我想打印的 Div

<div class="container right-container col-md-6" id="printableArea" style="display:block;">
    <span id="link7">   
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <h3 id="school_title"><?php echo "$school_name";?> </h3>
                <p><p> 
                <p style="font-size: 1.1em;" id="exam_title">Annual Examination [ 2015-2016 ] <p> 
                <div class="row">
                    <div class="col-md-4">
                        <div class="header-time-date-marks">
                            <span id="exam_time">Time: 12 AM - 2 PM</span>
                            <span id="exam_date">Date: 30/12/2016</span>
                        </div>
                    </div>
                    <div class="col-md-8 header-time-date-marks" style="text-align: right;padding-right: 36px;">
                        <span id="exam_marks">100 Marks</span>
                    </div>
                </div>
            </div>
        </div>
        <hr / id="line" style="margin-top: 13px;">
        <div class="row q-question-type-style" id='question_section'>
        </div>
    </span>
</div>

回答by Dashang G. Makwana

Check this solution: It is working. The <div>is able to display for print.

检查此解决方案:它正在工作。在<div>能够显示打印。

Place your <script>-tag above the <html>-tag:

将您的<script>-tag放在-tag 上方<html>

<script>
<html>

https://jsfiddle.net/6cz5br7m/

https://jsfiddle.net/6cz5br7m/

回答by viren shah

Call this function: PrintElem('printableArea')

调用这个函数: PrintElem('printableArea')

function PrintElem(elem){
        var mywindow = window.open('', 'PRINT', 'height=400,width=600');

        var css = "";
        var myStylesLocation = "${pageContext.request.contextPath}/ui/css/bootstrap.min.css";

        $.ajax({
            url: myStylesLocation,
            type: "POST",
            async: false
        }).done(function(data){
            css += data;
        }) 

        mywindow.document.write('<html><head><title></title>');
        mywindow.document.write('<style type="text/css">'+css+' </style>');
      //  mywindow.document.write('<link rel="stylesheet" href="${pageContext.request.contextPath}/ui/css/bootstrap.min.css" type="text/css" media="print"/>');
        mywindow.document.write('</head><body >');
        mywindow.document.write('<h1>' + document.title  + '</h1>');
        mywindow.document.write(document.getElementById(elem).innerHTML);
        mywindow.document.write('</body></html>');
        mywindow.document.close(); // necessary for IE >= 10
        mywindow.focus(); // necessary for IE >= 10*/

        mywindow.print(); 
        mywindow.close();


        return true;
    }

回答by suraj mahajan

You can try @media printin css. Like bellow

你可以试试 @media printcss。像下面这样

In css

在 CSS 中

 @media print
   {
      p.bodyText {font-family:georgia, times, serif;}
      .noprint
      {
        display:none
      }
   }

In Html

在 HTML 中

<div class="noprint">
I dont want it on print
</div>
<br><br>
<div>
Prrintable Area
</div>

Fiddle

小提琴

回答by SwSci.Org

I was able to look for the section that was undesirably appearing in the print of the modal, and just hiding it, while using your print function:

我能够在使用您的打印功能时查找不希望出现在模态打印中的部分,并将其隐藏起来:


  print() {
    let mainLayout = document.querySelector('app-main-layout') as HTMLDivElement;
    mainLayout.style.display = 'none';

    window.print();

    mainLayout.style.display = 'unset';
  }

And, make the buttons of my modal go away, by using this css:

并且,通过使用这个 css,让我的模态按钮消失:

@media print {
  button.close {display:none !important;}
}

回答by Shivkumar kondi

you just need to link your js script properly

你只需要正确链接你的 js 脚本

<!DOCTYPE html>
<html>
<body>

<div class="container right-container col-md-6" id="printableArea" style="display:block;">
    <span id="link7">   
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                <h3 id="school_title"><?php echo "$school_name";?> </h3>
                <p><p> 
                <p style="font-size: 1.1em;" id="exam_title">Annual Examination [ 2015-2016 ] <p> 
                <div class="row">
                    <div class="col-md-4">
                        <div class="header-time-date-marks">
                            <span id="exam_time">Time: 12 AM - 2 PM</span>
                            <span id="exam_date">Date: 30/12/2016</span>
                        </div>
                    </div>
                    <div class="col-md-8 header-time-date-marks" style="text-align: right;padding-right: 36px;">
                        <span id="exam_marks">100 Marks</span>
                    </div>
                </div>
            </div>
        </div>
        <hr / id="line" style="margin-top: 13px;">
        <div class="row q-question-type-style" id='question_section'>
        </div>
    </span>
</div>

 <div> <h1>
 asdads
 </h1>
</div>


<button class="btn btn-default" onclick="printDiv('printableArea')"><i class="fa fa-print" aria-hidden="true" style="    font-size: 17px;"> Print</i></button>
<script>
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
</script>

</body>
</html>