PHP generates pages that are easy to print

Source: Internet
Author: User
Tags relative
Many news and information sites offer a way to generate pages that are easy to print, the resulting layout of the page is more conducive to the printing output of the printer, which makes it easier for us to print the content we need directly from the page, without having to worry about formatting, or pasting it into a text editor. However, I do not see how many sites explain how these are implemented, here I provide a small piece of code--the use of PHP to achieve the production of easy to print pages is not as difficult as it is, I hope to help.

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

1, page width-the width of the generated page must be limited to print A4 paper, about 630 pixels wide page.
2, page background color-for the beautiful, many pages use a different background color and background picture, but as the page to be printed, the most appropriate effect or white background black word for good.
3, advertising strip-Remove the ads on the page
4, the background color of the table-we often use color in the table to emphasize information and titles, these must also be removed.
5, Links--The hyperlinks on the page must also be changed to make the URLs visible, for example: <a href=http://www.gbdirect.co.uk/>GBDirect</a> 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 templates, then the easiest way is to swap with a template that is easy to print without a menu.

All of the methods that make it easy to print a page are very simple, and when you need to implement it, you can put it in a Web page with the following code:
?
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 a page's build program pfp.php
?>
<a href= "pfp.php?page=<?= $page?>" >;
alt= "Click here to produce a printer friendly page" >

<font face= "Arial, Helvetica" size= "2" >
Printer Friendly Version
</font>
</a>
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 this page is loaded, the program can add, overwrite, or delete HTML fragments.

?
Ereg (' ^.*/', $SCRIPT _filename, $tmp);
$page _path = substr ($tmp [0],0,-1);
?>


<base href= "HTTP://&LT;? echo $HTTP _host >/">
<meta name= "Robots" content= "no index, no follow" >
<title>printer Friendly page</title>

<body bgcolor= "White" >

<font face= "Arial,helvetica" >

<table border= "0" cellpadding= "5" cellspacing= "0" width= "630" >

<tr>
&LT;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
{
Get the content of the page and put it in a string
$fcontents = Join (' ", File (" $page. Inc ");

Ignore the color attribute and convert to ' ignore ' instead of ' color '

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

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

Replace </a> Mark
$fcontents = ereg_replace (' </a> ', ', ', $fcontents);

Displays the absolute address of the URL
$fcontents = ereg_replace (' <a[^h]*href= "(http://[^"]*) "[^>]*>;([^]*) ',
' <strong>\\2</strong><em> (\\1) </em> ', $fcontents);

Convert relative links to absolute links
$fcontents = Ereg_replace (
' <a[^h]*href= ' ([^ "]*)" [^>]*> ([^]*) ',
"<strong>\\2</strong><em> (http://$HTTP _host/\\1) </em>";,
$fcontents);

Change the background color back to white
$fcontents = ereg_replace (' <body bgignore ', ' <body bgcolor ', $fcontents);

If any markers the left restore link end element
$fcontents = ereg_replace (', ' </a> ', $fcontents);

Output page
Echo $fcontents;
}
?>
</td>
</tr>

<tr>
&LT;TD align= "center" ></tr>

<tr>
&LT;TD align= "center" >
? Include ("$page _path/footer.inc");?>
</td>
</tr>

</table>

</font>

</body>
This easy to print the page is generated, hope for everyone to be helpful.


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.