1<?php
2# $Id: starter_textline.php,v 1.1.2.1 2007/12/29 23:26:06 rjs Exp $
3# Starter text line:
4# Demonstrate various options for placing a text line
5#
6# Place a text line with different font sizes.
7# Output overlined, stroke out, and underlined text.
8# Output text and define character spacing, work spacing, or horizontal
9# scaling.
10# Output text with a defined fill color. Output text including its outlines
11# with a defined stroke color.
12# Place text into a box at various positions. Place text completely into a box
13# with automatic scaling if required.
14#
15# Required software: PDFlib Lite/PDFlib/PDFlib+PDI/PPS 7
16# Required data: none
17
18# This is where the data files are. Adjust as necessary.
19$searchpath = "../data";
20$title = "Starter Text Line";
21
22$x = 10;
23$xt = 280;
24$y = 800;
25$yoff = 50;
26$textline = "Giant Wing Paper Plane";
27
28try {
29    # create a new PDFlib object
30    $p = new PDFlib();
31
32    $p->set_parameter("SearchPath", $searchpath);
33
34    # This means we must check return values of load_font() etc.
35    $p->set_parameter("errorpolicy", "return");
36
37    # Set an output path according to the name of the topic
38    if ($p->begin_document("", "") == 0) {
39	die("Error: " .  $p->get_errmsg());
40    }
41
42    $p->set_info("Creator", "PDFlib Cookbook");
43    $buf = $title . '  $Revision: 1.1.2.1 $';
44    $p->set_info("Title", $buf);
45
46    # Start Page
47    $p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
48
49    # For PDFlib Lite: change "unicode" to "winansi"
50    $font = $p->load_font("Helvetica", "winansi", "");
51
52    if ($font == 0) {
53	die("Error: " .  $p->get_errmsg());
54    }
55
56    # Set the font with a font size of 14
57    $p->setfont($font, 14);
58
59
60    # Place the text line without any options applied
61    $p->fit_textline($textline, $x, $y, "");
62
63    # Output descriptive text
64    $p->fit_textline("fit_textline() without any options", $xt, $y,
65	"fontsize=12");
66
67
68    # Place the text with a different font size
69    $optlist = "fontsize=22";
70
71    $p->fit_textline($textline, $x, $y-=$yoff, $optlist); # sample text
72    $p->fit_textline($optlist, $xt, $y, "fontsize=12"); # description
73
74
75    # Place stroke out text
76    $optlist = "strikeout";
77
78    $p->fit_textline($textline, $x, $y-=$yoff, $optlist); # sample text
79    $p->fit_textline($optlist, $xt, $y, "fontsize=12"); # description
80
81
82    # Place underlined text
83    $optlist = "underline underlinewidth=7% underlineposition=-20%";
84
85    $p->fit_textline($textline, $x, $y-=$yoff, $optlist); # sample text
86    $p->fit_textline($optlist, $xt, $y, "fontsize=12"); # description
87
88
89    # Place overlined text
90    $optlist = "overline";
91
92    $p->fit_textline($textline, $x, $y-=$yoff, $optlist); # sample text
93    $p->fit_textline($optlist, $xt, $y, "fontsize=12"); # description
94
95
96    # Place the text with a horizontal scaling of 150%
97    $optlist = "horizscaling=150%";
98
99    $p->fit_textline($textline, $x, $y-=$yoff, $optlist); # sample text
100    $p->fit_textline($optlist, $xt, $y, "fontsize=12"); # description
101
102
103    # Place the text with a character spacing of 30% of the font size
104    $optlist = "charspacing=30%";
105
106    $p->fit_textline($textline, $x, $y-=$yoff, $optlist); # sample text
107    $p->fit_textline($optlist, $xt, $y, "fontsize=12"); # description
108
109
110    # Place the text with a word spacing of 50% of the font size
111    $optlist = "wordspacing=50%";
112
113    $p->fit_textline($textline, $x, $y-=$yoff, $optlist); # sample text
114    $p->fit_textline($optlist, $xt, $y, "fontsize=12"); # description
115
116
117    # Place the text with a different fill color
118    $optlist = "fillcolor={rgb 0.5 0.2 0.5}";
119
120    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);
121    $p->fit_textline($optlist, $xt, $y, "fontsize=12");
122
123
124    # Place the text including its outlines using a text rendering mode of
125    # 2 for "filling and stroking text" and different fill and stroke
126    # colors
127
128    $optlist =
129	"fontsize=22 fillcolor={rgb 0.6 0.3 0.6} strokecolor={gray 0} " .
130	"strokewidth=0.4 textrendering=2";
131
132    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);
133
134    # Output descriptive text
135    $p->fit_textline("fillcolor={rgb 0.6 0.3 0.6} strokecolor={gray 0} ",
136	$xt, $y+10, "fontsize=12");
137    $p->fit_textline("strokewidth=0.4 textrendering=2 fontsize=22",
138	$xt, $y-5, "fontsize=12");
139
140
141    # Place the text with its outlines using a text rendering mode of
142    # 1 for "stroking text" and a stroke color of black
143
144    $optlist =
145	"fontsize=22 strokecolor={gray 0} strokewidth=0.4 textrendering=1";
146
147    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);
148
149    # Output descriptive text
150    $p->fit_textline("strokecolor={gray 0} strokewidth=0.4", $xt, $y+10,
151	"fontsize=12");
152    $p->fit_textline("textrendering=1 fontsize=22", $xt, $y-=5,
153	"fontsize=12");
154
155
156    # Place the text in a box with default positioning and fitting
157    $optlist = "boxsize={200 20} showborder";
158
159    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);   # sample text
160    $p->fit_textline($optlist, $xt, $y+3, "fontsize=12"); # description
161
162
163    # Place the text in a box on the top right
164    $optlist = "boxsize={200 20} position={top right} showborder";
165
166    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);   # sample text
167    $p->fit_textline($optlist, $xt, $y+3, "fontsize=12"); # description
168
169
170    # Use "fitmethod=clip" to place the text in a box not large enough to
171    # show the complete text. The text will be clipped.
172
173    $optlist = "boxsize={130 20} fitmethod=clip showborder";
174
175    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);   # sample text
176    $p->fit_textline($optlist, $xt, $y+3, "fontsize=12"); # description
177
178
179    # Fit the text into the box automatically with "fitmethod=auto".
180    # In this case, if the text doesn't fit into the box a distortion
181    # factor is calculated which makes the text fit into the box. If this
182    # factor is larger than the "shrinklimit" option the text will
183    # be distorted by that factor. Otherwise, the font size will be
184    # be decreased until until the text fits into the box.
185
186
187    # Use "fitmethod=auto" to place the text in a box not large enough to
188    # show the complete text. The text will be distorted.
189
190    $optlist = "boxsize={130 20} fitmethod=auto showborder";
191
192    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);   # sample text
193    $p->fit_textline($optlist, $xt, $y+3, "fontsize=12"); # description
194
195
196    # Use "fitmethod=auto" to place the text in a box too small to show the
197    # complete text. The font size will be reduced until the text fits into
198    # the box.
199
200    $optlist = "boxsize={100 20} fitmethod=auto showborder";
201
202    $p->fit_textline($textline, $x, $y-=$yoff, $optlist);   # sample text
203    $p->fit_textline($optlist, $xt, $y+3, "fontsize=12"); # description
204
205    $p->end_page_ext("");
206
207    $p->end_document("");
208
209    $buf = $p->get_buffer();
210    $len = strlen($buf);
211
212    header("Content-type: application/pdf");
213    header("Content-Length: $len");
214    header("Content-Disposition: inline; filename=hello.pdf");
215    print $buf;
216}
217catch (PDFlibException $e) {
218    die("PDFlib exception occurred in hello sample:\n" .
219        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
220        $e->get_errmsg() . "\n");
221}
222catch (Exception $e) {
223    die($e);
224}
225
226$p = 0;
227
228?>
229