The Main Class
Table of Contents
Introduction
As any other SetaPDF component the Stamper is represented by a main class: The SetaPDF_Stamper class.
It is the midsection of the stamp workflow. It collects the stamp instances and will process them as they are configured by its stamp()
or stampPageNo()
methods.
Get an Instance
A class instance can be simply created by passing a document instance, that will get stamped, to its constructor.
Reading and writing the PDF document is up to the Core component. This way it is possible to work on the document instance further.
Adding Stamp Objects
A stamp object could be seen as a kind of content, that should get added onto a specific or several page. It doesn't "know" about its position or rotation on a page but all this can be defined while adding it to the stamper instance.
A stamp object can be added several times to a stamper instance. All this is done with the addStamp()
method:
Description
Adds a stamp object to the stamper instance.
Parameters
- $stamp : SetaPDF_Stamper_Stamp
Stamp object which shall be stamped on the document
- $positionOrConfig : string|array
Position or array of configuration variables
- $showOnPage : int|string|array|callback
The configuration defining on which pages the stamp shall be shown. Possible values are:
- PAGES_XXX constant
- Integer with the valid page number
- String with the valid page number or the valid range (e.g. '10-12')
- Array with all valid page numbers
- Callback with the arguments (int $pageNumber, int $pageCount)
- $translateX : int
Move the stamp on x-axis by $translateX
- $translateY : int
Move the stamp on y-axis by $translateX
- $rotation : float
Rotate the stamp by $rotation degrees
- $underlay : bool
Defines whether the stamp should be place before or after the existing content
- $callback : null|callback
Callback which will be called every time before the document will be stamped by this stamp if it's not returning true the stamp will not stamped on this run.
The addStamp()
method can be called in 2 ways: By its parameter or with a config array which keys are named as the method parameters:
$stamper->addStamp( $stamp, SetaPDF_Stamper::POSITION_RIGHT_TOP, SetaPDF_Stamper::PAGES_LAST, -10, -10 ); // or $stamper->addStamp( $stamp, array( 'position' => SetaPDF_Stamper::POSITION_RIGHT_TOP, 'showOnPage' => SetaPDF_Stamper::PAGES_LAST, 'translateX' => -10, 'translateY' => -10 ) );
Positioning
The exact position of a stamp is a combination of the $position
and $translateX/Y
parameters.
The $position
parameter shall be a value of a predefined constant (see SetaPDF_Stamper::POSITION_XXX) that define the "starting position". The stamp can be positioned further by the $translateX
and $translateY
parameters. The origin of the coordinate system is the lower left corner of a page.
The constants are self-explanatory by their names but they can also be checked with the checkPositionParameter()
method. It will return true on success or throw an InvalidArgumentException
instead.
Position constant
Position constant
Position constant
Position constant
Position constant
Position constant
Position constant
Position constant
Position constant
A simple example:
Show on Page
The $showOnPage
parameter accepts various values defining the page or pages to show the stamp on. E.g. it allows you to pass following constants:
Page constant
Page constant
Page constant
Page constant
Page constant
Additionally it allows you pass an integer, an array of integers, a range, defined as a string or a callback:
// constants $showOnPage = \SetaPDF_Stamper::PAGES_EVEN; $showOnPage = \SetaPDF_Stamper::PAGES_ODD; $showOnPage = \SetaPDF_Stamper::PAGES_LAST; // integer values $showOnPage = 1; $showOnPage = 3; // array of integers $showOnPage = array(1, 3, 5); // range $showOnPage = '2-'; // stamp 2nd to last page $showOnPage = '3-5'; // stamp 3rd to 5th page // callback to stamp the second last page $showOnPage = function($pageNumber, $pageCount) { return $pageNumber === ($pageCount - 1); }
You can check this parameter with the checkShowOnPageParameter()
method. It returns true on success or may throw an InvalidArgumentException exception if the argument is not a valid $showOnPage parameter.
Rotation
It is possible to define a rotation when you add a stamp to the stamper instance. The rotation origin is always the center of the stamp appearance. Additionally the stamp will be moved into the viewable area:
Callback
A stamp object may display dynamic content like the current page number or its size may vary depending on the page size by defining a callback that will be called just before the stamp is done. This parameter could be passed to the $callback
parameter of the addStamp()
method.
The callback has to return true if the stamp should be executed.
Stamping the Stamp Objects
As already seen in the previous examples the added stamps will get processed by calling the stamp()
method.
If you've full control over the page numbers that should get stamped you could also use the stampPageNo()
method.