source: trunk/client/inc/hpdf/_fpdf/tutorial/tuto3.htm@ 2

Last change on this file since 2 was 2, checked in by root, 15 years ago

importo il progetto

File size: 8.8 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5<title>Line breaks and colors</title>
6<link type="text/css" rel="stylesheet" href="../fpdf.css">
7</head>
8<body>
9<h1>Line breaks and colors</h1>
10Let's continue with an example which prints justified paragraphs. It also illustrates the use
11of colors.
12<div class="source">
13<pre><code>&lt;?php
14<span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
15
16class </span>PDF <span class="kw">extends </span>FPDF
17<span class="kw">{
18function </span>Header<span class="kw">()
19{
20 global </span>$title<span class="kw">;
21
22 </span><span class="cmt">//Arial bold 15
23 </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">,</span>15<span class="kw">);
24 </span><span class="cmt">//Calculate width of title and position
25 </span>$w<span class="kw">=</span>$<span class="kw">this-&gt;</span>GetStringWidth<span class="kw">(</span>$title<span class="kw">)+</span>6<span class="kw">;
26 </span>$<span class="kw">this-&gt;</span>SetX<span class="kw">((</span>210<span class="kw">-</span>$w<span class="kw">)/</span>2<span class="kw">);
27 </span><span class="cmt">//Colors of frame, background and text
28 </span>$<span class="kw">this-&gt;</span>SetDrawColor<span class="kw">(</span>0<span class="kw">,</span>80<span class="kw">,</span>180<span class="kw">);
29 </span>$<span class="kw">this-&gt;</span>SetFillColor<span class="kw">(</span>230<span class="kw">,</span>230<span class="kw">,</span>0<span class="kw">);
30 </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>220<span class="kw">,</span>50<span class="kw">,</span>50<span class="kw">);
31 </span><span class="cmt">//Thickness of frame (1 mm)
32 </span>$<span class="kw">this-&gt;</span>SetLineWidth<span class="kw">(</span>1<span class="kw">);
33 </span><span class="cmt">//Title
34 </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>$w<span class="kw">,</span>9<span class="kw">,</span>$title<span class="kw">,</span>1<span class="kw">,</span>1<span class="kw">,</span><span class="str">'C'</span><span class="kw">,</span>true<span class="kw">);
35 </span><span class="cmt">//Line break
36 </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>10<span class="kw">);
37}
38
39function </span>Footer<span class="kw">()
40{
41 </span><span class="cmt">//Position at 1.5 cm from bottom
42 </span>$<span class="kw">this-&gt;</span>SetY<span class="kw">(-</span>15<span class="kw">);
43 </span><span class="cmt">//Arial italic 8
44 </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">'I'</span><span class="kw">,</span>8<span class="kw">);
45 </span><span class="cmt">//Text color in gray
46 </span>$<span class="kw">this-&gt;</span>SetTextColor<span class="kw">(</span>128<span class="kw">);
47 </span><span class="cmt">//Page number
48 </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>0<span class="kw">,</span>10<span class="kw">,</span><span class="str">'Page '</span><span class="kw">.</span>$<span class="kw">this-&gt;</span>PageNo<span class="kw">(),</span>0<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
49}
50
51function </span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$label<span class="kw">)
52{
53 </span><span class="cmt">//Arial 12
54 </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>12<span class="kw">);
55 </span><span class="cmt">//Background color
56 </span>$<span class="kw">this-&gt;</span>SetFillColor<span class="kw">(</span>200<span class="kw">,</span>220<span class="kw">,</span>255<span class="kw">);
57 </span><span class="cmt">//Title
58 </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>0<span class="kw">,</span>6<span class="kw">,</span><span class="str">"Chapter $num : $label"</span><span class="kw">,</span>0<span class="kw">,</span>1<span class="kw">,</span><span class="str">'L'</span><span class="kw">,</span>true<span class="kw">);
59 </span><span class="cmt">//Line break
60 </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">(</span>4<span class="kw">);
61}
62
63function </span>ChapterBody<span class="kw">(</span>$file<span class="kw">)
64{
65 </span><span class="cmt">//Read text file
66 </span>$f<span class="kw">=</span>fopen<span class="kw">(</span>$file<span class="kw">,</span><span class="str">'r'</span><span class="kw">);
67 </span>$txt<span class="kw">=</span>fread<span class="kw">(</span>$f<span class="kw">,</span>filesize<span class="kw">(</span>$file<span class="kw">));
68 </span>fclose<span class="kw">(</span>$f<span class="kw">);
69 </span><span class="cmt">//Times 12
70 </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Times'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>12<span class="kw">);
71 </span><span class="cmt">//Output justified text
72 </span>$<span class="kw">this-&gt;</span>MultiCell<span class="kw">(</span>0<span class="kw">,</span>5<span class="kw">,</span>$txt<span class="kw">);
73 </span><span class="cmt">//Line break
74 </span>$<span class="kw">this-&gt;</span>Ln<span class="kw">();
75 </span><span class="cmt">//Mention in italics
76 </span>$<span class="kw">this-&gt;</span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span><span class="str">'I'</span><span class="kw">);
77 </span>$<span class="kw">this-&gt;</span>Cell<span class="kw">(</span>0<span class="kw">,</span>5<span class="kw">,</span><span class="str">'(end of excerpt)'</span><span class="kw">);
78}
79
80function </span>PrintChapter<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">,</span>$file<span class="kw">)
81{
82 </span>$<span class="kw">this-&gt;</span>AddPage<span class="kw">();
83 </span>$<span class="kw">this-&gt;</span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">);
84 </span>$<span class="kw">this-&gt;</span>ChapterBody<span class="kw">(</span>$file<span class="kw">);
85}
86}
87
88</span>$pdf<span class="kw">=new </span>PDF<span class="kw">();
89</span>$title<span class="kw">=</span><span class="str">'20000 Leagues Under the Seas'</span><span class="kw">;
90</span>$pdf<span class="kw">-&gt;</span>SetTitle<span class="kw">(</span>$title<span class="kw">);
91</span>$pdf<span class="kw">-&gt;</span>SetAuthor<span class="kw">(</span><span class="str">'Jules Verne'</span><span class="kw">);
92</span>$pdf<span class="kw">-&gt;</span>PrintChapter<span class="kw">(</span>1<span class="kw">,</span><span class="str">'A RUNAWAY REEF'</span><span class="kw">,</span><span class="str">'20k_c1.txt'</span><span class="kw">);
93</span>$pdf<span class="kw">-&gt;</span>PrintChapter<span class="kw">(</span>2<span class="kw">,</span><span class="str">'THE PROS AND CONS'</span><span class="kw">,</span><span class="str">'20k_c2.txt'</span><span class="kw">);
94</span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
95</span>?&gt;</code></pre>
96</div>
97<p class='demo'><a href='tuto3.php' target='_blank' class='demo'>[Demo]</a></p>
98The <a href='../doc/getstringwidth.htm'>GetStringWidth()</a> method allows to determine the length of a string in the current font,
99which is used here to calculate the position and the width of the frame surrounding the title.
100Then colors are set (via <a href='../doc/setdrawcolor.htm'>SetDrawColor()</a>, <a href='../doc/setfillcolor.htm'>SetFillColor()</a> and <a href='../doc/settextcolor.htm'>SetTextColor()</a>) and the
101thickness of the line is set to 1 mm (against 0.2 by default) with <a href='../doc/setlinewidth.htm'>SetLineWidth()</a>. Finally,
102we output the cell (the last parameter to <code>true</code> indicates that the background must
103be filled).
104<br>
105<br>
106The method used to print the paragraphs is <a href='../doc/multicell.htm'>MultiCell()</a>. Each time a line reaches the
107right extremity of the cell or a carriage return character is met, a line break is issued
108and a new cell automatically created under the current one. Text is justified by default.
109<br>
110<br>
111Two document properties are defined: the title (<a href='../doc/settitle.htm'>SetTitle()</a>) and the author (<a href='../doc/setauthor.htm'>SetAuthor()</a>).
112Properties can be viewed by two means. First is to open the document directly with Acrobat Reader,
113go to the File menu and choose the Document Properties option. The second, also available from
114the plug-in, is to right-click and select Document Properties.
115</body>
116</html>
Note: See TracBrowser for help on using the repository browser.