如何通过window.print()打印页面的特定部分

How to print some specific part of a page via window.print()

本文关键字:定部 打印 window 何通过 print      更新时间:2023-09-26

我正在php-mysql中处理一个表单。所以在我的页面上有一个表格,下面我以表格格式显示了该表格的数据。现在我只想打印表格结构,这就是为什么我只制作了一个print按钮作为:

<span style="float:right;"><a href="javascript:window.print()" type="button" class="btn">PRINT</a></span>

但它会打印整个页面,包括表单字段和表格,我只想打印表格。这是我整个页面的设计,包括表单和表格:

<html>
<head></head>
<body>
 <div class="container">
 <form class="form-horizontal" action="" method="post" name="userform1" id="company-form" enctype="multipart/form-data">
     <?php if($_GET[id]){?>

<fieldset>
     <legend>Add Company</legend>
    <div class="control-group">
    <label class="control-label">Company Name</label>
    <div class="controls">
    <input type="text" name="company" id="company" value="<?php echo $selup['company']?>">
    </div>
    </div>
<div class="control-group">another field</div>
<div class="control-group">another field</div>
<div class="control-group">
    <div class="controls">
    <button type="submit" class="btn" name="submit" id="submit" value="Submit">Submit</button>
    </div>
    </div>
<table class="table table-bordered">
           <tbody>
            <tr>
                <td>S.No.</td>
                <td>Company Name</td>
                <td>Type</td>
                <td>Action</td>
          </tr>
                 <?php 
                 // to print the records
                $select = "select * from company where type='Miscellaneous'";
                $query1 = mysql_query($select);  
                 while($value = mysql_fetch_array($query1)){ ?>
                <tr>
                <td><?php echo $value[id];?></td>
                <td><?php echo $value[company ];?></td>
                <td><?php echo $value[type];?></td>
                <!--<td>&nbsp;</td>-->
                <?php /*?><td><?php echo $value[amount];?></td>               
                <td><?php echo $value[date];?></td><?php */?>
                <td><a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php echo $value[id];?>&cmd=edit"><i class="icon-edit"></i></a>
               <a href="<?php echo $_SERVER['PHP_SELF']; ?>?id=<?php  echo $value[id];?>&cmd=delete" onclick="return confirm('Are you sure you want to delete <?php  echo $value[customer];?>?')"><i class="icon-trash"></i></a></td>

                </tr><?php }?>
        </tbody>
       </table>
</fieldset>
<form>
</div>
</body>

所以我只想打印表格,而不是整页。

使用css隐藏不想打印的元素:

@media print {
    .control-group {
      display: none;
    }
}
function printContent(el){
    var restorepage = document.body.innerHTML;
    var printcontent = document.getElementById(el).innerHTML;
    document.body.innerHTML = printcontent;
    window.print();
    document.body.innerHTML = restorepage;
}
<!DOCTYPE html>
<html>
<head>
</head> 
<body> 
<h1>My page</h1>
<div id="div1">DIV 1 content...</div>
<button onclick="printContent('div1')">Print Content</button>
<div id="div2">DIV 2 content...</div>
<button onclick="printContent('div2')">Print Content</button>
<p id="p1">Paragraph 1 content...</p>
<button onclick="printContent('p1')">Print Content</button>
</body> 
</html>

您可以制作一个打印css(与@media print相同,但我认为它更干净,如果需要,您可以通过javascript禁用css include):

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

在该css中,您隐藏了所有不应该打印的元素,例如:

.wrapper, .header {display: none;}

一个可能的解决方案,也许不是最好的选择:

1. Open a new window with JS
2. Copy the whole table into the new window (with jQuery for example)
3. Print the new window
4. Close the window

当然它有眨眼的效果,但它会起作用的。