<form id="hz9zz"></form>
  • <form id="hz9zz"></form>

      <nobr id="hz9zz"></nobr>

      <form id="hz9zz"></form>

    1. 明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

      PHP生成便于打印的頁面

      [摘要]很多新聞和信息站點都提供了一種生成便于打印的網頁的方法,所產生的頁面的排版布局更有利于打印機的打印輸出,這種方法方便了我們從網頁上直接打印我們所需的內容,而不必為格式不規整傷腦筋,或者粘貼到文本編輯器中重新排版。然而,我卻沒看到有多少網站詳細解釋這些是如何實現的,在這里我提供一小段代碼——用PHP...
      很多新聞和信息站點都提供了一種生成便于打印的網頁的方法,所產生的頁面的排版布局更有利于打印機的打印輸出,這種方法方便了我們從網頁上直接打印我們所需的內容,而不必為格式不規整傷腦筋,或者粘貼到文本編輯器中重新排版。然而,我卻沒看到有多少網站詳細解釋這些是如何實現的,在這里我提供一小段代碼——用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=http://www.okasp.com/techinfo/"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) 


      日韩精品一区二区三区高清