PHP generates easy-to-print web pages

Source: Internet
Author: User
Many news and information sites provide a method for generating easy-to-print web pages. the layout of the generated pages is more conducive to printer printing and output, this method makes it easy for us to print the content we need directly from the web page, without having to worry about the format being irregular, or paste it into the text editor to retypeset. However, I don't see how many websites explain in detail how these are implemented, here I provide a short piece of code-using php to generate a page that is easy to print is not as difficult as you think, we hope that many news and information sites will provide a method for generating easy-to-print web pages. the layout of these pages is more conducive to printer printing and output, this method makes it easy for us to print the content we need directly from the web page, without having to worry about the format being irregular, or paste it into the text editor to retypeset. However, I don't see how many websites explain in detail how these are implemented, here I provide a short piece of code-using php to generate a web page that is easy to print is not as difficult as you think, and I hope it will help you.

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 features you want to generate, but some basic processing needs to be completed:

1. page width: the width of the page to be generated must be limited. to print A4 paper, the page must be around 630 pixels wide.
2. page background color-for the sake of appearance, many web pages use different background colors and images. However, as a webpage to be printed, the most suitable effect is the white background and black text.
3. advertisement bar-remove advertisement on the page
4. background color of the table-we often use colors to emphasize information and titles in the table, which must also be removed.
5. links-hyperlinks in the page must also be changed to make the URL visible, for example, GBDirect should be shown as GBDirect (http://www.gbdirect.co.uk /)
6. menu-menu is the most difficult to prohibit. However, if your page is built using a template, the simplest method is to use a template without menus that is easy to print.

All these methods for generating pages that are easy to print are very simple. you can put the following code into the webpage when you need to implement them:
// Obtain the relative path of the file from the environment variable
$ Page = substr ($ SCRipT_NAME, 1 );

// Display an icon and connect to PRinter Friendly Pages
// The pfp. php program is used to print the page.
?>
">;
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 the "file" function of PHP to process the page as a string. When this 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




















                       

    // Check if the filename for the page exists
If (! File_exists ("$ page. inc "))
{
Echo"Error-The page ".
"Does not exist on this site.
";
}
Else
{
// Get the page content and put it in a string
$ Fcontents = join ('', file (" $ page. inc "));

// Ignore the color attribute and replace 'color' with 'ignore'
     
$ Fcontents = ereg_replace ('color', 'ignore', $ fcontents );

// Remove "_ blank" from the hyperlink"
$ Fcontents = ereg_replace ('target = \ "_ blank \" ', '', $ fcontents );

// Replace the tag
$ Fcontents = ereg_replace ('','', $ fcontents );

// Display the absolute URL
$ Fcontents = ereg_replace ('] *>; ([^] *)',
'\ 2(\ 1)', $ Fcontents );

// Convert the relative link to an absolute link
$ Fcontents = ereg_replace (
'] *> ([^] *)',
"\ 2(Http: // $ HTTP_HOST/\ 1)";,
$ Fcontents );

// Change the background color to white
$ Fcontents = ereg_replace ('
// If any markers left restore link end element
$ Fcontents = ereg_replace ('','', $ fcontents );

// Output page
Echo $ fcontents;
}
?>
  

   
  

 

 


In this way, the printed page is generated, hoping to help you.

(Translated from PHPBulider/Mark Spink)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.