Home  >  Article  >  Backend Development  >  PHP generates web pages that are easy to print_PHP tutorial

PHP generates web pages that are easy to print_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:04:14869browse

Many news and information sites provide a method to generate web pages that are easy to print. The layout of the generated pages is more conducive to printer printout. This method makes it convenient for us to print the content we need directly from the web page. You don’t have to worry about irregular formatting, or paste it into a text editor and retype it. However, I haven't seen many websites that explain in detail how these are implemented. Here I provide a small piece of code - using PHP to generate a web page that is easy to print is not as difficult as imagined. I hope it will be helpful to everyone.

What do we need to do to generate a web page that is easy to print? This mainly depends on the characteristics of your website and the layout characteristics you want to generate, but there are some basic processes that need to be completed:

1. Page width - the width of the generated page must be limited, and A4 paper must be printed , approximately the web page should be 630 pixels wide.
2. Page background color - For the sake of beauty, many web pages use different background colors and background images. However, as a web page to be printed, the most suitable effect is white background and black text.
3. Advertising strips - remove ads on the page
4. Table background color - we often use color in tables to emphasize information and titles, these must also be removed.
5. Links - Hyperlinks in the page must also change to make the URL visible, for example: GBDirect should appear as GBDirect (http://www.gbdirect.co.uk/)
6. Menu - Menus are the most difficult to ban. However, if your page is built using templates, the easiest way is to change Use menu-less templates that are easy to print.

All these methods of generating pages that are easy to print are very simple. When you need to implement them, you can put the following code into the web page:
//From environment variables Get the relative path of the file in
$page=substr($SCRIPT_NAME,1);

// Display an icon and connect to Printer Friendly Pages
// Generate program pfp to facilitate printing pages. php
?>
;
alt="Click here to produce a printer friendly page">


Printer Friendly Version


Pass the name of the current page to the pfp.php program, which uses PHP's "file" function Treat the page as a string. When the page is loaded, the program can add, rewrite, or delete HTML fragments.

ereg('^.*/',$SCRIPT_FILENAME,$tmp);
$page_path = substr($tmp[0],0,-1);
?>




  
  
  Printer Friendly Page








  

    

"90%">




              // check if the filename for the page exists
        if (!file_exists("$page.inc"))
        {
           echo "Error - The page ".
                "does not exist on this site.
";
        }
        else
        {
          // 得到页面的内容并把它放到一个字符串中
          $fcontents = join('', file("$page.inc"));

          // 忽略颜色属性,转换以'ignore'替代'color'

          $fcontents = ereg_replace('color','ignore',$fcontents);

          // 去除超链接中的 “_blank”
          $fcontents = ereg_replace('target="_blank"','',$fcontents);

          // 替换标记
          $fcontents = ereg_replace('','',$fcontents);

          // 显示URL的绝对地址
          $fcontents = ereg_replace(']*>;([^]*)',
          '\2(\1)',$fcontents);

          // 把相对链接转为绝对链接
          $fcontents = ereg_replace(
              ']*>([^]*)',
       "\2(http://$HTTP_HOST/\1)";,
             $fcontents);

          // 背景颜色改回白色
          $fcontents = ereg_replace('
         // if any markers left restore link end element
         $fcontents = ereg_replace('','',$fcontents);

         // 输出页面
         echo $fcontents;
       }
?>
 


                                                               ;



This is a page that is easy to print. I hope it will be helpful to everyone.

(Translated from PHPBulider/Mark Spink)



http://www.bkjia.com/PHPjc/316009.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/316009.html

Many news and information sites provide a method to generate web pages that are easy to print, and the layout of the generated pages The layout is more conducive to the printer's printout. This method facilitates us...
Statement:
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