天天看點

利用PHP生成便于列印的網頁的方法

很多新聞和資訊站點都提供了一種生成便于列印的網頁的方法,所産生的頁面的排版布局更有利于列印機的列印輸出,這種方法友善了我們從網頁上直接列印我們所需的内容,而不必為格式不規整傷腦筋,或者粘貼到文本編輯器中重新排版。然而,我卻沒看到有多少網站詳細解釋這些是如何實作的,在這裡我提供一小段代碼——用PHP來實作生成便于列印的網頁并不是像想象的那麼難,希望對大家有幫助。 

要生成便于列印的網頁,需要我們做哪些工作呢?這主要取決于你的網站特點,和你想要生成的版式特征,不過有一些基本處理需要完成: 

1、 頁寬——生成頁面的寬度必須限制,要列印A4的紙,大約網頁要在630像素寬。 

2、 頁面背景色——為了美觀,很多網頁使用了不同的背景色和背景圖檔,但是作為要列印的網頁,最合适效果的還是白底黑字為好。 

3、 廣告條——移除頁面上的廣告 

4、 表格的背景色——我們經常在表格中用顔色來強調資訊和标題,這些也必須移除。 

5、 連結——頁面中的超連結也必須改變以使URL可見,例如:<a href=http://www.gbdirect.co.uk/ >GBDirect</a>應顯示為GBDirect (http://www.gbdirect.co.uk/) 

6、 菜單——菜單是最難被禁止的,然而如果你的頁面是使用模闆來建構的話,那麼最簡單的方法是換用便于列印的沒有菜單的模闆。 

這些生成便于列印頁面的所有方法,都是非常簡單的,需要實作的時候你可以被下面的代碼放到網頁中: 

1

2

3

4

5

6

7

8

9

10

11

12

13

<?

//從環境變量中得到檔案的相對路徑

$page

=

substr

(

$SCRIPT_NAME

,1);

// 顯示一個圖示并連接配接到Printer Friendly Pages

// 便于列印頁面的生成程式pfp.php

?>

<a href=

"pfp.php?page=<?=$page?>"

>;

<img src=

"printer.gif"

width=

"36"

height=

"36"

0"

alt=

"Click here to produce a printer friendly page"

>

<font arial, helvetica"

size=

"2"

>

Printer Friendly Version

</font>

</a>

把目前頁面的名稱傳遞到pfp.php程式中,這個程式使用PHP的“file”函數把頁面作為一個字元串來處理。當這個頁面被載入的時候,程式就可以增加、改寫或删除HTML片段。 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

<?

ereg

('^.*/',$SCRIPT_FILENAME,$tmp);

$

page_path

=

substr

($tmp[0],0,-1);

?>

<

html

>

<

head

>

<

base

href="http://<? echo $HTTP_HOST ?>/" target="_blank" rel="external nofollow" >

<

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

{

// 得到頁面的内容并把它放到一個字元串中

$fcontents = join('', file("$page.inc"));

// 忽略顔色屬性,轉換以'ignore'替代'color'

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

// 去除超連結中的 “_blank”

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

// 替換</

a

>标記

$fcontents = ereg_replace('</

a

>','',$fcontents);

// 顯示URL的絕對位址

$fcontents = ereg_replace('<

a

[^h]*

href

=

"(http://[^"

]*)"[^>]*>;([^]*)',

'<

strong

>\\2</

strong

><

em

>(\\1)</

em

>',$fcontents);

// 把相對連結轉為絕對連結

$fcontents = ereg_replace(

'<

a

[^h]*

href

=

"([^"

]*)"[^>]*>([^]*)',

"<

strong

>\\2</

strong

><

em

>(http://$HTTP_HOST/\\1)</

em

>";,

$fcontents);

// 背景顔色改回白色

$fcontents = ereg_replace('<

body

bgignore','<body bgcolor',  $fcontents);

// if any markers left restore link end element

$

fcontents

=

ereg_replace

('','</a>',$fcontents);

// 輸出頁面

echo $fcontents;

}

?>

</

td

>

</

tr

>

<

tr

>

<

td

align

=

"center"

><

hr

width

=

"90%"

></

td

>

</

tr

>

<

tr

>

<

td

align

=

"center"

>

<?

include

("$page_path/footer.inc"); ?>

</

td

>

</

tr

>

</

table

>

</

font

>

</

body

>

</

html

>

這樣便于列印的頁面就生成了,希望對大家能有幫助。