phpdftk API Documentation

ContentStream extends PdfStream
in package

Content stream for page graphics and text.

Provides a fluent API covering all PDF content stream operators as defined in the PDF 1.7 specification (ISO 32000-1).

Table of Contents

Properties

$data  : string
$dictionary  : PdfDictionary
$generationNumber  : int
$objectNumber  : int

Methods

__construct()  : mixed
beginCompatibility()  : self
BX - Begin a compatibility section (unknown operators are ignored)
beginMarkedContent()  : self
BMC - Begin a marked-content sequence
beginMarkedContentWithProperties()  : self
BDC - Begin a marked-content sequence with property list.
beginText()  : self
BT - Begin text object
clearOperators()  : void
Clear the operators array. After this, toPdf() will use $this->data directly instead of regenerating from operators.
clip()  : self
W - Modify clipping path (non-zero winding)
clipEvenOdd()  : self
W* - Modify clipping path (even-odd rule)
closeAndStroke()  : self
s - Close and stroke
closeFillAndStroke()  : self
b - Close, fill, and stroke (non-zero winding)
closeFillAndStrokeEvenOdd()  : self
b* - Close, fill, and stroke (even-odd rule)
closePath()  : self
h - Close path
concatMatrix()  : self
cm - Concatenate matrix to current transformation matrix
concatMatrixObject()  : self
cm - Concatenate matrix using a Geometry Matrix value object
curveTo()  : self
c - Curve to (full cubic Bezier)
curveToV()  : self
v - Curve to (current point replicated as first control point)
curveToY()  : self
y - Curve to (final point replicated as second control point)
doXObject()  : self
Do - Invoke named XObject
endCompatibility()  : self
EX - End a compatibility section
endMarkedContent()  : self
EMC - End a marked-content sequence
endPath()  : self
n - End path without painting
endText()  : self
ET - End text object
fill()  : self
f - Fill path (non-zero winding)
fillAndStroke()  : self
B - Fill and stroke (non-zero winding)
fillAndStrokeEvenOdd()  : self
B* - Fill and stroke (even-odd rule)
fillEvenOdd()  : self
f* - Fill path (even-odd rule)
getOperators()  : array<int, string>
inlineImage()  : self
BI...ID...EI - Inline image.
lineTo()  : self
l - Line to
markedContentPoint()  : self
MP - Define a marked-content point
markedContentPointWithProperties()  : self
DP - Define a marked-content point with property list.
moveTextPosition()  : self
Td - Move text position
moveTextPositionNewLine()  : self
TD - Move text position and set leading
moveTo()  : self
m - Move to
moveToNextLineAndShowText()  : self
' - Move to next line and show text (equivalent to T* string Tj)
nextLine()  : self
T* - Move to next line
paintShading()  : self
sh - Paint the shape and colour defined by a shading pattern
raw()  : self
Emit a raw PDF operator string verbatim.
rectangle()  : self
re - Rectangle
rectangleObject()  : self
re - Rectangle using a Geometry Rectangle value object
restoreGraphicsState()  : self
Q - Restore graphics state
saveGraphicsState()  : self
q - Save graphics state
setCharSpacing()  : self
Tc - Set character spacing
setDashPattern()  : self
d - Set line dash pattern
setFillCmykColor()  : self
Set fill color using a CmykColor value object
setFillColor()  : self
scn - Set fill color (for special color spaces)
setFillColorCMYK()  : self
k - Set fill color (DeviceCMYK)
setFillColorGray()  : self
g - Set fill color (DeviceGray)
setFillColorRGB()  : self
rg - Set fill color (DeviceRGB)
setFillColorSpace()  : self
cs - Set fill color space
setFillGrayColor()  : self
Set fill color using a GrayColor value object
setFillRgbColor()  : self
Set fill color using an RgbColor value object
setFilter()  : void
Set a filter to encode/decode the stream data.
setFlatness()  : self
i - Set flatness tolerance
setFont()  : self
Tf - Set font and size.
setGlyphWidth()  : self
d0 - Set glyph width in a Type 3 font (coloured glyph)
setGlyphWidthAndBoundingBox()  : self
d1 - Set glyph width and bounding box in a Type 3 font (uncoloured glyph)
setGraphicsState()  : self
gs - Set graphics state from ExtGState resource
setHorizontalScaling()  : self
Tz - Set horizontal scaling
setLineCap()  : self
J - Set line cap style
setLineJoin()  : self
j - Set line join style
setLineWidth()  : self
w - Set line width
setMiterLimit()  : self
M - Set miter limit
setRenderingIntent()  : self
ri - Set rendering intent
setSpacingMoveAndShowText()  : self
" - Set word/char spacing, move to next line, and show text (equivalent to aw Tw ac Tc T* string Tj)
setStrokeCmykColor()  : self
Set stroke color using a CmykColor value object
setStrokeColor()  : self
SCN - Set stroke color (for special color spaces)
setStrokeColorCMYK()  : self
K - Set stroke color (DeviceCMYK)
setStrokeColorGray()  : self
G - Set stroke color (DeviceGray)
setStrokeColorRGB()  : self
RG - Set stroke color (DeviceRGB)
setStrokeColorSpace()  : self
CS - Set stroke color space
setStrokeGrayColor()  : self
Set stroke color using a GrayColor value object
setStrokeRgbColor()  : self
Set stroke color using an RgbColor value object
setTextLeading()  : self
TL - Set text leading
setTextMatrix()  : self
Tm - Set text matrix and text line matrix
setTextRenderingMode()  : self
Tr - Set text rendering mode
setTextRise()  : self
Ts - Set text rise
setWordSpacing()  : self
Tw - Set word spacing
showText()  : self
Tj - Show text
showTextArray()  : self
TJ - Show text with individual character positioning
showTextArrayHex()  : self
TJ with a hex/kern array — emits `[ <hex> kern <hex> kern ... ] TJ` for a CID font where shaped glyphs need per-glyph kern adjustments (positive kern moves the text position backward in 1/1000 em units, negative kern moves forward). Each `$items` entry is either a hex GID string (without `< >` delimiters) or a numeric kern in 1/1000-em units.
showTextHex()  : self
Tj with hex-encoded string - Show text using hex-encoded glyph IDs (for CID fonts)
showUnicodeText()  : self
Show Unicode text using a CID font's GID mapping.
showUnicodeTextKerned()  : self
Emit a TJ array with kerning adjustments from GPOS data.
showUnicodeTextShaped()  : self
Apply OpenType GSUB ligature substitutions before kerning.
stroke()  : self
S - Stroke path
toIndirectObject()  : string
Wrap the object in an indirect object structure: X Y obj ... endobj
toPdf()  : string
Returns the stream body: dictionary, stream keyword, data, endstream.

Properties

$generationNumber

public int $generationNumber = 0

Methods

beginCompatibility()

BX - Begin a compatibility section (unknown operators are ignored)

public beginCompatibility() : self
Return values
self

beginMarkedContent()

BMC - Begin a marked-content sequence

public beginMarkedContent(string $tag) : self
Parameters
$tag : string
Return values
self

beginMarkedContentWithProperties()

BDC - Begin a marked-content sequence with property list.

public beginMarkedContentWithProperties(string $tag, string $properties) : self

$properties is either a resource name (e.g. '/MC0') or an inline dict string.

Parameters
$tag : string
$properties : string
Return values
self

beginText()

BT - Begin text object

public beginText() : self
Return values
self

clearOperators()

Clear the operators array. After this, toPdf() will use $this->data directly instead of regenerating from operators.

public clearOperators() : void

clip()

W - Modify clipping path (non-zero winding)

public clip() : self
Return values
self

clipEvenOdd()

W* - Modify clipping path (even-odd rule)

public clipEvenOdd() : self
Return values
self

closeAndStroke()

s - Close and stroke

public closeAndStroke() : self
Return values
self

closeFillAndStroke()

b - Close, fill, and stroke (non-zero winding)

public closeFillAndStroke() : self
Return values
self

closeFillAndStrokeEvenOdd()

b* - Close, fill, and stroke (even-odd rule)

public closeFillAndStrokeEvenOdd() : self
Return values
self

closePath()

h - Close path

public closePath() : self
Return values
self

concatMatrix()

cm - Concatenate matrix to current transformation matrix

public concatMatrix(float $a, float $b, float $c, float $d, float $e, float $f) : self
Parameters
$a : float
$b : float
$c : float
$d : float
$e : float
$f : float
Return values
self

concatMatrixObject()

cm - Concatenate matrix using a Geometry Matrix value object

public concatMatrixObject(Matrix $m) : self
Parameters
$m : Matrix
Return values
self

curveTo()

c - Curve to (full cubic Bezier)

public curveTo(float $x1, float $y1, float $x2, float $y2, float $x3, float $y3) : self
Parameters
$x1 : float
$y1 : float
$x2 : float
$y2 : float
$x3 : float
$y3 : float
Return values
self

curveToV()

v - Curve to (current point replicated as first control point)

public curveToV(float $x2, float $y2, float $x3, float $y3) : self
Parameters
$x2 : float
$y2 : float
$x3 : float
$y3 : float
Return values
self

curveToY()

y - Curve to (final point replicated as second control point)

public curveToY(float $x1, float $y1, float $x3, float $y3) : self
Parameters
$x1 : float
$y1 : float
$x3 : float
$y3 : float
Return values
self

doXObject()

Do - Invoke named XObject

public doXObject(string $name) : self
Parameters
$name : string
Return values
self

endCompatibility()

EX - End a compatibility section

public endCompatibility() : self
Return values
self

endMarkedContent()

EMC - End a marked-content sequence

public endMarkedContent() : self
Return values
self

endPath()

n - End path without painting

public endPath() : self
Return values
self

endText()

ET - End text object

public endText() : self
Return values
self

fill()

f - Fill path (non-zero winding)

public fill() : self
Return values
self

fillAndStroke()

B - Fill and stroke (non-zero winding)

public fillAndStroke() : self
Return values
self

fillAndStrokeEvenOdd()

B* - Fill and stroke (even-odd rule)

public fillAndStrokeEvenOdd() : self
Return values
self

fillEvenOdd()

f* - Fill path (even-odd rule)

public fillEvenOdd() : self
Return values
self

getOperators()

public getOperators() : array<int, string>
Return values
array<int, string>

inlineImage()

BI...ID...EI - Inline image.

public inlineImage(array<string, string> $params, string $data) : self
Parameters
$params : array<string, string>

Associative array of image dictionary entries (abbreviated keys).

$data : string

Raw image data.

Return values
self

lineTo()

l - Line to

public lineTo(float $x, float $y) : self
Parameters
$x : float
$y : float
Return values
self

markedContentPoint()

MP - Define a marked-content point

public markedContentPoint(string $tag) : self
Parameters
$tag : string
Return values
self

markedContentPointWithProperties()

DP - Define a marked-content point with property list.

public markedContentPointWithProperties(string $tag, string $properties) : self

$properties is either a resource name (string) or an inline dict string.

Parameters
$tag : string
$properties : string
Return values
self

moveTextPosition()

Td - Move text position

public moveTextPosition(float $x, float $y) : self
Parameters
$x : float
$y : float
Return values
self

moveTextPositionNewLine()

TD - Move text position and set leading

public moveTextPositionNewLine(float $tx, float $ty) : self
Parameters
$tx : float
$ty : float
Return values
self

moveTo()

m - Move to

public moveTo(float $x, float $y) : self
Parameters
$x : float
$y : float
Return values
self

moveToNextLineAndShowText()

' - Move to next line and show text (equivalent to T* string Tj)

public moveToNextLineAndShowText(string $text) : self
Parameters
$text : string
Return values
self

nextLine()

T* - Move to next line

public nextLine() : self
Return values
self

paintShading()

sh - Paint the shape and colour defined by a shading pattern

public paintShading(string $name) : self
Parameters
$name : string
Return values
self

raw()

Emit a raw PDF operator string verbatim.

public raw(string $operator) : self
Parameters
$operator : string
Return values
self

rectangle()

re - Rectangle

public rectangle(float $x, float $y, float $w, float $h) : self
Parameters
$x : float
$y : float
$w : float
$h : float
Return values
self

rectangleObject()

re - Rectangle using a Geometry Rectangle value object

public rectangleObject(Rectangle $r) : self
Parameters
$r : Rectangle
Return values
self

restoreGraphicsState()

Q - Restore graphics state

public restoreGraphicsState() : self
Return values
self

saveGraphicsState()

q - Save graphics state

public saveGraphicsState() : self
Return values
self

setCharSpacing()

Tc - Set character spacing

public setCharSpacing(float $cs) : self
Parameters
$cs : float
Return values
self

setDashPattern()

d - Set line dash pattern

public setDashPattern(array<int, int|float> $dash, int $phase) : self
Parameters
$dash : array<int, int|float>
$phase : int
Return values
self

setFillCmykColor()

Set fill color using a CmykColor value object

public setFillCmykColor(CmykColor $color) : self
Parameters
$color : CmykColor
Return values
self

setFillColor()

scn - Set fill color (for special color spaces)

public setFillColor(float|int|string ...$components) : self
Parameters
$components : float|int|string
Return values
self

setFillColorCMYK()

k - Set fill color (DeviceCMYK)

public setFillColorCMYK(float $c, float $m, float $y, float $k) : self
Parameters
$c : float
$m : float
$y : float
$k : float
Return values
self

setFillColorGray()

g - Set fill color (DeviceGray)

public setFillColorGray(float $g) : self
Parameters
$g : float
Return values
self

setFillColorRGB()

rg - Set fill color (DeviceRGB)

public setFillColorRGB(float $r, float $g, float $b) : self
Parameters
$r : float
$g : float
$b : float
Return values
self

setFillColorSpace()

cs - Set fill color space

public setFillColorSpace(string $name) : self
Parameters
$name : string
Return values
self

setFillGrayColor()

Set fill color using a GrayColor value object

public setFillGrayColor(GrayColor $color) : self
Parameters
$color : GrayColor
Return values
self

setFillRgbColor()

Set fill color using an RgbColor value object

public setFillRgbColor(RgbColor $color) : self
Parameters
$color : RgbColor
Return values
self

setFilter()

Set a filter to encode/decode the stream data.

public setFilter(FilterInterface $filter, string $pdfFilterName) : void
Parameters
$filter : FilterInterface

The filter implementation

$pdfFilterName : string

The PDF filter name (e.g. 'FlateDecode', 'ASCII85Decode')

setFlatness()

i - Set flatness tolerance

public setFlatness(float $flatness) : self
Parameters
$flatness : float
Return values
self

setFont()

Tf - Set font and size.

public setFont(RegisteredFont|string $name, float $size) : self

Accepts either a resource name string (legacy / raw-bytes mode — the caller is responsible for emitting bytes in the font's encoding) or a RegisteredFont handle from PdfWriter::addFont(). When a handle is passed, the stream remembers the font's text encoder so subsequent showText/showTextArray calls accept UTF-8 directly.

Parameters
$name : RegisteredFont|string
$size : float
Return values
self

setGlyphWidth()

d0 - Set glyph width in a Type 3 font (coloured glyph)

public setGlyphWidth(float $wx, float $wy) : self
Parameters
$wx : float
$wy : float
Return values
self

setGlyphWidthAndBoundingBox()

d1 - Set glyph width and bounding box in a Type 3 font (uncoloured glyph)

public setGlyphWidthAndBoundingBox(float $wx, float $wy, float $llx, float $lly, float $urx, float $ury) : self
Parameters
$wx : float
$wy : float
$llx : float
$lly : float
$urx : float
$ury : float
Return values
self

setGraphicsState()

gs - Set graphics state from ExtGState resource

public setGraphicsState(string $name) : self
Parameters
$name : string
Return values
self

setHorizontalScaling()

Tz - Set horizontal scaling

public setHorizontalScaling(float $hs) : self
Parameters
$hs : float
Return values
self

setLineCap()

J - Set line cap style

public setLineCap(int $cap) : self
Parameters
$cap : int
Return values
self

setLineJoin()

j - Set line join style

public setLineJoin(int $join) : self
Parameters
$join : int
Return values
self

setLineWidth()

w - Set line width

public setLineWidth(float $w) : self
Parameters
$w : float
Return values
self

setMiterLimit()

M - Set miter limit

public setMiterLimit(float $limit) : self
Parameters
$limit : float
Return values
self

setRenderingIntent()

ri - Set rendering intent

public setRenderingIntent(string $intent) : self
Parameters
$intent : string
Return values
self

setSpacingMoveAndShowText()

" - Set word/char spacing, move to next line, and show text (equivalent to aw Tw ac Tc T* string Tj)

public setSpacingMoveAndShowText(float $aw, float $ac, string $text) : self
Parameters
$aw : float
$ac : float
$text : string
Return values
self

setStrokeCmykColor()

Set stroke color using a CmykColor value object

public setStrokeCmykColor(CmykColor $color) : self
Parameters
$color : CmykColor
Return values
self

setStrokeColor()

SCN - Set stroke color (for special color spaces)

public setStrokeColor(float|int|string ...$components) : self
Parameters
$components : float|int|string
Return values
self

setStrokeColorCMYK()

K - Set stroke color (DeviceCMYK)

public setStrokeColorCMYK(float $c, float $m, float $y, float $k) : self
Parameters
$c : float
$m : float
$y : float
$k : float
Return values
self

setStrokeColorGray()

G - Set stroke color (DeviceGray)

public setStrokeColorGray(float $g) : self
Parameters
$g : float
Return values
self

setStrokeColorRGB()

RG - Set stroke color (DeviceRGB)

public setStrokeColorRGB(float $r, float $g, float $b) : self
Parameters
$r : float
$g : float
$b : float
Return values
self

setStrokeColorSpace()

CS - Set stroke color space

public setStrokeColorSpace(string $name) : self
Parameters
$name : string
Return values
self

setStrokeGrayColor()

Set stroke color using a GrayColor value object

public setStrokeGrayColor(GrayColor $color) : self
Parameters
$color : GrayColor
Return values
self

setStrokeRgbColor()

Set stroke color using an RgbColor value object

public setStrokeRgbColor(RgbColor $color) : self
Parameters
$color : RgbColor
Return values
self

setTextLeading()

TL - Set text leading

public setTextLeading(float $tl) : self
Parameters
$tl : float
Return values
self

setTextMatrix()

Tm - Set text matrix and text line matrix

public setTextMatrix(float $a, float $b, float $c, float $d, float $e, float $f) : self
Parameters
$a : float
$b : float
$c : float
$d : float
$e : float
$f : float
Return values
self

setTextRenderingMode()

Tr - Set text rendering mode

public setTextRenderingMode(int $mode) : self
Parameters
$mode : int
Return values
self

setTextRise()

Ts - Set text rise

public setTextRise(float $rise) : self
Parameters
$rise : float
Return values
self

setWordSpacing()

Tw - Set word spacing

public setWordSpacing(float $ws) : self
Parameters
$ws : float
Return values
self

showText()

Tj - Show text

public showText(string $text) : self
Parameters
$text : string
Return values
self

showTextArray()

TJ - Show text with individual character positioning

public showTextArray(array<int, string|int|float> $texts) : self
Parameters
$texts : array<int, string|int|float>
Return values
self

showTextArrayHex()

TJ with a hex/kern array — emits `[ <hex> kern <hex> kern ... ] TJ` for a CID font where shaped glyphs need per-glyph kern adjustments (positive kern moves the text position backward in 1/1000 em units, negative kern moves forward). Each `$items` entry is either a hex GID string (without `< >` delimiters) or a numeric kern in 1/1000-em units.

public showTextArrayHex(array<int, string|int|float> $items) : self
Parameters
$items : array<int, string|int|float>
Return values
self

showTextHex()

Tj with hex-encoded string - Show text using hex-encoded glyph IDs (for CID fonts)

public showTextHex(string $hexEncodedGids) : self
Parameters
$hexEncodedGids : string
Return values
self

showUnicodeText()

Show Unicode text using a CID font's GID mapping.

public showUnicodeText(string $text, array<int, int> $unicodeToGid) : self

Converts UTF-8 text to hex-encoded 2-byte GID sequences and emits a Tj operator. Requires a unicode-to-GID map (typically from TrueTypeData::$fullUnicodeToGid or OpenTypeData::$fullUnicodeToGid).

Parameters
$text : string

UTF-8 text

$unicodeToGid : array<int, int>

Unicode codepoint → GID mapping

Return values
self

showUnicodeTextKerned()

Emit a TJ array with kerning adjustments from GPOS data.

public showUnicodeTextKerned(string $text, array<int, int> $unicodeToGid, array<int, array<int, int>> $kernPairs, int $unitsPerEm) : self

Positive kern values in font units are negated per PDF convention (negative TJ displacements move the cursor right). Falls back to plain Tj when no kerning pairs apply.

Parameters
$text : string

UTF-8 text

$unicodeToGid : array<int, int>

Unicode codepoint => GID mapping

$kernPairs : array<int, array<int, int>>

leftGid => [rightGid => xAdvanceAdjust (design units)]

$unitsPerEm : int

Font design units per em

Return values
self

showUnicodeTextShaped()

Apply OpenType GSUB ligature substitutions before kerning.

public showUnicodeTextShaped(string $text, array<int, int> $unicodeToGid, array<int, array<int, array{components: int[], ligature: int}>> $ligatures[, array<int, array<int, int>> $kernPairs = [] ][, int $unitsPerEm = 1000 ]) : self

Ligatures reduce the glyph count (e.g., "fi" -> single glyph), so the GID sequence may be shorter than the input. If kern pairs are provided, kerning adjustments are applied between the shaped glyphs using TJ; otherwise a plain Tj is emitted.

Parameters
$text : string

UTF-8 text to render

$unicodeToGid : array<int, int>

Unicode codepoint → GID map

$ligatures : array<int, array<int, array{components: int[], ligature: int}>>

GSUB ligature rules

$kernPairs : array<int, array<int, int>> = []

Kern pairs (optional)

$unitsPerEm : int = 1000

Font design units per em

Return values
self

stroke()

S - Stroke path

public stroke() : self
Return values
self

toIndirectObject()

Wrap the object in an indirect object structure: X Y obj ... endobj

public toIndirectObject() : string
Return values
string

toPdf()

Returns the stream body: dictionary, stream keyword, data, endstream.

public toPdf() : string

/Length is injected into the dictionary at serialization time. If a filter is set, the data is encoded before writing.

Return values
string

        
On this page

Search results