Home Backend Development PHP Tutorial PHP生成便于打印的网页

PHP生成便于打印的网页

Jun 01, 2016 pm 02:30 PM

很多新闻和信息站点都提供了一种生成便于打印的网页的方法,所产生的页面的排版布局更有利于打印机的打印输出,这种方法方便了我们从网页上直接打印我们所需的内容,而不必为格式不规整伤脑筋,或者粘贴到文本编辑器中重新排版。然而,我却没看到有多少网站详细解释这些是如何实现的,在这里我提供一小段代码——用php来实现生成便于打印的网页并不是像想象的那么难,希望对大家有帮助。

要生成便于打印的网页,需要我们做哪些工作呢?这主要取决于你的网站特点,和你想要生成的版式特征,不过有一些基本处理需要完成:

1、  页宽——生成页面的宽度必须限制,要打印A4的纸,大约网页要在630像素宽。
2、  页面背景色——为了美观,很多网页使用了不同的背景色和背景图片,但是作为要打印的网页,最合适效果的还是白底黑字为好。
3、  广告条——移除页面上的广告
4、  表格的背景色——我们经常在表格中用颜色来强调信息和标题,这些也必须移除。
5、  链接——页面中的超链接也必须改变以使URL可见,例如:GBDirect应显示为GBDirect (http://www.gbdirect.co.uk/) 
6、  菜单——菜单是最难被禁止的,然而如果你的页面是使用模板来构建的话,那么最简单的方法是换用便于打印的没有菜单的模板。

这些生成便于打印页面的所有方法,都是非常简单的,需要实现的时候你可以被下面的代码放到网页中:

//从环境变量中得到文件的相对路径
$page=substr($SCRipT_NAME,1);

// 显示一个图标并连接到PRinter Friendly Pages
// 便于打印页面的生成程序pfp.php
?>
;
 PHP生成便于打印的网页   alt="Click here to produce a printer friendly page">

 
  Printer Friendly Version
 

  
把当前页面的名称传递到pfp.php程序中,这个程序使用PHP的“file”函数把页面作为一个字符串来处理。当这个页面被载入的时候,程序就可以增加、改写或删除HTML片段。


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 =$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"); ?>
  

 

 


这样便于打印的页面就生成了,希望对大家能有帮助。

(译自PHPBulider/Mark Spink) 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov

See all articles