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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17964834/
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-08-25 16:44:11  来源:igfitidea点击:

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

phpjavascripthtmlcss

提问by Montiyago

i'm working on a form in php mysql. so in my page there is a form and below that i shown the data of that form in table format.now i want to print only the table structure thats why i just made a PRINT button as:

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

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

but it will print the whole page with form field and table and i just want the tableto print. Here is the design of my whole page with form and table:

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

<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>

so i just want to print the table not whole page.

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

回答by Marek

Use css to hide elements you don't want to print:

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

@media print {
    .control-group {
      display: none;
    }
}

回答by Rakesh Dongarwar

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>

回答by tobspr

You could make a print-css (which does the same as @media print, but I think it is cleaner, and you can disable the css include via javascript, if you need it):

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

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

In that css, you hide all elements which should not get printed, for example:

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

.wrapper, .header {display: none;}

回答by m4t1t0

One posible solution, perhaps not the best option:

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

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

Sure it has a blink effect but It will work.

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