【问题标题】:How can we add css in print preview?我们如何在打印预览中添加 css?
【发布时间】:2019-12-06 08:59:16
【问题描述】:

代码:

<style>
  .center {
    float: left;
    text-align: center;
  }
  .center h3 {
    color: #000;
    font-weight: 600;
  }
  .left {
    font-size: 14px;
    float: left;
    width: 30%;
    color: #000;
  }
  .right {
    font-size: 14px;
    float: right;
    width: 70%;
    color: #000;
  }
  #bot {
    margin-bottom: 2em;
  }
  #left {
    font-size: 14px;
    float: left;
    width: 42%;
    color: #000;
  }
  #right {
    font-size: 14px;
    float: right;
    width: 58%;
    color: #000;
  }
  table, td, th {  
    border: 1px solid #000;
    text-align: left;
    font-size: 14px;
    font-weight: inherit;
  }
  table {
    border-collapse: collapse;
    width: 100%;
  }
  th, td {
    padding: 15px;
  }
  #pp {
    font-size:14px;
    color:#000;
  }
  .table-responsive {
      overflow-x: hidden!important;
  }
  .dataTables_wrapper .dataTables_paginate .paginate_button {
    padding:0px!important;
  }
  </style>
<div class="col-md-12">
  <a href="javascript:void(0)" class="btn btn-success" style="float: right;" onclick='printDiv();'>Print Sheet</a>
</div>
<div id="print_wrapp">
  <div class="col-md-12" id="bot">
    <div class="col-md-2">
      <img src="<?php echo base_url(); ?>uploads/images/Lingayas Vidyapeeth Logo.png" class="img-responsive" style="width: 110px;">
    </div>
    <div class="col-md-10">
      <div class="center">
        <h3>
          Lingaya's Vidyapeeth, Fraidabad<br>
          Award Sheet<br>
          End Semester Examination<br>
          (May-June, 2019)
        </h3>
      </div>
    </div>
  </div>

  <div class="row">
    <div class="col-md-12" id="bot">
      <div class="col-md-6">
        <div class="left">
          <p>Department: </p>
        </div>
        <div class="right">
          <p><?php echo $award[0]['department_name']?></p>
        </div>

        <div class="left">
          <p>Program/Course: </p>
        </div>
        <div class="right">
          <p><?php echo $award[0]['classes']?></p>
        </div>

        <div class="left">
          <p>Subject Name: </p>
        </div>
        <div class="right">
          <p><?php echo $award[0]['subject']?></p>
        </div>

        <div class="left">
          <p>Session: </p>
        </div>
        <div class="right">
          <p><?php echo $award[0]['session']?></p>
        </div>

      </div>
      <div class="col-md-6">

        <div class="left">
          <p>Semester: </p>
        </div>
        <div class="right">
          <p><?php echo $award[0]['yearsOrSemester']?></p>
        </div>

        <div class="left">
          <p>Subject Code: </p>
        </div>
        <div class="right">
          <p><?php echo $award[0]['subject_code']?></p>
        </div>

        <div class="left">
          <p>Maximum Marks: </p>
        </div>
        <div class="right">
          <p></p>
        </div>

      </div>
    </div>

    <div class="col-md-12" id="bot">
      <div class="col-md-12">
        <div class="table-responsive">
          <table id="example1" class="table table-striped table-bordered table-hover dataTable no-footer">
            <thead>
              <th>S.No.</th>
              <th>Roll No.</th>
              <th>Marks(In Figure)</th>
              <th>Marks(in Words)</th>
            </thead>
            <tbody>
              <?php
                $i=1;
                foreach($award as $row)
                {
              ?>
                  <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $row['roll']; ?></td>
                    <td></td>
                    <td></td>
                  </tr>
              <?php
                $i++;
                }
              ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>


    <div class="col-md-12" id="bot">
      <div class="col-md-6">
        <div id="left">
          <p>Total No. of Student Pass: </p>
        </div>
        <div id="right">
          <p></p>
        </div>
      </div>
      <div class="col-md-6">
        <div id="left">
          <p>Total No. of Student Fail: </p>
        </div>
        <div id="right">
          <p></p>
        </div>
      </div>
    </div>

  </div>
</div>

  <script>
    function printDiv() 
    {
        var divToPrint=document.getElementById('print_wrapp');
        var newWin=window.open('','Print-Window');
        newWin.document.open();
        newWin.document.write('<html><link rel="stylesheet" href="<?php echo base_url(); ?>assets/plugins/bootstrap/css/bootstrap.css" type="text/css" /><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
        newWin.document.close();
        setTimeout(function(){newWin.close();},10);
    }
  </script>

在这段代码中,我只是通过点击功能在 div &lt;div id="print_wrapp"&gt; 中运行打印内容,即 printDiv 并且它工作正常,但问题是打印预览 css 没有正确显示 div 内容,下面提到了一个图像:

我的原始页面如下所示:

那么,我该如何解决这个问题?请帮帮我。

谢谢

【问题讨论】:

  • CSS 没有它应该适用的 HTML,是相当无用的。请提供正确的minimal reproducible example 您的问题。并解释什么是“正确显示”。
  • 试试内联 CSS 并检查一下!
  • 我在我的代码 @04FS 中进行了编辑
  • 似乎您在代码中使用了 Bootstrap。引导 css 库将 screen 用作媒体查询,并且不适用于 print。您可以更改 Bootstrap css 以包括 print
  • 您是否 100% 确定 &lt;?php echo base_url(); ?&gt;assets/plugins/bootstrap/css/bootstrap.css 链接到有效的 url 并正确加载 css?

标签: php jquery html css print-preview


【解决方案1】:

您也可以使用 media='screen,print' 将样式表应用到 print 和 screen 中

<link ../bootstrap.min.css' rel='stylesheet' media='screen,print'>

<link ../bootstrap.min.css' rel='stylesheet' media='all'>

【讨论】:

  • OP 没有使用@print,因为他们没有使用 print preview(尽管标题是这样说的)——他们正在使用 print-popup-window——创建一个新的窗口并用部分 html 填充它。还是@screen
【解决方案2】:

当我们想在打印页面时应用一些样式,我们需要使用@media print 来应用这些样式。则只有样式适用于 windows 打印功能。

样式.css

@media print
{
    .center {
        float: left;
        text-align: center;
      }
      .center h3 {
        color: #000;
        font-weight: 600;
      }
      .left {
        font-size: 14px;
        float: left;
        width: 30%;
        color: #000;
      }
      .right {
        font-size: 14px;
        float: right;
        width: 70%;
        color: #000;
      }
}

然后用样式链接你要打印页面的css文件。

<link rel="stylesheet" type="text/css" href="Style.css">

在按钮点击时调用打印函数

<button onclick="printPage()" class="center">Print</button>

<script>
    function printPage()
    {
        print();
    }
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 2015-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多