The Main Class

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
public SetaPDF_Stamper::addStamp (
SetaPDF_Stamper_Stamp $stamp [, string|array $positionOrConfig = SetaPDF_Stamper::POSITION_LEFT_TOP [, int|string|array|callback $showOnPage = SetaPDF_Stamper::PAGES_ALL [, int $translateX = 0 [, int $translateY = 0 [, float $rotation = 0.0 [, bool $underlay = false [, null|callback $callback = null ]]]]]]]
): void

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:

PHP
$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.

public const string SetaPDF_Stamper::POSITION_CENTER_BOTTOM = 'CB'

Position constant

public const string SetaPDF_Stamper::POSITION_CENTER_MIDDLE = 'CM'

Position constant

public const string SetaPDF_Stamper::POSITION_CENTER_TOP = 'CT'

Position constant

public const string SetaPDF_Stamper::POSITION_LEFT_BOTTOM = 'LB'

Position constant

public const string SetaPDF_Stamper::POSITION_LEFT_MIDDLE = 'LM'

Position constant

public const string SetaPDF_Stamper::POSITION_LEFT_TOP = 'LT'

Position constant

public const string SetaPDF_Stamper::POSITION_RIGHT_BOTTOM = 'RB'

Position constant

public const string SetaPDF_Stamper::POSITION_RIGHT_MIDDLE = 'RM'

Position constant

public const string SetaPDF_Stamper::POSITION_RIGHT_TOP = 'RT'

Position constant

A simple example:  

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new \SetaPDF_Core_Writer_Http('positioning.pdf', true);
// we create a fresh document instance for demonstration purpose
$document = new \SetaPDF_Core_Document($writer);
// create at least one page
$document->getCatalog()->getPages()->create(\SetaPDF_Core_PageFormats::A4);

// create a stamper instance
$stamper = new \SetaPDF_Stamper($document);

// create a font object
$font = \SetaPDF_Core_Font_Standard_Helvetica::create($document);

// create simple text stamp
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 12);
$stamp->setBorderWidth(1);
$stamp->setPadding(4);
$stamp->setAlign(\SetaPDF_Core_Text::ALIGN_CENTER);
$stamp->setText("Positioning\nDemo");

// default position: left top
$stamper->addStamp($stamp, array(
    'translateX' => 10,
    'translateY' => -10,
));

// right top
$stamper->addStamp($stamp, array(
    'position' => \SetaPDF_Stamper::POSITION_RIGHT_TOP,
    'translateX' => -10,
    'translateY' => -10,
));

// right bottom
$stamper->addStamp($stamp, array(
    'position' => \SetaPDF_Stamper::POSITION_RIGHT_BOTTOM,
    'translateX' => -10,
    'translateY' => 10,
));

// left bottom
$stamper->addStamp($stamp, array(
    'position' => \SetaPDF_Stamper::POSITION_LEFT_BOTTOM,
    'translateX' => 10,
    'translateY' => 10,
));

// center
$stamper->addStamp($stamp, \SetaPDF_Stamper::POSITION_CENTER_MIDDLE);

// stamp the document
$stamper->stamp();

// show the whole page at opening time
$document->getCatalog()->setPageLayout(\SetaPDF_Core_Document_PageLayout::SINGLE_PAGE);

// save and send it to the client
$document->save()->finish();

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:

public const string SetaPDF_Stamper::PAGES_ALL = 'all'

Page constant

public const string SetaPDF_Stamper::PAGES_EVEN = 'even'

Page constant

public const string SetaPDF_Stamper::PAGES_FIRST = 'first'

Page constant

public const string SetaPDF_Stamper::PAGES_LAST = 'last'

Page constant

public const string SetaPDF_Stamper::PAGES_ODD = 'odd'

Page constant

Additionally it allows you pass an integer, an array of integers, a range, defined as a string or a callback: 

PHP
// 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: 

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new \SetaPDF_Core_Writer_Http('rotation.pdf', true);
// we create a fresh document instance for demonstration purpose
$document = new \SetaPDF_Core_Document($writer);
// create at least one page
$document->getCatalog()->getPages()->create(\SetaPDF_Core_PageFormats::A4);

// create a stamper instance
$stamper = new \SetaPDF_Stamper($document);

// create a font object
$font = \SetaPDF_Core_Font_Standard_Helvetica::create($document);

// create simple text stamp
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 30);
$stamp->setBorderWidth(1);
$stamp->setPadding(4);
$stamp->setAlign(\SetaPDF_Core_Text::ALIGN_CENTER);
$stamp->setText("Rotation\nDemo");

// left top with 45 degrees rotation
$stamper->addStamp($stamp, array(
    'rotation' => 45
));

// right top with 90 degrees rotation
$stamper->addStamp($stamp, array(
    'position' => \SetaPDF_Stamper::POSITION_RIGHT_TOP,
    'rotation' => 90
));

// stamp the document
$stamper->stamp();

// show the whole page at opening time
$document->getCatalog()->setPageLayout(\SetaPDF_Core_Document_PageLayout::SINGLE_PAGE);

// save and send it to the client
$document->save()->finish();

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. 

PHP
<?php
require_once('library/SetaPDF/Autoload.php');

// create a writer
$writer = new \SetaPDF_Core_Writer_Http('callback.pdf', true);
// we create a fresh document instance for demonstration purpose
$document = new \SetaPDF_Core_Document($writer);
// get the pages instance
$pages = $document->getCatalog()->getPages();
// create 20 pages
for ($i = 0; $i < 20; $i++) {
    $orientation = $i % 2
        ? \SetaPDF_Core_PageFormats::ORIENTATION_LANDSCAPE
        : \SetaPDF_Core_PageFormats::ORIENTATION_PORTRAIT;

    $pages->create(\SetaPDF_Core_PageFormats::A4, $orientation);
}

// create a stamper instance
$stamper = new \SetaPDF_Stamper($document);

// create a font object
$font = \SetaPDF_Core_Font_Standard_Helvetica::create($document);

// create simple text stamp
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 16);
$stamp->setPadding(4);
$stamp->setAlign(\SetaPDF_Core_Text::ALIGN_RIGHT);

/**
 * @param $pageNumber The current page number that should be stamped
 * @param $pageCount The page count of the document
 * @param \SetaPDF_Core_Document_Page $page The page instance of the current page
 * @param \SetaPDF_Stamper_Stamp_Text $stamp The stamp object
 * @param array $currentStampData The data that were passed to the addStamp() method.
 *
 * @return bool
 */
$callback = function(
    $pageNumber,
    $pageCount,
    \SetaPDF_Core_Document_Page $page,
    \SetaPDF_Stamper_Stamp_Text $stamp,
    array &$currentStampData
) {
    // set the page number
    $stamp->setText('Page ' . $pageNumber . ' / ' . $pageCount);
    // increase the font size for demonstration purpose
    $stamp->setFontSize($stamp->getFontSize() + 2);

    // if the page is landscape we want to position the stamp on the lower left with a rotation of 45 degrees
    if ($page->getWidth() > $page->getHeight()) {
        $currentStampData['position'] = \SetaPDF_Stamper::POSITION_LEFT_BOTTOM;
        $currentStampData['rotation'] = 45;
    // otherwise lower right and a -45 degree rotation
    } else {
        $currentStampData['position'] = \SetaPDF_Stamper::POSITION_RIGHT_BOTTOM;
        $currentStampData['rotation'] = -45;
    }

    return true;
};

// right bottom and callback
$stamper->addStamp($stamp, array(
    'position' => \SetaPDF_Stamper::POSITION_RIGHT_BOTTOM,
    'callback' => $callback
));

// stamp the document
$stamper->stamp();

// show the whole page at opening time
$document->getCatalog()->setPageLayout(\SetaPDF_Core_Document_PageLayout::SINGLE_PAGE);

// save and send it to the client
$document->save()->finish();

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.