doumao8803 2014-12-19 06:37
浏览 43

不要使用CSS和Javascript获得打印div的样式

I want to print content in a div using Javascript and CSS. My main div is with id 'preview'. Content in a div taken from database using PHP and MySQL. In my print page don't get style of the div 'preview'. I want to open print screen in new window. Any body give any suggestion for these issue?

My page and print are

enter image description here

enter image description here

My code is given below.

     <?php
 error_reporting(0);
 $host='localhost'; // Host Name.
 $db_user= 'root'; //User Name
 $db_password= '';
 $db= 'excel'; // Dat
 $conn=@mysql_connect($host,$db_user,$db_password) or die (mysql_error());
 mysql_select_db($db) or die (mysql_error());
 $sql = "select * from first order by id";
 $rsd = @mysql_query($sql);
 ?>

 <script type="text/javascript">
 function printDiv(divID)
   {
        var divElements = document.getElementById(divID).innerHTML;
        var oldPage = document.body.innerHTML;
        document.body.innerHTML = 
          "<html><head><title></title></head><body>" + 
          divElements + "</body>";
        window.print();
        document.body.innerHTML = oldPage;
   }


 </script>
 <style type="text/css" media="print">
 @media print{ #preview{ height:100%;overflow:visible;} } 
 </style>
  <style>

  #my-list{

  padding: 10px;
  padding-left:15px;
  width:auto;
 margin:auto;
  }
 #my-list > li {
display: inline-block;
zoom:1;
*display:inline;
  }
 #my-list > li > a{
color: #666666;
text-decoration: none;
padding: 3px 8px;
 }
 </style>

  <input type="button" value="Print" onClick="javascript:printDiv('preview')" />
   <div id="preview" style="width:1000px; margin:auto;">
 <ul id="my-list" >

    <?php  



    $si=1;

    while($fet=mysql_fetch_array($rsd))
    {
        ?>
             <li> 



            <div class="droppable2"  style="border-color:#3300FF; border:solid #999999;  
            height:180px;width:180px;position:relative; " >
            <div style="float:left;position:absolute; bottom:30px;" class="left">

             <img  src="img.png" >

            </div>
            <div style="float:right;">
            <p style="color: #003399;  font-size: 10px;
             padding-right:5px; font-weight:800; ">www.selafone.net</p>
             <table style="font-size:10px;" >
             <tr> <td >USERNAME: </td> <td> <?php echo $fet['name']; ?> </td></tr>
              <tr> <td>PASSWORD:</td> <td> <?php echo $fet['email']; ?></td></tr></table>
            </div>
             <div  style="position:absolute;background-color:#FF0000;
              padding-bottom:0px; bottom: 0;   height:36px;  ">
              <div style="color:#FFFFFF; padding-left:30px; vertical-align:middle;
              font-weight:100;padding-top:10px; font-size: 8px;">
              <strong> International prepaid Calling Card</strong></div></div>
              </div>
          </li>
        <?php   
        $si=$si+1;  
        }

      ?>

      </ul>

     </div>
  • 写回答

3条回答 默认 最新

  • douzhuo2722 2014-12-19 06:58
    关注

    Source: Background color not showing in print preview


    this is copy/paste reply of @purgatory101 from the top url


    "

    The Chrome css property "-webkit-print-color-adjust: exact;" works appropriately.

    However, making sure you have the correct css for printing can often be tricky. Several things can be done to avoid the difficulties you are having. First, separate all your print css from your screen css. This is done via the @media print and @media screen.

    Often times just setting up some extra @media print css is not enough because you still have all your other css included when printing as well. In these cases you just need to be aware of css specificity as the print rules don't automatically win against non-print css rules.

    In your case, the -webkit-print-color-adjust: exact is working. However, your background-color and color definitions are being beaten out by other css with higher specificity.

    While I DO NOT endorse using !important in nearly any circumstance, the following definitions work properly and expose the problem:

    @media print {
      .your_id {
        color: white !important;
        background-color: #1a4567 !important;
        -webkit-print-color-adjust: exact; 
      }
    }
    

    "

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭