首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php下载/打印页面的特定部分

问如何使用php下载/打印页面的特定部分
EN

Stack Overflow用户
提问于 2011-02-26 15:23:45
回答 2查看 13.9K关注 0票数 2

我有一个如下的HTML页面

代码语言:javascript
复制
Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
<table>
    <th>WEEK</th>
    <th>DATES</th>
    <th>Workout #1</th>
    <th>Workout #2</th>
    <th>Workout #3</th>
    <tr>
        <td>1</td>
        <td>3/27-4/2</td>
        <td>Warm up for 5 minutes. </td>
        <td>Same as #1 for this week.</td>
        <td>Same as #1 for this week.</td>
    </tr>
    <tr>
        <td>2</td>
        <td>4/3-4/9</td>
        <td>Warm up for 5 minutes. </td>
        <td>Same as #1 for this week.</td>
        <td>Same as #1 for this week.</td>
    </tr></table>

如何使用php和/或javascript仅使表格可下载和打印。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-26 15:33:53

创建一个打印样式表,该样式表将表以外的所有内容都设置为display: none。浏览器负责打印,而不是您的代码,并在执行此操作时使用打印样式表。

票数 2
EN

Stack Overflow用户

发布于 2012-12-04 23:08:27

是否要打印网页的一部分?网页中是否有多个打印按钮,用于打印页面的一部分?是否要排除页面打印中的某些元素?(如图像、广告)是否要以不同方式打印显示页?(改变文本的字体或颜色)在这里,我将给出上述问题的代码示例

让我们来看一个HTML网页示例

代码语言:javascript
复制
<html>
    <body>
        <div id="header">Header Content</div>
        <div id="content">actual content</div>
        <div id="footer">Footer content</div>
        <input type="button" value="Print" onclick="printPage();"></input>
    </body>
</html>

如果你想打印整个页面,那么

代码语言:javascript
复制
<script language="javascript">
    function printPage() {
        window.print();
    }
</script>

要打印网页的一部分:-创建打印窗口并写入要打印的html文本-聚焦窗口并调用“Print ()”函数-关闭打印窗口

下面是修改后的printPage函数

代码语言:javascript
复制
<input type="button" value="Print" onclick="printPage('content');"></input>
function printPage(id) {
    var html="<html>";
    html+= document.getElementById(id).innerHTML;
    html+="</html>";
    var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status =0');
    printWin.document.write(html);
    printWin.document.close();
    printWin.focus();
    printWin.print();
    printWin.close();
}

可以通过添加样式元素或将某些元素隐藏在实际内容中

包括打印窗口html中css文件

代码语言:javascript
复制
<div id="content">
    <div class="ads"> Ad content</div>
    actual content
    <img src="example.gif" />
</div>
----
function printPage(id) {
    var html="<html>";
    html+="<head>";
    html+="<style type='text/css'>#content div.ads, #content img {display:none}    </style>";
    html+="<link rel='Stylesheet' type='text/css' href='css/print.css' media='print' />";
    html+="</head>";
    html+= document.getElementById(id).innerHTML;
    html+="</html>";
.....
}

在上面的示例中,我已经添加了样式和css文件,您可以使用其中之一或两者都使用。使用这些样式或css,我们可以设置仅用于打印的文本样式。如果您有多个打印按钮,并且每个按钮都打印网页的选定区域,那么您需要在每次单击按钮时传递打印区域的id。

代码语言:javascript
复制
<div id="area1">Area1 content</div>
<input type="button" value="Print" onclick="printPage('area1');"></input>
<div id="area2">Area2 content</div>
<input type="button" value="Print" onclick="printPage('area2');"></input>

只需根据需要更改字段即可

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5125641

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档