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
$data
public
string
$data
= ''
$dictionary
public
PdfDictionary
$dictionary
$generationNumber
public
int
$generationNumber
= 0
$objectNumber
public
int
$objectNumber
= 0
Methods
__construct()
public
__construct() : mixed
beginCompatibility()
BX - Begin a compatibility section (unknown operators are ignored)
public
beginCompatibility() : self
Return values
selfbeginMarkedContent()
BMC - Begin a marked-content sequence
public
beginMarkedContent(string $tag) : self
Parameters
- $tag : string
Return values
selfbeginMarkedContentWithProperties()
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
selfbeginText()
BT - Begin text object
public
beginText() : self
Return values
selfclearOperators()
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
selfclipEvenOdd()
W* - Modify clipping path (even-odd rule)
public
clipEvenOdd() : self
Return values
selfcloseAndStroke()
s - Close and stroke
public
closeAndStroke() : self
Return values
selfcloseFillAndStroke()
b - Close, fill, and stroke (non-zero winding)
public
closeFillAndStroke() : self
Return values
selfcloseFillAndStrokeEvenOdd()
b* - Close, fill, and stroke (even-odd rule)
public
closeFillAndStrokeEvenOdd() : self
Return values
selfclosePath()
h - Close path
public
closePath() : self
Return values
selfconcatMatrix()
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
selfconcatMatrixObject()
cm - Concatenate matrix using a Geometry Matrix value object
public
concatMatrixObject(Matrix $m) : self
Parameters
- $m : Matrix
Return values
selfcurveTo()
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
selfcurveToV()
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
selfcurveToY()
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
selfdoXObject()
Do - Invoke named XObject
public
doXObject(string $name) : self
Parameters
- $name : string
Return values
selfendCompatibility()
EX - End a compatibility section
public
endCompatibility() : self
Return values
selfendMarkedContent()
EMC - End a marked-content sequence
public
endMarkedContent() : self
Return values
selfendPath()
n - End path without painting
public
endPath() : self
Return values
selfendText()
ET - End text object
public
endText() : self
Return values
selffill()
f - Fill path (non-zero winding)
public
fill() : self
Return values
selffillAndStroke()
B - Fill and stroke (non-zero winding)
public
fillAndStroke() : self
Return values
selffillAndStrokeEvenOdd()
B* - Fill and stroke (even-odd rule)
public
fillAndStrokeEvenOdd() : self
Return values
selffillEvenOdd()
f* - Fill path (even-odd rule)
public
fillEvenOdd() : self
Return values
selfgetOperators()
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
selflineTo()
l - Line to
public
lineTo(float $x, float $y) : self
Parameters
- $x : float
- $y : float
Return values
selfmarkedContentPoint()
MP - Define a marked-content point
public
markedContentPoint(string $tag) : self
Parameters
- $tag : string
Return values
selfmarkedContentPointWithProperties()
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
selfmoveTextPosition()
Td - Move text position
public
moveTextPosition(float $x, float $y) : self
Parameters
- $x : float
- $y : float
Return values
selfmoveTextPositionNewLine()
TD - Move text position and set leading
public
moveTextPositionNewLine(float $tx, float $ty) : self
Parameters
- $tx : float
- $ty : float
Return values
selfmoveTo()
m - Move to
public
moveTo(float $x, float $y) : self
Parameters
- $x : float
- $y : float
Return values
selfmoveToNextLineAndShowText()
' - Move to next line and show text (equivalent to T* string Tj)
public
moveToNextLineAndShowText(string $text) : self
Parameters
- $text : string
Return values
selfnextLine()
T* - Move to next line
public
nextLine() : self
Return values
selfpaintShading()
sh - Paint the shape and colour defined by a shading pattern
public
paintShading(string $name) : self
Parameters
- $name : string
Return values
selfraw()
Emit a raw PDF operator string verbatim.
public
raw(string $operator) : self
Parameters
- $operator : string
Return values
selfrectangle()
re - Rectangle
public
rectangle(float $x, float $y, float $w, float $h) : self
Parameters
- $x : float
- $y : float
- $w : float
- $h : float
Return values
selfrectangleObject()
re - Rectangle using a Geometry Rectangle value object
public
rectangleObject(Rectangle $r) : self
Parameters
- $r : Rectangle
Return values
selfrestoreGraphicsState()
Q - Restore graphics state
public
restoreGraphicsState() : self
Return values
selfsaveGraphicsState()
q - Save graphics state
public
saveGraphicsState() : self
Return values
selfsetCharSpacing()
Tc - Set character spacing
public
setCharSpacing(float $cs) : self
Parameters
- $cs : float
Return values
selfsetDashPattern()
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
selfsetFillCmykColor()
Set fill color using a CmykColor value object
public
setFillCmykColor(CmykColor $color) : self
Parameters
- $color : CmykColor
Return values
selfsetFillColor()
scn - Set fill color (for special color spaces)
public
setFillColor(float|int|string ...$components) : self
Parameters
- $components : float|int|string
Return values
selfsetFillColorCMYK()
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
selfsetFillColorGray()
g - Set fill color (DeviceGray)
public
setFillColorGray(float $g) : self
Parameters
- $g : float
Return values
selfsetFillColorRGB()
rg - Set fill color (DeviceRGB)
public
setFillColorRGB(float $r, float $g, float $b) : self
Parameters
- $r : float
- $g : float
- $b : float
Return values
selfsetFillColorSpace()
cs - Set fill color space
public
setFillColorSpace(string $name) : self
Parameters
- $name : string
Return values
selfsetFillGrayColor()
Set fill color using a GrayColor value object
public
setFillGrayColor(GrayColor $color) : self
Parameters
- $color : GrayColor
Return values
selfsetFillRgbColor()
Set fill color using an RgbColor value object
public
setFillRgbColor(RgbColor $color) : self
Parameters
- $color : RgbColor
Return values
selfsetFilter()
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
selfsetFont()
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
selfsetGlyphWidth()
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
selfsetGlyphWidthAndBoundingBox()
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
selfsetGraphicsState()
gs - Set graphics state from ExtGState resource
public
setGraphicsState(string $name) : self
Parameters
- $name : string
Return values
selfsetHorizontalScaling()
Tz - Set horizontal scaling
public
setHorizontalScaling(float $hs) : self
Parameters
- $hs : float
Return values
selfsetLineCap()
J - Set line cap style
public
setLineCap(int $cap) : self
Parameters
- $cap : int
Return values
selfsetLineJoin()
j - Set line join style
public
setLineJoin(int $join) : self
Parameters
- $join : int
Return values
selfsetLineWidth()
w - Set line width
public
setLineWidth(float $w) : self
Parameters
- $w : float
Return values
selfsetMiterLimit()
M - Set miter limit
public
setMiterLimit(float $limit) : self
Parameters
- $limit : float
Return values
selfsetRenderingIntent()
ri - Set rendering intent
public
setRenderingIntent(string $intent) : self
Parameters
- $intent : string
Return values
selfsetSpacingMoveAndShowText()
" - 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
selfsetStrokeCmykColor()
Set stroke color using a CmykColor value object
public
setStrokeCmykColor(CmykColor $color) : self
Parameters
- $color : CmykColor
Return values
selfsetStrokeColor()
SCN - Set stroke color (for special color spaces)
public
setStrokeColor(float|int|string ...$components) : self
Parameters
- $components : float|int|string
Return values
selfsetStrokeColorCMYK()
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
selfsetStrokeColorGray()
G - Set stroke color (DeviceGray)
public
setStrokeColorGray(float $g) : self
Parameters
- $g : float
Return values
selfsetStrokeColorRGB()
RG - Set stroke color (DeviceRGB)
public
setStrokeColorRGB(float $r, float $g, float $b) : self
Parameters
- $r : float
- $g : float
- $b : float
Return values
selfsetStrokeColorSpace()
CS - Set stroke color space
public
setStrokeColorSpace(string $name) : self
Parameters
- $name : string
Return values
selfsetStrokeGrayColor()
Set stroke color using a GrayColor value object
public
setStrokeGrayColor(GrayColor $color) : self
Parameters
- $color : GrayColor
Return values
selfsetStrokeRgbColor()
Set stroke color using an RgbColor value object
public
setStrokeRgbColor(RgbColor $color) : self
Parameters
- $color : RgbColor
Return values
selfsetTextLeading()
TL - Set text leading
public
setTextLeading(float $tl) : self
Parameters
- $tl : float
Return values
selfsetTextMatrix()
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
selfsetTextRenderingMode()
Tr - Set text rendering mode
public
setTextRenderingMode(int $mode) : self
Parameters
- $mode : int
Return values
selfsetTextRise()
Ts - Set text rise
public
setTextRise(float $rise) : self
Parameters
- $rise : float
Return values
selfsetWordSpacing()
Tw - Set word spacing
public
setWordSpacing(float $ws) : self
Parameters
- $ws : float
Return values
selfshowText()
Tj - Show text
public
showText(string $text) : self
Parameters
- $text : string
Return values
selfshowTextArray()
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
selfshowTextArrayHex()
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
selfshowTextHex()
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
selfshowUnicodeText()
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
selfshowUnicodeTextKerned()
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
selfshowUnicodeTextShaped()
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
selfstroke()
S - Stroke path
public
stroke() : self
Return values
selftoIndirectObject()
Wrap the object in an indirect object structure: X Y obj ... endobj
public
toIndirectObject() : string
Return values
stringtoPdf()
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.