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. There is no need 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. To print on A4 paper, the web page must be approximately 630 pixels wide.
2. Page background color - For the sake of beauty, many web pages use different background colors and background pictures. However, as a web page to be printed, the most suitable effect is white background and black text.
3. Advertising banners - remove ads on the page
4. Background color of tables - we often use colors 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. Menus - Menus are the hardest to ban. However, if your page is built using templates, the easiest way is to switch to one that is easy to print. Template without menu.
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:
//Get the relative path of the file from the environment variable
$page =substr($SCRIPT_NAME,1);
// Display an icon and connect to Printer Friendly Pages
// Generate program pfp.php to facilitate printing pages
?>
;
alt="Click here to produce a printer friendly page">
Printer Friendly Version
Pass the name of the current page to the pfp.php program , this program uses PHP's "file" function to process 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);
?>
// check if the filename for the page exists if (!file_exists("$page.inc")) { echo "Error - The page =$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; } ?> |
include("$page_path/footer.inc"); ?> |
The above introduces the web page generated by PHP that is easy to print on the homepage of the School of Online Education of Shaanxi Normal University, including the content of the homepage of the School of Online Education of Shaanxi Normal University. I hope it will be helpful to friends who are interested in PHP tutorials.