PHP生成便于打印的网页

  •   2009-08-01/08:17
  • 很多新闻和信息站点都提供了一种生成便于打印的网页的方法,所产生的页面的排版布局更有利于打印机的打印输出,这种方法方便了我们从网页上直接打印我们所需的内容,而不必为格式不规整伤脑筋,或者粘贴到文本编辑器中重新排版。然而,我却没看到有多少网站详细解释这些是如何实现的,在这里我提供一小段代码——用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) 

    评论 {{userinfo.comments}}

    {{money}}

    {{question.question}}

    A {{question.A}}
    B {{question.B}}
    C {{question.C}}
    D {{question.D}}
    提交

    驱动号 更多