Class Cpdf

Description

Cpdf

http://www.ros.co.nz/pdf

A PHP class to provide the basic functionality to create a pdf document without any requirement for additional modules.

Note that they companion class CezPdf can be used to extend this class and dramatically simplify the creation of documents.

IMPORTANT NOTE there is no warranty, implied or otherwise with this software.

LICENCE This code has been placed in the Public Domain for all to enjoy.

Located in /lib/class.pdf.php (line 23)


	
			
Variable Summary
Method Summary
Cpdf Cpdf ([mixed $pageSize = array(0, 0, 612, 792)])
void addDestination (mixed $label, mixed $style, mixed $a, mixed $b, mixed $c)
void addImage (mixed &$img, mixed $x, mixed $y, mixed $w, mixed $h, [mixed $quality = 75])
void addInfo (mixed $label, mixed $value)
void addInternalLink (mixed $label, mixed $x0, mixed $y0, mixed $x1, mixed $y1)
void addJpegFromFile (mixed $img, mixed $x, mixed $y, mixed $w, mixed $h)
void addLink (mixed $url, mixed $x0, mixed $y0, mixed $x1, mixed $y1)
void addMessage (mixed $message)
void addObject (mixed $id, [mixed $options = 'add'])
void addPngFromFile (mixed $file, mixed $x, mixed $y, mixed $w, mixed $h)
void addText (mixed $x, mixed $y, mixed $size, mixed $text, mixed $angle, mixed $wordSpaceAdjust)
void addTextWrap (mixed $x, mixed $y, mixed $width, mixed $size, mixed $text, [mixed $justification = 'left'], mixed $angle, mixed $test)
void ARC4 (mixed $text)
void ARC4_init ([mixed $key = ''])
void checkAllHere ()
void closeObject ()
void curve (mixed $x0, mixed $y0, mixed $x1, mixed $y1, mixed $x2, mixed $y2, mixed $x3, mixed $y3)
void ellipse (mixed $x0, mixed $y0, mixed $r1, mixed $r2, mixed $angle, [mixed $nSeg = 8], mixed $astart, [mixed $afinish = 360], [mixed $close = 1], mixed $fill)
void encryptInit (mixed $id)
void filledEllipse (mixed $x0, mixed $y0, mixed $r1, mixed $r2, mixed $angle, [mixed $nSeg = 8], mixed $astart, [mixed $afinish = 360])
void filledRectangle (mixed $x1, mixed $y1, mixed $width, mixed $height)
void getFirstPageId ()
void getFontDecender (mixed $size)
void getFontHeight (mixed $size)
void getTextWidth (mixed $size, mixed $text, mixed $spacing)
void line (mixed $x1, mixed $y1, mixed $x2, mixed $y2)
void md5_16 (mixed $string)
void newPage (mixed $insert, mixed $id, [mixed $pos = 'after'])
void openHere (mixed $style, mixed $a, mixed $b, mixed $c)
void openObject ()
void output (mixed $debug)
void o_action (mixed $id, mixed $action, [mixed $options = ''])
void o_annotation (mixed $id, mixed $action, [mixed $options = ''])
void o_catalog (mixed $id, mixed $action, [mixed $options = ''])
void o_contents (mixed $id, mixed $action, [mixed $options = ''])
void o_destination (mixed $id, mixed $action, [mixed $options = ''])
void o_encryption (mixed $id, mixed $action, [mixed $options = ''])
void o_extGState (mixed $id, mixed $action, [mixed $options = ""])
void o_font (mixed $id, mixed $action, [mixed $options = ''])
void o_fontDescriptor (mixed $id, mixed $action, [mixed $options = ''])
void o_fontEncoding (mixed $id, mixed $action, [mixed $options = ''])
void o_image (mixed $id, mixed $action, [mixed $options = ''])
void o_info (mixed $id, mixed $action, [mixed $options = ''])
void o_outlines (mixed $id, mixed $action, [mixed $options = ''])
void o_page (mixed $id, mixed $action, [mixed $options = ''])
void o_pages (mixed $id, mixed $action, [mixed $options = ''])
void o_procset (mixed $id, mixed $action, [mixed $options = ''])
void o_viewerPreferences (mixed $id, mixed $action, [mixed $options = ''])
void partEllipse (mixed $x0, mixed $y0, mixed $astart, mixed $afinish, mixed $r1, mixed $r2, mixed $angle, [mixed $nSeg = 8])
void polygon (mixed $p, mixed $np, mixed $f)
void rectangle (mixed $x1, mixed $y1, mixed $width, mixed $height)
void reopenObject (mixed $id)
void restoreSerializedObject (mixed $obj)
void restoreState (mixed $pageEnd)
void saveState (mixed $pageEnd)
void selectFont (mixed $fontName, [mixed $encoding = ''], [mixed $set = 1])
void serializeObject (mixed $id)
void setColor (mixed $r, mixed $g, mixed $b, mixed $force)
void setEncryption ([mixed $userPass = ''], [mixed $ownerPass = ''], [mixed $pc = array()])
void setFillTransparency (string $mode, float $opacity)
void setFontFamily (mixed $family, [mixed $options = ''])
void setGraphicsState (mixed $parameters)
void setLineStyle ([mixed $width = 1], [mixed $cap = ''], [mixed $join = ''], [mixed $dash = ''], mixed $phase)
void setLineTransparency (string $mode, float $opacity)
void setPreferences (mixed $label, mixed $value)
void setStrokeColor (mixed $r, mixed $g, mixed $b, mixed $force)
void stopObject (mixed $id)
void stream ([mixed $options = ''])
void transaction (mixed $action)
Variables
mixed $addLooseObjects = array() (line 148)

array contains infomation about how the loose objects are to be added to the document

  • access: public
mixed $arc4 = '' (line 203)

the ancryption array for the document encryption is stored here

  • access: public
mixed $arc4_objnum = 0 (line 208)

the object Id of the encryption information

  • access: public
mixed $callback = array() (line 228)

array which forms a stack to keep track of nested callback functions

  • access: public
mixed $catalogId (line 39)

the objectId (number within the objects array) of the document catalog

  • access: public
mixed $checkpoint = '' (line 246)

store the stack for the transaction commands, each item in here is a record of the values of all the publiciables within the class, so that the user can rollback at will (from each 'start' command) note that this includes the objects array, so these can be large.

  • access: public
mixed $currentBaseFont = '' (line 55)

the current base font

  • access: public
mixed $currentColour = array('r'=>-1, 'g'=>-1, 'b'=>-1) (line 91)

current colour for fill operations, defaults to inactive value, all three components should be between 0 and 1 inclusive when active

  • access: public
mixed $currentContents (line 75)

object number of the currently active contents block

  • access: public
mixed $currentFillTransparency = array("mode" => "Normal", "opacity" => 1.0) (line 111)

current fill transparency (partial graphics state)

  • access: public
mixed $currentFont = '' (line 50)

a record of the current font

  • access: public
mixed $currentFontNum = 0 (line 60)

the number of the current font within the font array

  • access: public
mixed $currentLineStyle = '' (line 101)

current style that lines are drawn in

  • access: public
mixed $currentLineTransparency = array("mode" => "Normal", "opacity" => 1.0) (line 106)

current line transparency (partial graphics state)

  • access: public
mixed $currentNode (line 65)
  • access: public
mixed $currentPage (line 70)

object number of the current page

  • access: public
mixed $currentStrokeColour = array('r'=>-1, 'g'=>-1, 'b'=>-1) (line 96)

current colour for stroke operations (lines etc.)

  • access: public
mixed $currentTextState = '' (line 193)

track if the current font is bolded or italicised

  • access: public
mixed $destinations = array() (line 239)

store label->id pairs for named destinations, these will be used to replace internal links

done this way so that destinations can be defined after the location that links to them

  • access: public
mixed $encrypted = 0 (line 218)

a flag to say if a document is to be encrypted or not

  • access: public
mixed $encryptionKey = '' (line 223)

the ancryption key for the encryption of all the document content (structure is not encrypted)

  • access: public
mixed $fileIdentifier = '' (line 213)

the file identifier, used to uniquely identify a pdf document

  • access: public
mixed $firstPageId (line 170)

the objectId of the first page of the document

  • access: public
mixed $fontFamilies = array() (line 188)

store the information about the relationship between font families this used so that the code knows which font is the bold version of another font, etc.

the value of this array is initialised in the constuctor function.

  • access: public
mixed $fonts = array() (line 45)

array carrying information about the fonts that the system currently knows about

used to ensure that a font is not loaded twice, among other things

  • access: public
mixed $infoObject = 0 (line 154)

the objectId of the information object for the document this contains authorship, title etc.

  • access: public
mixed $looseObjects = array() (line 143)

an array which contains information about the objects which are not firmly attached to pages

these have been added with the addObject function

  • access: public
mixed $messages = '' (line 198)

messages are stored here during processing, these can be selected afterwards to give some useful debug information

  • access: public
mixed $nCallback = 0 (line 233)

the number of callback functions in the callback array

  • access: public
mixed $nStack = 0 (line 137)

number of elements within the object Id storage stack

  • access: public
mixed $nStateStack = 0 (line 122)

number of elements within the state stack

  • access: public
mixed $numFonts = 0 (line 80)

number of fonts within the system

  • access: public
mixed $numImages = 0 (line 159)

number of images being tracked within the document

  • access: public
mixed $numObj = 0 (line 29)

the current number of pdf objects in the document

  • access: public
mixed $numPages = 0 (line 127)

number of page objects within the document

  • access: public
mixed $objects = array() (line 34)

this array contains all of the pdf objects, ready for final assembly

  • access: public
mixed $options = array('compression'=>1) (line 165)

an array containing options about the document

it defaults to turning on the compression of the objects

  • access: public
mixed $procsetObjectId (line 181)

the object Id of the procset object

  • access: public
mixed $stack = array() (line 132)

object Id storage stack

  • access: public
mixed $stateStack = array() (line 117)

an array which is used to save the state of the document, mainly the colours and styles

it is used to temporarily change to another state, the change back to what it was before

  • access: public
mixed $wordSpaceAdjust = 0 (line 176)

used to track the last used value of the inter-word spacing, this is so that it is known when the spacing is changed.

  • access: public
Methods
Constructor Cpdf (line 253)

class constructor

this will start a new document

  • var: array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
Cpdf Cpdf ([mixed $pageSize = array(0, 0, 612, 792)])
addDestination (line 4793)

create a labelled destination within the document

void addDestination (mixed $label, mixed $style, mixed $a, mixed $b, mixed $c)
addImage (line 4657)

add an image into the document, from a GD object

this function is not all that reliable, and I would probably encourage people to use the file based functions

void addImage (mixed &$img, mixed $x, mixed $y, mixed $w, mixed $h, [mixed $quality = 75])
addInfo (line 4229)

add content to the documents info object

void addInfo (mixed $label, mixed $value)
addInternalLink (line 1966)

add a link in the document to an internal destination (ie. within the document)

void addInternalLink (mixed $label, mixed $x0, mixed $y0, mixed $x1, mixed $y1)
addJpegFromFile (line 4591)

add a JPEG image into the document, from a file

void addJpegFromFile (mixed $img, mixed $x, mixed $y, mixed $w, mixed $h)
addLink (line 1953)

add a link in the document to an external URL

void addLink (mixed $url, mixed $x0, mixed $y0, mixed $x1, mixed $y1)
addMessage (line 4856)

used to add messages for use in debugging

void addMessage (mixed $message)
addObject (line 4119)

after an object has been created, it wil only show if it has been added, using this function.

void addObject (mixed $id, [mixed $options = 'add'])
addPngFromFile (line 4292)

add a PNG image into the document, from a file

this should work with remote files

void addPngFromFile (mixed $file, mixed $x, mixed $y, mixed $w, mixed $h)
addText (line 3537)

add text to the document, at a specified location, size and angle on the page

void addText (mixed $x, mixed $y, mixed $size, mixed $text, mixed $angle, mixed $wordSpaceAdjust)
addTextWrap (line 3794)

add text to the page, but ensure that it fits within a certain width if it does not fit then put in as much as possible, splitting at word boundaries and return the remainder.

justification and angle can also be specified for the text

void addTextWrap (mixed $x, mixed $y, mixed $width, mixed $size, mixed $text, [mixed $justification = 'left'], mixed $angle, mixed $test)
ARC4 (line 1912)

ARC4 encrypt a text string

void ARC4 (mixed $text)
ARC4_init (line 1870)

initialize the ARC4 encryption

void ARC4_init ([mixed $key = ''])
checkAllHere (line 2019)

should be used for internal checks, not implemented as yet

void checkAllHere ()
closeObject (line 4083)

close an object

void closeObject ()
curve (line 2816)

draw a bezier curve based on 4 control points

void curve (mixed $x0, mixed $y0, mixed $x1, mixed $y1, mixed $x2, mixed $y2, mixed $x3, mixed $y3)
ellipse (line 2856)

draw an ellipse note that the part and filled ellipse are just special cases of this function

draws an ellipse in the current line style centered at $x0,$y0, radii $r1,$r2 if $r2 is not set, then a circle is drawn nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a pretty crappy shape at 2, as we are approximating with bezier curves.

void ellipse (mixed $x0, mixed $y0, mixed $r1, mixed $r2, mixed $angle, [mixed $nSeg = 8], mixed $astart, [mixed $afinish = 360], [mixed $close = 1], mixed $fill)
encryptInit (line 1848)

initialize the encryption for processing a particular object

void encryptInit (mixed $id)
filledEllipse (line 2840)

draw a filled ellipse

void filledEllipse (mixed $x0, mixed $y0, mixed $r1, mixed $r2, mixed $angle, [mixed $nSeg = 8], mixed $astart, [mixed $afinish = 360])
filledRectangle (line 3041)

a filled rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not

the coordinates of the upper-right corner

void filledRectangle (mixed $x1, mixed $y1, mixed $width, mixed $height)
getFirstPageId (line 2680)

function for the user to find out what the ID is of the first page that was created during startup - useful if they wish to add something to it later.

void getFirstPageId ()
getFontDecender (line 3208)

return the font decender, this will normally return a negative number

if you add this number to the baseline, you get the level of the bottom of the font it is in the pdf user units

void getFontDecender (mixed $size)
getFontHeight (line 3189)

return the height in units of the current font in the given size

void getFontHeight (mixed $size)
getTextWidth (line 3670)

calculate how wide a given text string will be on a page, at a given size.

this can be called externally, but is alse used by the other class functions

void getTextWidth (mixed $size, mixed $text, mixed $spacing)
line (line 2806)

draw a line from one set of coordinates to another

void line (mixed $x1, mixed $y1, mixed $x2, mixed $y2)
md5_16 (line 1830)

calculate the 16 byte version of the 128 bit md5 digest of the string

void md5_16 (mixed $string)
newPage (line 3061)

add a new page to the document

this also makes the new page the current active object

void newPage (mixed $insert, mixed $id, [mixed $pos = 'after'])
openHere (line 4768)

specify where the document should open when it first starts

void openHere (mixed $style, mixed $a, mixed $b, mixed $c)
openObject (line 4041)

make a loose object, the output will go into this object, until it is closed, then will revert to the current one.

this object will not appear until it is included within a page. the function will return the object number

void openObject ()
output (line 2026)

return the pdf stream as a string returned from the function

void output (mixed $debug)
o_action (line 1177)

an action object, used to link to URLS initially

void o_action (mixed $id, mixed $action, [mixed $options = ''])
o_annotation (line 1247)

an annotation object, this will add an annotation to the current page.

initially will support just link annotations

void o_annotation (mixed $id, mixed $action, [mixed $options = ''])
o_catalog (line 412)

define the document catalog, the overall controller for the document

void o_catalog (mixed $id, mixed $action, [mixed $options = ''])
o_contents (line 1458)

the contents objects hold all of the content which appears on pages

void o_contents (mixed $id, mixed $action, [mixed $options = ''])
o_destination (line 285)

destination object, used to specify the location for the user to jump to, presently on opening

void o_destination (mixed $id, mixed $action, [mixed $options = ''])
o_encryption (line 1715)

encryption object.

void o_encryption (mixed $id, mixed $action, [mixed $options = ''])
o_extGState (line 1668)

graphics state object

void o_extGState (mixed $id, mixed $action, [mixed $options = ""])
o_font (line 744)

an object to hold the font description

void o_font (mixed $id, mixed $action, [mixed $options = ''])
o_fontDescriptor (line 889)

a font descriptor, needed for including additional fonts

void o_fontDescriptor (mixed $id, mixed $action, [mixed $options = ''])
o_fontEncoding (line 981)

the font encoding

void o_fontEncoding (mixed $id, mixed $action, [mixed $options = ''])
o_image (line 1539)

an image object, will be an XObject in the document, includes description and data

void o_image (mixed $id, mixed $action, [mixed $options = ''])
o_info (line 1100)

define the document information

void o_info (mixed $id, mixed $action, [mixed $options = ''])
o_outlines (line 694)

define the outlines in the doc, empty for now

void o_outlines (mixed $id, mixed $action, [mixed $options = ''])
o_page (line 1338)

a page object, it also creates a contents object to hold its contents

void o_page (mixed $id, mixed $action, [mixed $options = ''])
o_pages (line 502)

object which is a parent to the pages in the document

void o_pages (mixed $id, mixed $action, [mixed $options = ''])
o_procset (line 1041)

the document procset, solves some problems with printing to old PS printers

void o_procset (mixed $id, mixed $action, [mixed $options = ''])
o_viewerPreferences (line 347)

set the viewer preferences

void o_viewerPreferences (mixed $id, mixed $action, [mixed $options = ''])
partEllipse (line 2831)

draw a part of an ellipse

void partEllipse (mixed $x0, mixed $y0, mixed $astart, mixed $afinish, mixed $r1, mixed $r2, mixed $angle, [mixed $nSeg = 8])
polygon (line 3016)

draw a polygon, the syntax for this is similar to the GD polygon command

void polygon (mixed $p, mixed $np, mixed $f)
rectangle (line 3051)

draw a rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not

the coordinates of the upper-right corner

void rectangle (mixed $x1, mixed $y1, mixed $width, mixed $height)
reopenObject (line 4064)

open an existing object for editing

void reopenObject (mixed $id)
restoreSerializedObject (line 4212)

restore an object from its stored representation. returns its new object id.

void restoreSerializedObject (mixed $obj)
restoreState (line 4012)

restore a previously saved state

void restoreState (mixed $pageEnd)
saveState (line 3980)

this will be called at a new page to return the state to what it was on the

end of the previous page, before the stack was closed down This is to get around not being able to have open 'q' across pages

void saveState (mixed $pageEnd)
selectFont (line 2340)

if the font is not loaded then load it and make the required object

else just make it the current font the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding' note that encoding='none' will need to be used for symbolic fonts and 'differences' => an array of mappings between numbers 0->255 and character names.

void selectFont (mixed $fontName, [mixed $encoding = ''], [mixed $set = 1])
serializeObject (line 4199)

return a storable representation of a specific object

void serializeObject (mixed $id)
setColor (line 2700)

sets the colour for fill operations

void setColor (mixed $r, mixed $g, mixed $b, mixed $force)
setEncryption (line 1981)

set the encryption of the document can be used to turn it on and/or set the passwords which it will have.

also the functions that the user will have are set here, such as print, modify, add

void setEncryption ([mixed $userPass = ''], [mixed $ownerPass = ''], [mixed $pc = array()])
setFillTransparency (line 2784)

Set current blend mode & opacity for filled objects.

Valid blend modes are:

Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDogde, ColorBurn, HardLight, SoftLight, Difference, Exclusion

void setFillTransparency (string $mode, float $opacity)
  • string $mode: the blend mode to use
  • float $opacity: 0.0 fully transparent, 1.0 fully opaque
setFontFamily (line 4814)

define font families, this is used to initialize the font families for the default fonts and for the user to add new ones for their fonts. The default bahavious can be overridden should that be desired.

void setFontFamily (mixed $family, [mixed $options = ''])
setGraphicsState (line 2728)

Set the graphics state for compositions

void setGraphicsState (mixed $parameters)
setLineStyle (line 2969)

this sets the line drawing style.

width, is the thickness of the line in user units cap is the type of cap to put on the line, values can be 'butt','round','square' where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the end of the line. join can be 'miter', 'round', 'bevel' dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the on and off dashes. (2) represents 2 on, 2 off, 2 on , 2 off ... (2,1) is 2 on, 1 off, 2 on, 1 off.. etc phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.

void setLineStyle ([mixed $width = 1], [mixed $cap = ''], [mixed $join = ''], [mixed $dash = ''], mixed $phase)
setLineTransparency (line 2752)

Set current blend mode & opacity for lines.

Valid blend modes are:

Normal, Multiply, Screen, Overlay, Darken, Lighten, ColorDogde, ColorBurn, HardLight, SoftLight, Difference, Exclusion

void setLineTransparency (string $mode, float $opacity)
  • string $mode: the blend mode to use
  • float $opacity: 0.0 fully transparent, 1.0 fully opaque
setPreferences (line 4251)

set the viewer preferences of the document, it is up to the browser to obey these.

void setPreferences (mixed $label, mixed $value)
setStrokeColor (line 2714)

sets the colour for stroke operations

void setStrokeColor (mixed $r, mixed $g, mixed $b, mixed $force)
stopObject (line 4105)

stop an object from appearing on pages from this point on

void stopObject (mixed $id)
stream (line 3129)

output the pdf code, streaming it to the browser

the relevant headers are set so that hopefully the browser will recognise it

void stream ([mixed $options = ''])
transaction (line 4865)

a few functions which should allow the document to be treated transactionally.

void transaction (mixed $action)

Documentation generated on Sat, 19 Nov 2005 12:36:31 -0700 by phpDocumentor 1.3.0RC3