当前位置: 首页 > 图文教程 > 网络编程 > PHP > PHP生成便于打印的网页

PHP
PHP 执行系统外部命令 system() exec() passthru()
最新的php 文件上传模型,支持多文件上传
php 静态页面中显示动态内容
数据库查询记录php 多行多列显示
谈PHP生成静态页面分析 模板+缓存+写文件
PHP 各种排序算法实现代码
PHP nl2br函数 将换行字符转成 <br>
php 分页原理详解
Discuz 模板语句分析及知识技巧
php win下Socket方式发邮件类
怎样去阅读一份php源代码
建站常用13种PHP开源CMS比较
php xml留言板 xml存储数据的简单例子
PHP 开源AJAX框架14种
PHP 替换模板变量实现步骤
PHP has encountered an Access Violation at 7C94BD02解决方法
php 正则匹配函数体
php 文件夹删除、php清除缓存程序
php download.php实现代码 跳转到下载文件(response.redirect)
PHP类(Class)入门教程

PHP生成便于打印的网页


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 208 ::
收藏到网摘: n/a

很多新闻和信息站点都提供了一种生成便于打印的网页的方法,所产生的页面的排版布局更有利于打印机的打印输出,这种方法方便了我们从网页上直接打印我们所需的内容,而不必为格式不规整伤脑筋,或者粘贴到文本编辑器中重新排版。然而,我却没看到有多少网站详细解释这些是如何实现的,在这里我提供一小段代码——用PHP来实现生成便于打印的网页并不是像想象的那么难,希望对大家有帮助。
要生成便于打印的网页,需要我们做哪些工作呢?这主要取决于你的网站特点,和你想要生成的版式特征,不过有一些基本处理需要完成:
1、 页宽——生成页面的宽度必须限制,要打印A4的纸,大约网页要在630像素宽。
2、 页面背景色——为了美观,很多网页使用了不同的背景色和背景图片,但是作为要打印的网页,最合适效果的还是白底黑字为好。
3、 广告条——移除页面上的广告
4、 表格的背景色——我们经常在表格中用颜色来强调信息和标题,这些也必须移除。
5、 链接——页面中的超链接也必须改变以使URL可见,例如:<a href=http://www.gbdirect.co.uk/ >GBDirect</a>应显示为GBDirect (http://www.gbdirect.co.uk/)
6、 菜单——菜单是最难被禁止的,然而如果你的页面是使用模板来构建的话,那么最简单的方法是换用便于打印的没有菜单的模板。
这些生成便于打印页面的所有方法,都是非常简单的,需要实现的时候你可以被下面的代码放到网页中:
<?
//从环境变量中得到文件的相对路径
$page=substr($SCRIPT_NAME,1);
// 显示一个图标并连接到Printer Friendly Pages
// 便于打印页面的生成程序pfp.php
?>
<a href="pfp.php?page=<?=$page?>">;
<img src="printer.gif" width="36" height="36" border="0"
alt="Click here to produce a printer friendly page">
<font face="arial, helvetica" size="2">
Printer Friendly Version
</font>
</a>
把当前页面的名称传递到pfp.php程序中,这个程序使用PHP的“file”函数把页面作为一个字符串来处理。当这个页面被载入的时候,程序就可以增加、改写或删除HTML片段。
<?
ereg('^.*/',$SCRIPT_FILENAME,$tmp);
$page_path = substr($tmp[0],0,-1);
?>
<html>
<head>
<base href="http://<? echo $HTTP_HOST ?>/">
<meta name="robots" content="no index, no follow">
<title>Printer Friendly Page</title>
</head>
<body bgcolor="white">
<font face="Arial,Helvetica">
<table border="0" cellpadding="5" cellspacing="0" width="630" >
<tr>
<td valign="top">
<?
// check if the filename for the page exists
if (!file_exists("$page.inc"))
{
echo "<strong>Error - The page <?=$page?>".
"does not exist on this site.</strong>";
}
else
{
// 得到页面的内容并把它放到一个字符串中
$fcontents = join('', file("$page.inc"));
// 忽略颜色属性,转换以'ignore'替代'color'
$fcontents = ereg_replace('color','ignore',$fcontents);
// 去除超链接中的 “_blank”
$fcontents = ereg_replace('target=\"_blank\"','',$fcontents);
// 替换</a>标记
$fcontents = ereg_replace('</a>','',$fcontents);
// 显示URL的绝对地址
$fcontents = ereg_replace('<a[^h]*href="(http://[^"]*)"[^>]*>;([^]*)',
'<strong>\\2</strong><em>(\\1)</em>',$fcontents);
// 把相对链接转为绝对链接
$fcontents = ereg_replace(
'<a[^h]*href="([^"]*)"[^>]*>([^]*)',
"<strong>\\2</strong><em>(http://$HTTP_HOST/\\1)</em>";,
$fcontents);
// 背景颜色改回白色
$fcontents = ereg_replace('<body bgignore','<body bgcolor', $fcontents);
// if any markers left restore link end element
$fcontents = ereg_replace('','</a>',$fcontents);
// 输出页面
echo $fcontents;
}
?>
</td>
</tr>
<tr>
<td align="center"><hr width="90%"></td>
</tr>
<tr>
<td align="center">
<? include("$page_path/footer.inc"); ?>
</td>
</tr>
</table>
</font>
</body>
</html>
这样便于打印的页面就生成了,希望对大家能有帮助。
(译自PHPBulider/Mark Spink)