How to use PHP to generate web pages that are easy to print

不言
Release: 2023-04-01 16:00:01
Original
3644 people have browsed it

这篇文章主要介绍了关于利用PHP生成便于打印的网页的方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

很多新闻和信息站点都提供了一种生成便于打印的网页的方法,所产生的页面的排版布局更有利于打印机的打印输出,这种方法方便了我们从网页上直接打印我们所需的内容,而不必为格式不规整伤脑筋,或者粘贴到文本编辑器中重新排版。然而,我却没看到有多少网站详细解释这些是如何实现的,在这里我提供一小段代码——用PHP来实现生成便于打印的网页并不是像想象的那么难,希望对大家有帮助。
要生成便于打印的网页,需要我们做哪些工作呢?这主要取决于你的网站特点,和你想要生成的版式特征,不过有一些基本处理需要完成:
1、    页宽——生成页面的宽度必须限制,要打印A4的纸,大约网页要在630像素宽。
2、    页面背景色——为了美观,很多网页使用了不同的背景色和背景图片,但是作为要打印的网页,最合适效果的还是白底黑字为好。
3、    广告条——移除页面上的广告
4、    表格的背景色——我们经常在表格中用颜色来强调信息和标题,这些也必须移除。
5、    链接——页面中的超链接也必须改变以使URL可见,例如:GBDirect应显示为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>
Copy after login

把当前页面的名称传递到pfp.php程序中,这个程序使用PHP的“file”函数把页面作为一个字符串来处理。当这个页面被载入的时候,程序就可以增加、改写或删除HTML片段。

<? 
 ereg(&#39;^.*/&#39;,$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(&#39;&#39;, file("$page.inc")); 
          // 忽略颜色属性,转换以&#39;ignore&#39;替代&#39;color&#39; 
          $fcontents = ereg_replace(&#39;color&#39;,&#39;ignore&#39;,$fcontents); 
          // 去除超链接中的 “_blank” 
          $fcontents = ereg_replace(&#39;target=\"_blank\"&#39;,&#39;&#39;,$fcontents); 
          // 替换</a>标记 
          $fcontents = ereg_replace(&#39;</a>&#39;,&#39;&#39;,$fcontents); 
          // 显示URL的绝对地址 
          $fcontents = ereg_replace(&#39;<a[^h]*href="(http://[^"]*)"[^>]*>;([^]*)&#39;, 
          &#39;<strong>\\2</strong><em>(\\1)</em>&#39;,$fcontents); 
          // 把相对链接转为绝对链接 
          $fcontents = ereg_replace( 
              &#39;<a[^h]*href="([^"]*)"[^>]*>([^]*)&#39;, 
       "<strong>\\2</strong><em>(http://$HTTP_HOST/\\1)</em>";, 
             $fcontents); 
          // 背景颜色改回白色 
          $fcontents = ereg_replace(&#39;<body bgignore&#39;,&#39;<body bgcolor&#39;,  $fcontents); 
         // if any markers left restore link end element 
         $fcontents = ereg_replace(&#39;&#39;,&#39;</a>&#39;,$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>
Copy after login

这样便于打印的页面就生成了,希望对大家能有帮助。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

在php中生成随机数的3种方法

PHP生成图片验证码以及点击切换的代码

如何使用php动态生成版权所有信息的方法

The above is the detailed content of How to use PHP to generate web pages that are easy to print. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!