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>
|
---|
10 | Let's continue with an example which prints justified paragraphs. It also illustrates the use
|
---|
11 | of colors.
|
---|
12 | <div class="source">
|
---|
13 | <pre><code><?php
|
---|
14 | <span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
|
---|
15 |
|
---|
16 | class </span>PDF <span class="kw">extends </span>FPDF
|
---|
17 | <span class="kw">{
|
---|
18 | function </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-></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-></span>GetStringWidth<span class="kw">(</span>$title<span class="kw">)+</span>6<span class="kw">;
|
---|
26 | </span>$<span class="kw">this-></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-></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-></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-></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-></span>SetLineWidth<span class="kw">(</span>1<span class="kw">);
|
---|
33 | </span><span class="cmt">//Title
|
---|
34 | </span>$<span class="kw">this-></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-></span>Ln<span class="kw">(</span>10<span class="kw">);
|
---|
37 | }
|
---|
38 |
|
---|
39 | function </span>Footer<span class="kw">()
|
---|
40 | {
|
---|
41 | </span><span class="cmt">//Position at 1.5 cm from bottom
|
---|
42 | </span>$<span class="kw">this-></span>SetY<span class="kw">(-</span>15<span class="kw">);
|
---|
43 | </span><span class="cmt">//Arial italic 8
|
---|
44 | </span>$<span class="kw">this-></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-></span>SetTextColor<span class="kw">(</span>128<span class="kw">);
|
---|
47 | </span><span class="cmt">//Page number
|
---|
48 | </span>$<span class="kw">this-></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-></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 |
|
---|
51 | function </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-></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-></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-></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-></span>Ln<span class="kw">(</span>4<span class="kw">);
|
---|
61 | }
|
---|
62 |
|
---|
63 | function </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-></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-></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-></span>Ln<span class="kw">();
|
---|
75 | </span><span class="cmt">//Mention in italics
|
---|
76 | </span>$<span class="kw">this-></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-></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 |
|
---|
80 | function </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-></span>AddPage<span class="kw">();
|
---|
83 | </span>$<span class="kw">this-></span>ChapterTitle<span class="kw">(</span>$num<span class="kw">,</span>$title<span class="kw">);
|
---|
84 | </span>$<span class="kw">this-></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">-></span>SetTitle<span class="kw">(</span>$title<span class="kw">);
|
---|
91 | </span>$pdf<span class="kw">-></span>SetAuthor<span class="kw">(</span><span class="str">'Jules Verne'</span><span class="kw">);
|
---|
92 | </span>$pdf<span class="kw">-></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">-></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">-></span>Output<span class="kw">();
|
---|
95 | </span>?></code></pre>
|
---|
96 | </div>
|
---|
97 | <p class='demo'><a href='tuto3.php' target='_blank' class='demo'>[Demo]</a></p>
|
---|
98 | The <a href='../doc/getstringwidth.htm'>GetStringWidth()</a> method allows to determine the length of a string in the current font,
|
---|
99 | which is used here to calculate the position and the width of the frame surrounding the title.
|
---|
100 | Then 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
|
---|
101 | thickness of the line is set to 1 mm (against 0.2 by default) with <a href='../doc/setlinewidth.htm'>SetLineWidth()</a>. Finally,
|
---|
102 | we output the cell (the last parameter to <code>true</code> indicates that the background must
|
---|
103 | be filled).
|
---|
104 | <br>
|
---|
105 | <br>
|
---|
106 | The method used to print the paragraphs is <a href='../doc/multicell.htm'>MultiCell()</a>. Each time a line reaches the
|
---|
107 | right extremity of the cell or a carriage return character is met, a line break is issued
|
---|
108 | and a new cell automatically created under the current one. Text is justified by default.
|
---|
109 | <br>
|
---|
110 | <br>
|
---|
111 | Two document properties are defined: the title (<a href='../doc/settitle.htm'>SetTitle()</a>) and the author (<a href='../doc/setauthor.htm'>SetAuthor()</a>).
|
---|
112 | Properties can be viewed by two means. First is to open the document directly with Acrobat Reader,
|
---|
113 | go to the File menu and choose the Document Properties option. The second, also available from
|
---|
114 | the plug-in, is to right-click and select Document Properties.
|
---|
115 | </body>
|
---|
116 | </html>
|
---|