CWIS Developer Documentation

- Home
- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

PSDocument.php
Go to the documentation of this file.
1 <?PHP
2
3 #
4 # Axis--PSDocument.php
5 # PHP Objects for Generating PostScript and PDF Documents
6 #
7 # Copyright 1999-2001 Axis Data
8 # This code is free software that can be used or redistributed under the
9 # terms of Version 2 of the GNU General Public License, as published by the
10 # Free Software Foundation (http://www.fsf.org).
11 #
12 # Author: Edward Almasy (almasy@axisdata.com)
13 #
14 # Part of the AxisPHP library v1.2.4
15 # For more information see http://www.axisdata.com/AxisPHP/
16 #
17
18
19 class PSDocument {
20
21 # ---- PUBLIC INTERFACE --------------------------------------------------
22
23 # object constructor
24 function PSDocument()
25 {
26 # print document header
27 $this->PrintRaw("
28 gsave
29 ");
30
31 # set default font for document
32 $this->SetFont("Times-Roman", 12);
33
34 # set reasonable default starting coordinates
35 $this->MoveTo(100, 100);
36 }
37
38 function NextPage()
39 {
40 # increment our internal page number value
41 $CurrentPageNumber = $this->PageNumber();
42 $CurrentPageNumber++;
43 $this->PageNumber($CurrentPageNumber++);
44 }
45
46 function NextLine()
47 {
48 $SpacingMultiplier = 1.35;
49
50 if ($this->TextAngle == 0)
51 {
52 $this->YPos -= (int)($this->GetFontHeight() * $SpacingMultiplier);
53 }
54 elseif ($this->TextAngle == 90)
55 {
56 $this->XPos -= (int)($this->GetFontHeight() * $SpacingMultiplier);
57 }
58 }
59
60 function MoveToX($NewXPos)
61 {
62 $this->XPos = $NewXPos;
63 }
64
65 function MoveToY($NewYPos)
66 {
67 $this->YPos = $NewYPos;
68 }
69
70 function MoveToRelX($NewXPos)
71 {
72 $this->XPos = $this->CurrentXPos() + $NewXPos;
73 }
74
75 function MoveToRelY($NewYPos)
76 {
77 $this->YPos = $this->CurrentYPos() + $NewYPos;
78 }
79
80 function MoveTo($NewXPos, $NewYPos)
81 {
82 $this->MoveToX($NewXPos);
83 $this->MoveToY($NewYPos);
84 }
85
86 function CurrentXPos()
87 {
88 return $this->XPos;
89 }
90
91 function CurrentYPos()
92 {
93 return $this->YPos;
94 }
95
96 function WritePostscriptToFile($FileName)
97 {
98 # open output file
99 $OutputFilePointer = fopen($FileName, "w+") or die("unable to open PostScript output file ".$FileName);
100
101 # write out document header
102 fwrite($OutputFilePointer, "%!PS-Adobe-2.0
103 %%Creator: AxisPHP
104 %%Orientation: Landscape
105 %%EndComments
106 /UseFont { findfont exch scalefont setfont } bind def
107 %%EndProlog
108 ");
109
110 # for each page that has text
111 for ($Index = 1; $Index <= $this->HighestPageNumber; $Index++)
112 {
113 # if there is text on the page
114 if (strlen($this->PageText[$Index]) > 0)
115 {
116 # write out page text
117 fwrite($OutputFilePointer, $this->PageText[$Index]);
118
119 # write out page footer
120 fwrite($OutputFilePointer, "
121 stroke
122 grestore
123 showpage
124 ");
125 }
126 }
127
128 # write out document footer
129 fwrite($OutputFilePointer, "
130 %%Trailer
131 %%EOF
132 ");
133
134 # close output file
135 fclose($OutputFilePointer);
136 }
137
138 function WritePDFToFile($OutputFileName)
139 {
140 # create PostScript file
141 $PSFileName = tempnam("/tmp", $FileNamePrefix) or die("unable to generate temporary file name for PostScript file for PDF generation");
142 $this->WritePostscriptToFile($PSFileName);
143
144 # build PostScript-to-PDF command string
145 $Command = sprintf("cat %s | gs -q -sDEVICE=pdfwrite -sOutputFile=%s - ",
146 $PSFileName, $OutputFileName);
147
148 # run PostScript-to-PDF command
149 system($Command);
150
151 # remove PostScript file
152 system(sprintf("rm %s", $PSFileName));
153 }
154
155 function SetPrintCommand($NewPrintCommand)
156 {
157 $this->PrintCommand = $NewPrintCommand;
158 }
159
160 function PrintDocument($FileNamePrefix = "PSDocument")
161 {
162 # generate file name
163 $OutputFileName = tempnam("/tmp", $FileNamePrefix) or die("unable to generate temporary file name for PostScript file");
164
165 # dump document to file
166 $this->WritePostscriptToFile($OutputFileName);
167
168 # substitute file name into print command
169 $Command = str_replace("%f", $OutputFileName, $this->PrintCommand);
170
171 # issue print command
172 system(EscapeShellCmd($Command));
173
174 # return file name to caller
175 return $OutputFileName;
176 }
177
178 function SetPageSize($PointsHigh, $PointsWide)
179 {
180 $this->PageHeightInPoints = $PointsHigh;
181 $this->PageWidthInPoints = $PointsWide;
182 }
183
184 function SetTextWrapLength($NewLength = 0)
185 {
186 $this->TextWrapLength = $NewLength;
187 }
188
189 function SetFont($FontName, $FontSize)
190 {
191 $this->PrintRaw(sprintf("
192 %s /%s UseFont
193 ", $FontSize, $FontName));
194
195 $this->FontSize = $FontSize;
196 }
197
198 function GetFontHeight()
199 {
200 return (int)($this->FontSize * 0.8);
201 }
202
203 function PageNumber($NewPageNumber = -1)
204 {
205 if ($NewPageNumber != -1)
206 {
207 $this->PageNumber = $NewPageNumber;
208
209 if ($this->PageNumber > $this->HighestPageNumber)
210 {
211 $this->HighestPageNumber = $this->PageNumber;
212 }
213 }
214 return $this->PageNumber;
215 }
216
217 function PrintText($TextToPrint)
218 {
219 # trim off any leading or trailing whitespace in string
220 $TextToPrint = trim($TextToPrint);
221
222 # split string into pieces delineated by newlines
223 $TextArray = split("\n", $TextToPrint);
224
225 # for each string in array
226 for ($Index = 0; $Index < count($TextArray); $Index++)
227 {
228 # trim off any leading or trailing whitespace in string
229 $Text = trim($TextArray[$Index]);
230
231 # if string is not empty
232 if (strlen($Text) > 0)
233 {
234 # if text wrap length is set and string is longer than that
235 if (($this->TextWrapLength > 0)
236 && (strlen($Text) > $this->TextWrapLength))
237 {
238 # append portion of string beyond wrap len to next string
239 $TextArray[$Index + 1] = substr($Text, $this->TextWrapLength)
240 ." ".$TextArray[$Index + 1];
241
242 # trim off portion of string beyond wrap len
243 $Text = substr($Text, 0, $this->TextWrapLength);
244 }
245
246 # escape any Postscript delimiters in string
247 $Text = str_replace("(", "\(", $Text);
248 $Text = str_replace(")", "\)", $Text);
249
250 # print string piece
251 if ($this->TextAngle == 0)
252 {
253 $this->PrintRaw(sprintf("%s %s moveto\n",
254 (int)$this->XPos,
255 (int)($this->YPos - $this->GetFontHeight())));
256 }
257 elseif ($this->TextAngle == 90)
258 {
259 $this->PrintRaw(sprintf("%s %s moveto\n",
260 (int)($this->XPos - $this->GetFontHeight()),
261 (int)$this->YPos));
262 }
263 if ($this->TextAngle != 0)
264 {
265 $this->PrintRaw(sprintf("-%s rotate\n",
266 (int)$this->TextAngle));
267 }
268 $this->PrintRaw(sprintf("(%s) show\n", $Text));
269 if ($this->TextAngle != 0)
270 {
271 $this->PrintRaw(sprintf("%s rotate\n",
272 (int)$this->TextAngle));
273 }
274 }
275
276 # if not last string
277 if ($Index < (count($TextArray) - 1))
278 {
279 # move to next row
280 $this->NextLine();
281 }
282 }
283 }
284
285 function PrintTextAt($XPos, $YPos, $TextToPrint)
286 {
287 $this->MoveTo($XPos, $YPos);
288 $this->PrintText($TextToPrint);
289 }
290
291 function TextAngle($NewAngle = -1)
292 {
293 if ($NewAngle != -1)
294 {
295 $this->TextAngle = $NewAngle % 360;
296 }
297
298 return $this->TextAngle;
299 }
300
301 function UseLandscapeOrientation()
302 {
303 $this->PrintRaw(sprintf("
304 90 rotate
305 0 %s translate
306 ", (0 - $this->PageWidthInPoints)));
307 }
308
309 function DefineStyle($StyleName, $FontName, $FontSize)
310 {
311 $this->StyleInfo[$StyleName]["FontName"] = $FontName;
312 $this->StyleInfo[$StyleName]["FontSize"] = $FontSize;
313 }
314
315 function UseStyle($StyleName)
316 {
317 $this->PrintRaw(sprintf("
318 %s /%s UseFont
319 ",
320 $this->StyleInfo[$StyleName]["FontSize"],
321 $this->StyleInfo[$StyleName]["FontName"]));
322
323 $this->FontSize = $this->StyleInfo[$StyleName]["FontSize"];
324 }
325
326 function PrintRaw($TextToPrint)
327 {
328 # add string to page text
329 $this->PageText[$this->PageNumber] .= $TextToPrint;
330 }
331
332
333 # ---- PRIVATE INTERFACE -------------------------------------------------
334
335 # array of text for each page
336 var $PageText;
337
338 # current print position
339 var $XPos = 0;
340 var $YPos = 0;
341
342 # current page number
343 var $PageNumber = 1;
344
345 # highest page number with text on it
346 var $HighestPageNumber = 1;
347
348 # current text rotation angle
349 var $TextAngle = 0;
350
351 # values for last table printed
352 var $TableXPos = 0;
353 var $TableYPos = 0;
354 var $TableRowHeight = 0;
355 var $TableColWidth = 0;
356
357 # values for last font set
358 var $FontHeight = 12;
359
360 # default to letter size (792x612)
361 var $PageHeightInPoints = 792;
362 var $PageWidthInPoints = 612;
363
364 # printing command
365 var $PrintCommand = "lpr %f";
366
367 # font style settings
368 var $StyleInfo;
369
370 # current string wrap length
371 var $TextWrapLength = 0;
372 }
373
374 ?>
PSDocument
Definition: PSDocument.php:19
PSDocument\$XPos
$XPos
Definition: PSDocument.php:339
PSDocument\WritePDFToFile
WritePDFToFile($OutputFileName)
Definition: PSDocument.php:138
PSDocument\CurrentYPos
CurrentYPos()
Definition: PSDocument.php:91
PSDocument\NextPage
NextPage()
Definition: PSDocument.php:38
PSDocument\$TextAngle
$TextAngle
Definition: PSDocument.php:349
PSDocument\SetTextWrapLength
SetTextWrapLength($NewLength=0)
Definition: PSDocument.php:184
PSDocument\$FontHeight
$FontHeight
Definition: PSDocument.php:358
PSDocument\$PrintCommand
$PrintCommand
Definition: PSDocument.php:365
PSDocument\$PageHeightInPoints
$PageHeightInPoints
Definition: PSDocument.php:361
PSDocument\$PageNumber
$PageNumber
Definition: PSDocument.php:343
PSDocument\SetFont
SetFont($FontName, $FontSize)
Definition: PSDocument.php:189
PSDocument\$StyleInfo
$StyleInfo
Definition: PSDocument.php:368
PSDocument\SetPrintCommand
SetPrintCommand($NewPrintCommand)
Definition: PSDocument.php:155
PSDocument\PrintDocument
PrintDocument($FileNamePrefix="PSDocument")
Definition: PSDocument.php:160
PSDocument\PrintTextAt
PrintTextAt($XPos, $YPos, $TextToPrint)
Definition: PSDocument.php:285
PSDocument\$YPos
$YPos
Definition: PSDocument.php:340
PSDocument\MoveToRelX
MoveToRelX($NewXPos)
Definition: PSDocument.php:70
PSDocument\WritePostscriptToFile
WritePostscriptToFile($FileName)
Definition: PSDocument.php:96
PSDocument\MoveToY
MoveToY($NewYPos)
Definition: PSDocument.php:65
PSDocument\GetFontHeight
GetFontHeight()
Definition: PSDocument.php:198
PSDocument\UseLandscapeOrientation
UseLandscapeOrientation()
Definition: PSDocument.php:301
PSDocument\PageNumber
PageNumber($NewPageNumber=-1)
Definition: PSDocument.php:203
PSDocument\PrintRaw
PrintRaw($TextToPrint)
Definition: PSDocument.php:326
PSDocument\NextLine
NextLine()
Definition: PSDocument.php:46
PSDocument\$TextWrapLength
$TextWrapLength
Definition: PSDocument.php:371
PSDocument\MoveToRelY
MoveToRelY($NewYPos)
Definition: PSDocument.php:75
PHP
PHP
Definition: RSSClient--Test.php:12
PSDocument\TextAngle
TextAngle($NewAngle=-1)
Definition: PSDocument.php:291
PSDocument\$TableXPos
$TableXPos
Definition: PSDocument.php:352
PSDocument\CurrentXPos
CurrentXPos()
Definition: PSDocument.php:86
PSDocument\UseStyle
UseStyle($StyleName)
Definition: PSDocument.php:315
PSDocument\MoveToX
MoveToX($NewXPos)
Definition: PSDocument.php:60
PSDocument\$TableColWidth
$TableColWidth
Definition: PSDocument.php:355
PSDocument\DefineStyle
DefineStyle($StyleName, $FontName, $FontSize)
Definition: PSDocument.php:309
PSDocument\PSDocument
PSDocument()
Definition: PSDocument.php:24
PSDocument\$TableYPos
$TableYPos
Definition: PSDocument.php:353
PSDocument\$HighestPageNumber
$HighestPageNumber
Definition: PSDocument.php:346
PSDocument\MoveTo
MoveTo($NewXPos, $NewYPos)
Definition: PSDocument.php:80
PSDocument\$PageWidthInPoints
$PageWidthInPoints
Definition: PSDocument.php:362
PSDocument\PrintText
PrintText($TextToPrint)
Definition: PSDocument.php:217
PSDocument\$TableRowHeight
$TableRowHeight
Definition: PSDocument.php:354
PSDocument\SetPageSize
SetPageSize($PointsHigh, $PointsWide)
Definition: PSDocument.php:178
PSDocument\$PageText
$PageText
Definition: PSDocument.php:336