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 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, <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 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.
?>
<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. 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 );
?>
<Html>
<Head>
<Base href = "http: // <? Echo $ HTTP_HOST?> /">
<Meta name = "robots" content = "no index, no follow">
<Title> Printer Friendly Page </title>
</Head>
<Body bgcolor = "white">
<Font face = "Arial, Helvetica">
<Table border = "0" cellpadding = "5" cellspacing = "0" width = "630">
<Tr>
<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 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 </a>
$ Fcontents = ereg_replace ('</a>', '', $ fcontents );
// Display the absolute URL
$ Fcontents = ereg_replace ('<a [^ h] * href = "(http: // [^"] *) "[^>] *>; ([^] *) ',
'<Strong> \ 2 </strong> <em> (\ 1) </em>', $ fcontents );
// Convert the relative link to an absolute link
$ Fcontents = ereg_replace (
'<A [^ h] * href = "([^"] *) "[^>] *> ([^] *)',
"<Strong> \ 2 </strong> <em> (http: // $ HTTP_HOST/\ 1) </em> ";,
$ Fcontents );
// Change the background color to white
$ Fcontents = ereg_replace ('<body bgignore', '<body bgcolor', $ fcontents );
// If any markers left restore link end element
$ Fcontents = ereg_replace ('', '</a>', $ fcontents );
// Output page
Echo $ fcontents;
}
?>
</Td>
</Tr>
<Tr>
<Td align = "center"> </Tr>
<Tr>
<Td align = "center">
<? Include ("$ page_path/footer. inc");?>
</Td>
</Tr>
</Table>
</Font>
</Body>
</Html>
In this way, the printed page is generated, hoping to help you.
(Translated from PHPBulider/Mark Spink)