PHP generates easy-to-print web pages _php Tutorials

Source: Internet
Author: User
Many news and information sites provide a way to generate pages that are easy to print, and the layout of the resulting pages is more conducive to the printout of the printer, which makes it easier for us to print the content we need directly from the Web page without having to worry about formatting or pasting it into a text editor to re-typeset. However, I do not see how many sites explain how these are implemented in detail, here I provide a small piece of code-PHP to generate easy to print Web page is not as difficult as imagined, I hope to be helpful.

What do we need to do to generate pages that are easy to print? This depends on your site's characteristics, and the layout features you want to generate, but there are some basic processing needs to be done:

1, page width-the width of the generated page must be limited, to print A4 of paper, about the page to be 630 pixels wide.
2, page background color-for beautiful, a lot of web pages use different background color and background image, but as the page to be printed, the most appropriate effect or white black word for good.
3. Banner--Remove the ads on the page
4, the background color of the table-we often use color in the table to emphasize the information and the title, which must also be removed.
5, Link--The hyperlink in the page must also change to make the URL visible, for example: Gbdirect should be displayed as Gbdirect (http://www.gbdirect.co.uk/)
6, Menu-menu is the most difficult to be banned, however, if your page is built using a template, then the simplest way is to easily print the template without a menu.

These builds are easy to print all the way to the page, it is very easy to implement when you can be placed in the following code on the page:
To get the relative path of a file from an environment variable
$page =substr ($SCRIPT _name,1);

Displays an icon and connects to printer friendly Pages
Easy to print page generation program pfp.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 to handle 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);
?>






<title>Printer Friendly Page</title>





















Check if the filename for the page exists
if (!file_exists ("$page. Inc")
{
echo "error-the page ".
"Does not exist in this site.
";
}
Else
{
Get the contents of the page and put it in a string
$fcontents = Join ("', File (" $page. Inc "));

Ignore color attributes, convert to ' ignore ' instead of ' color '

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

Remove "_blank" from hyperlinks
$fcontents = ereg_replace (' target=\ ' _blank\ "', '", $fcontents);

Replace tag
$fcontents = ereg_replace (', ', ', $fcontents);

Displays the absolute address of the URL
$fcontents = ereg_replace (']*>;([^]*) ',
'\\2(\\1)', $fcontents);

Convert relative links to absolute links
$fcontents = Ereg_replace (
']*> ([^]*) ',
"\\2(HTTP//$HTTP _host/\\1)";,
$fcontents);

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

Output page
Echo $fcontents;
}
?>








This is a convenient way to print the page is generated, we hope to be helpful.

(translated from Phpbulider/mark Spink)

http://www.bkjia.com/PHPjc/316009.html www.bkjia.com true http://www.bkjia.com/PHPjc/316009.html techarticle Many news and information sites provide a way to generate easy-to-print pages, the resulting page layout is more conducive to printer printout, this method is convenient for us ...

  • 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.