Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions scripts/ESignaturePlaceholder/ESignaturePlaceholderTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace FPDF\Scripts\ESignaturePlaceholder;

trait ESignaturePlaceholderTrait {
protected $acroformObjId = 0;
protected $SigFields = [];

/**
* Add a e-signature placeholder
*
* @param float $x Abscissa of upper-left corner
* @param float $y Ordinate of upper-left corner
* @param float $w Width
* @param string $h Height
* @param string $name Name of pdf object
*/
public function AddSignatureField($x, $y, $w, $h, $name = '')
{
$this->PageLinks[$this->page][] = compact('name', 'x', 'y', 'w', 'h');

if (! isset($this->SigFields[$this->page])) {
$this->SigFields[$this->page] = true;
$this->PageLinks[$this->page][] = []; // just to reserve the oid on _putpages
}
}

protected function _putlinks($n)
{
$this->_putsigfields($n);
parent::_putlinks($n);
}

protected function _putsigfields($n)
{
$pageLinks = $this->PageLinks[$n];
$this->PageLinks[$n] = [];
$this->SigFields[$n] = [];
foreach ($pageLinks as $pl)
{
if (isset($pl['name'])) // it's a sig placeholder
$this->SigFields[$n][] = $pl;
elseif (isset($pl[0])) // it's a page link
$this->PageLinks[$n][] = $pl;
}

if (empty($this->SigFields[$n]))
return;

$fieldRefs = [];
$page = $this->PageInfo[$n]['n'] . ' 0 R';

foreach ($this->SigFields[$n] as $i => $f) {
$this->_newobj();
$fieldRefs[] = $this->n . ' 0 R';
$name = $f['name'] ?: 'Sign_' . $n. '_' . $i;
$rect = sprintf(
'%.2F %.2F %.2F %.2F',
$f['x'] * $this->k,
($this->h - $f['y'] - $f['h']) * $this->k,
($f['x'] + $f['w']) * $this->k,
($this->h - $f['y']) * $this->k
);
$this->_put("<</Type/Annot/Subtype/Widget/FT/Sig/V()/T({$name})/P {$page}/Rect[{$rect}]/F 132>>");
$this->_put('endobj');
}

$this->_newobj();
$this->acroformObjId = $this->n;
$this->_put('<</SigFlags 3/Fields[' . implode(' ', $fieldRefs) . ']>>');
$this->_put('endobj');
}

function _putcatalog()
{
parent::_putcatalog();
$this->_putesignatureplaceholdercatalog();
}

protected function _putesignatureplaceholdercatalog() {
if ($this->acroformObjId)
$this->_put('/AcroForm ' . $this->acroformObjId . ' 0 R');
}
}
51 changes: 51 additions & 0 deletions scripts/ESignaturePlaceholder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# ESignaturePlaceholderTrait
![GitHub license](https://img.shields.io/badge/license-FPDF-green)
[![Author](https://img.shields.io/badge/author-ErikN-blue)](https://github.com/erikn69)

This trait handles e-Signature Placeholders

## Usage
The method to add empty placeholder for e-signature:

```php
/**
* Add a e-signature placeholder
*
* @param float $x Abscissa of upper-left corner
* @param float $y Ordinate of upper-left corner
* @param float $w Width
* @param float $h Height
* @param string $name Name of pdf object
*/
function AddSignatureField(float $x, float $y, float $w, float $h, string $name = '');;
```

## Example

```php
declare(strict_types=1);

require dirname(dirname(__DIR__)) . '/fpdf/fpdf.php';
require __DIR__ . '/ESignaturePlaceholderTrait.php';

use FPDF\Scripts\ESignaturePlaceholder\ESignaturePlaceholderTrait;

$pdf = new class extends FPDF {
use ESignaturePlaceholderTrait;
};

$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdf->Text(20, 10, 'First Sign Here:');
// add placeholder with name `SignPlaceholder1` on page 1
$pdf->AddSignatureField(20, 15, 120, 40, 'SignPlaceholder1');

$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdf->Text(20, 10, 'Second Sign Here:');
// add placeholder with default name on page 2
$pdf->AddSignatureField(20, 15, 120, 40);

$pdf->Output('F', __DIR__ . '/example.pdf');
```
[Result](ex.pdf)
Binary file added scripts/ESignaturePlaceholder/ex.pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions scripts/ESignaturePlaceholder/ex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

require dirname(dirname(__DIR__)) . '/fpdf/fpdf.php';
require __DIR__ . '/ESignaturePlaceholderTrait.php';

use FPDF\Scripts\ESignaturePlaceholder\ESignaturePlaceholderTrait;

$pdf = new class extends FPDF {
use ESignaturePlaceholderTrait;
};

$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdf->Text(20, 10, 'First Sign Here:');
// add placeholder with name `SignPlaceholder1` on page 1
$pdf->AddSignatureField(20, 15, 120, 40, 'SignPlaceholder1');

$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);
$pdf->Text(20, 10, 'Second Sign Here:');
// add placeholder with default name on page 2
$pdf->AddSignatureField(20, 15, 120, 40);
1 change: 1 addition & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ $pdf = new class extends FPDF {
95. [Attachments](Attachments) by **Olivier** (2012-04-29)

- [QRcode](QRcode) by **Laurent MINGUET** (2010-04-29)
- [ESignaturePlaceholder](ESignaturePlaceholder) by **Erik N.** (2025-12-06)
5 changes: 5 additions & 0 deletions src/FawnoFPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Fawno\FPDF\Traits\BasicFunctionsTrait;
use Fawno\FPDF\Traits\EAN13Trait;
use Fawno\FPDF\Traits\FontsTrait;
use FPDF\Scripts\ESignaturePlaceholder\ESignaturePlaceholderTrait;
use FPDF\Scripts\Attachments\AttachmentsTrait;
use FPDF\Scripts\PDFBookmark\PDFBookmarkTrait;
use FPDF\Scripts\PDFCircularText\PDFCircularTextTrait;
Expand All @@ -29,6 +30,9 @@ class FawnoFPDF extends PDFWrapper {
AttachmentsTrait::_putresources as Attachments_putresources;
AttachmentsTrait::_putcatalog as Attachments_putcatalog;
}
use ESignaturePlaceholderTrait{
ESignaturePlaceholderTrait::_putcatalog as ESignaturePlaceholder_putcatalog;
}
use PDFBookmarkTrait {
PDFBookmarkTrait::_putresources as PDFBookmark_putresources;
PDFBookmarkTrait::_putcatalog as PDFBookmark_putcatalog;
Expand Down Expand Up @@ -59,5 +63,6 @@ protected function _putcatalog()
parent::_putcatalog();
$this->_putbookmarkscatalog();
$this->_putfilescatalog();
$this->_putesignatureplaceholdercatalog();
}
}
22 changes: 22 additions & 0 deletions tests/Scripts/ESignaturePlaceholderTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);

namespace Fawno\FPDF\Tests\Scripts;

use FPDF;
use FPDF\Scripts\ESignaturePlaceholder\ESignaturePlaceholderTrait;
use Fawno\FPDF\Tests\TestCase;

class ESignaturePlaceholderTraitTest extends TestCase {
public function testESignaturePlaceholderTrait () {
$pdf = new class extends FPDF {
use ESignaturePlaceholderTrait;
};

$pdf->AddSignatureField(20, 15, 120, 40);

$this->assertFileCanBeCreated($pdf);

$this->assertPdfIsOk($pdf);
}
}
Binary file modified tests/examples/example.pdf
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
$pdf->Bookmark('Paragraph 3', false, 1, -1);
$pdf->Cell(0, 6, 'Paragraph 3');

// ESignaturePlaceholderTrait
// Page 3
$pdf->AddPage();
$pdf->AddSignatureField(20, 15, 120, 40);
// Page 4
$pdf->AddPage();
$pdf->AddSignatureField(20, 15, 120, 40);

//EAN13Trait
$pdf->AddPage();
$pdf->Bookmark('EAN13 Barcode', false);
Expand Down
Loading