Search:

# CWIS Developers Documentation

- Main Page
- Classes
- Files

- File List
- File Members

# Axis--PSDocument.php

Go to the documentation of this file.

    00001 <?PHP
    00002
    00003 #
    00004 #   Axis--PSDocument.php
    00005 #   PHP Objects for Generating PostScript and PDF Documents
    00006 #
    00007 #   Copyright 1999-2001 Axis Data
    00008 #   This code is free software that can be used or redistributed under the
    00009 #   terms of Version 2 of the GNU General Public License, as published by the
    00010 #   Free Software Foundation (http://www.fsf.org).
    00011 #
    00012 #   Author:  Edward Almasy (almasy@axisdata.com)
    00013 #
    00014 #   Part of the AxisPHP library v1.2.4
    00015 #   For more information see http://www.axisdata.com/AxisPHP/
    00016 #
    00017
    00018
    00019 class PSDocument {
    00020
    00021     # ---- PUBLIC INTERFACE --------------------------------------------------
    00022
    00023     # object constructor
    00024     function PSDocument()
    00025     {
    00026         # print document header
    00027         $this->PrintRaw("
    00028                 gsave
    00029                 ");
    00030
    00031         # set default font for document
    00032         $this->SetFont("Times-Roman", 12);
    00033
    00034         # set reasonable default starting coordinates
    00035         $this->MoveTo(100, 100);
    00036     }
    00037
    00038     function NextPage()
    00039     {
    00040         # increment our internal page number value
    00041         $CurrentPageNumber = $this->PageNumber();
    00042         $CurrentPageNumber++;
    00043         $this->PageNumber($CurrentPageNumber++);
    00044     }
    00045
    00046     function NextLine()
    00047     {
    00048         $SpacingMultiplier = 1.35;
    00049
    00050         if ($this->TextAngle == 0)
    00051         {
    00052             $this->YPos -= (int)($this->GetFontHeight() * $SpacingMultiplier);
    00053         }
    00054         elseif ($this->TextAngle == 90)
    00055         {
    00056             $this->XPos -= (int)($this->GetFontHeight() * $SpacingMultiplier);
    00057         }
    00058     }
    00059
    00060     function MoveToX($NewXPos)
    00061     {
    00062         $this->XPos = $NewXPos;
    00063     }
    00064
    00065     function MoveToY($NewYPos)
    00066     {
    00067         $this->YPos = $NewYPos;
    00068     }
    00069
    00070     function MoveToRelX($NewXPos)
    00071     {
    00072         $this->XPos = $this->CurrentXPos() + $NewXPos;
    00073     }
    00074
    00075     function MoveToRelY($NewYPos)
    00076     {
    00077         $this->YPos = $this->CurrentYPos() + $NewYPos;
    00078     }
    00079
    00080     function MoveTo($NewXPos, $NewYPos)
    00081     {
    00082         $this->MoveToX($NewXPos);
    00083         $this->MoveToY($NewYPos);
    00084     }
    00085
    00086     function CurrentXPos()
    00087     {
    00088         return $this->XPos;
    00089     }
    00090
    00091     function CurrentYPos()
    00092     {
    00093         return $this->YPos;
    00094     }
    00095
    00096     function WritePostscriptToFile($FileName)
    00097     {
    00098         # open output file
    00099         $OutputFilePointer = fopen($FileName, "w+") or die("unable to open PostScript output file ".$FileName);
    00100
    00101         # write out document header
    00102         fwrite($OutputFilePointer, "%!PS-Adobe-2.0
    00103                 %%Creator: AxisPHP
    00104                 %%Orientation: Landscape
    00105                 %%EndComments
    00106                 /UseFont { findfont exch scalefont setfont } bind def
    00107                 %%EndProlog
    00108                 ");
    00109
    00110         # for each page that has text
    00111         for ($Index = 1;  $Index <= $this->HighestPageNumber;  $Index++)
    00112         {
    00113             # if there is text on the page
    00114             if (strlen($this->PageText[$Index]) > 0)
    00115             {
    00116                 # write out page text
    00117                 fwrite($OutputFilePointer, $this->PageText[$Index]);
    00118
    00119                 # write out page footer
    00120                 fwrite($OutputFilePointer, "
    00121                         stroke
    00122                         grestore
    00123                         showpage
    00124                         ");
    00125             }
    00126         }
    00127
    00128         # write out document footer
    00129         fwrite($OutputFilePointer, "
    00130                 %%Trailer
    00131                 %%EOF
    00132                 ");
    00133
    00134         # close output file
    00135         fclose($OutputFilePointer);
    00136     }
    00137
    00138     function WritePDFToFile($OutputFileName)
    00139     {
    00140         # create PostScript file
    00141         $PSFileName = tempnam("/tmp", $FileNamePrefix) or die("unable to generate temporary file name for PostScript file for PDF generation");
    00142         $this->WritePostscriptToFile($PSFileName);
    00143
    00144         # build PostScript-to-PDF command string
    00145         $Command = sprintf("cat %s | gs -q -sDEVICE=pdfwrite -sOutputFile=%s - ",
    00146                 $PSFileName, $OutputFileName);
    00147
    00148         # run PostScript-to-PDF command
    00149         system($Command);
    00150
    00151         # remove PostScript file
    00152         system(sprintf("rm %s", $PSFileName));
    00153     }
    00154
    00155     function SetPrintCommand($NewPrintCommand)
    00156     {
    00157         $this->PrintCommand = $NewPrintCommand;
    00158     }
    00159
    00160     function PrintDocument($FileNamePrefix = "PSDocument")
    00161     {
    00162         # generate file name
    00163         $OutputFileName = tempnam("/tmp", $FileNamePrefix) or die("unable to generate temporary file name for PostScript file");
    00164
    00165         # dump document to file
    00166         $this->WritePostscriptToFile($OutputFileName);
    00167
    00168         # substitute file name into print command
    00169         $Command = str_replace("%f", $OutputFileName, $this->PrintCommand);
    00170
    00171         # issue print command
    00172         system(EscapeShellCmd($Command));
    00173
    00174         # return file name to caller
    00175         return $OutputFileName;
    00176     }
    00177
    00178     function SetPageSize($PointsHigh, $PointsWide)
    00179     {
    00180         $this->PageHeightInPoints = $PointsHigh;
    00181         $this->PageWidthInPoints = $PointsWide;
    00182     }
    00183
    00184     function SetTextWrapLength($NewLength = 0)
    00185     {
    00186         $this->TextWrapLength = $NewLength;
    00187     }
    00188
    00189     function SetFont($FontName, $FontSize)
    00190     {
    00191         $this->PrintRaw(sprintf("
    00192                 %s /%s UseFont
    00193                 ", $FontSize, $FontName));
    00194
    00195         $this->FontSize = $FontSize;
    00196     }
    00197
    00198     function GetFontHeight()
    00199     {
    00200         return (int)($this->FontSize * 0.8);
    00201     }
    00202
    00203     function PageNumber($NewPageNumber = -1)
    00204     {
    00205         if ($NewPageNumber != -1)
    00206         {
    00207             $this->PageNumber = $NewPageNumber;
    00208
    00209             if ($this->PageNumber > $this->HighestPageNumber)
    00210             {
    00211                 $this->HighestPageNumber = $this->PageNumber;
    00212             }
    00213         }
    00214         return $this->PageNumber;
    00215     }
    00216
    00217     function PrintText($TextToPrint)
    00218     {
    00219         # trim off any leading or trailing whitespace in string
    00220         $TextToPrint = trim($TextToPrint);
    00221
    00222         # split string into pieces delineated by newlines
    00223         $TextArray = split("\n", $TextToPrint);
    00224
    00225         # for each string in array
    00226         for ($Index = 0;  $Index < count($TextArray);  $Index++)
    00227         {
    00228             # trim off any leading or trailing whitespace in string
    00229             $Text = trim($TextArray[$Index]);
    00230
    00231             # if string is not empty
    00232             if (strlen($Text) > 0)
    00233             {
    00234                 # if text wrap length is set and string is longer than that
    00235                 if (($this->TextWrapLength > 0)
    00236                         && (strlen($Text) > $this->TextWrapLength))
    00237                 {
    00238                     # append portion of string beyond wrap len to next string
    00239                     $TextArray[$Index + 1] = substr($Text, $this->TextWrapLength)
    00240                             ." ".$TextArray[$Index + 1];
    00241
    00242                     # trim off portion of string beyond wrap len
    00243                     $Text = substr($Text, 0, $this->TextWrapLength);
    00244                 }
    00245
    00246                 # escape any Postscript delimiters in string
    00247                 $Text = str_replace("(", "\(", $Text);
    00248                 $Text = str_replace(")", "\)", $Text);
    00249
    00250                 # print string piece
    00251                 if ($this->TextAngle == 0)
    00252                 {
    00253                     $this->PrintRaw(sprintf("%s %s moveto\n",
    00254                             (int)$this->XPos,
    00255                             (int)($this->YPos - $this->GetFontHeight())));
    00256                 }
    00257                 elseif ($this->TextAngle == 90)
    00258                 {
    00259                     $this->PrintRaw(sprintf("%s %s moveto\n",
    00260                             (int)($this->XPos - $this->GetFontHeight()),
    00261                             (int)$this->YPos));
    00262                 }
    00263                 if ($this->TextAngle != 0)
    00264                 {
    00265                     $this->PrintRaw(sprintf("-%s rotate\n",
    00266                             (int)$this->TextAngle));
    00267                 }
    00268                 $this->PrintRaw(sprintf("(%s) show\n", $Text));
    00269                 if ($this->TextAngle != 0)
    00270                 {
    00271                     $this->PrintRaw(sprintf("%s rotate\n",
    00272                             (int)$this->TextAngle));
    00273                 }
    00274             }
    00275
    00276             # if not last string
    00277             if ($Index < (count($TextArray) - 1))
    00278             {
    00279                 # move to next row
    00280                 $this->NextLine();
    00281             }
    00282         }
    00283     }
    00284
    00285     function PrintTextAt($XPos, $YPos, $TextToPrint)
    00286     {
    00287         $this->MoveTo($XPos, $YPos);
    00288         $this->PrintText($TextToPrint);
    00289     }
    00290
    00291     function TextAngle($NewAngle = -1)
    00292     {
    00293         if ($NewAngle != -1)
    00294         {
    00295             $this->TextAngle = $NewAngle % 360;
    00296         }
    00297
    00298         return $this->TextAngle;
    00299     }
    00300
    00301     function UseLandscapeOrientation()
    00302     {
    00303         $this->PrintRaw(sprintf("
    00304                 90 rotate
    00305                 0 %s translate
    00306                 ", (0 - $this->PageWidthInPoints)));
    00307     }
    00308
    00309     function DefineStyle($StyleName, $FontName, $FontSize)
    00310     {
    00311         $this->StyleInfo[$StyleName]["FontName"] = $FontName;
    00312         $this->StyleInfo[$StyleName]["FontSize"] = $FontSize;
    00313     }
    00314
    00315     function UseStyle($StyleName)
    00316     {
    00317         $this->PrintRaw(sprintf("
    00318                 %s /%s UseFont
    00319                 ",
    00320                 $this->StyleInfo[$StyleName]["FontSize"],
    00321                 $this->StyleInfo[$StyleName]["FontName"]));
    00322
    00323         $this->FontSize = $this->StyleInfo[$StyleName]["FontSize"];
    00324     }
    00325
    00326     function PrintRaw($TextToPrint)
    00327     {
    00328         # add string to page text
    00329         $this->PageText[$this->PageNumber] .= $TextToPrint;
    00330     }
    00331
    00332
    00333     # ---- PRIVATE INTERFACE -------------------------------------------------
    00334
    00335     # array of text for each page
    00336     var $PageText;
    00337
    00338     # current print position
    00339     var $XPos = 0;
    00340     var $YPos = 0;
    00341
    00342     # current page number
    00343     var $PageNumber = 1;
    00344
    00345     # highest page number with text on it
    00346     var $HighestPageNumber = 1;
    00347
    00348     # current text rotation angle
    00349     var $TextAngle = 0;
    00350
    00351     # values for last table printed
    00352     var $TableXPos = 0;
    00353     var $TableYPos = 0;
    00354     var $TableRowHeight = 0;
    00355     var $TableColWidth = 0;
    00356
    00357     # values for last font set
    00358     var $FontHeight = 12;
    00359
    00360     # default to letter size (792x612)
    00361     var $PageHeightInPoints = 792;
    00362     var $PageWidthInPoints = 612;
    00363
    00364     # printing command
    00365     var $PrintCommand = "lpr %f";
    00366
    00367     # font style settings
    00368     var $StyleInfo;
    00369
    00370     # current string wrap length
    00371     var $TextWrapLength = 0;
    00372 }
    00373
    00374 ?>

Copyright 2010 Internet Scout