Skip to content

Commit

Permalink
Merge pull request #174 from Setasign/link-handling
Browse files Browse the repository at this point in the history
Link handling
  • Loading branch information
JanSlabon committed Jul 12, 2023
2 parents f79f2e6 + d41d39a commit 8e02c37
Show file tree
Hide file tree
Showing 35 changed files with 1,845 additions and 278 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"phpunit/phpunit": "~5.7",
"setasign/fpdf": "~1.8",
"tecnickcom/tcpdf": "~6.2",
"setasign/tfpdf": "^1.31",
"setasign/tfpdf": "~1.31",
"squizlabs/php_codesniffer": "^3.5"
},
"autoload-dev": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion local-tests/concatenate.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$pdf->AddPage();

$pageId = $pdf->importPage($pageNo, 'ArtBox');
$pageId = $pdf->importPage($pageNo, 'TrimBox', true, true);
if ($useLegacy) {
$pdf->useTemplate($pageId, null, null, 0, 0, true);
} else {
Expand Down
10 changes: 6 additions & 4 deletions local-tests/simple.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
}

$files = [
__DIR__ . '/../tests/_files/pdfs/Fantastic-Speaker.pdf',
// __DIR__ . '/../tests/_files/pdfs/Fantastic-Speaker.pdf',
// __DIR__ . '/../tests/_files/pdfs/links/links.pdf',
__DIR__ . '/../tests/_files/pdfs/links/boxes.pdf',
// __DIR__ . '/../tests/_files/pdfs/stamps/ENU/StandardBusiness.pdf',
// __DIR__ . '/../tests/_files/pdfs/tektown/Logo.pdf',
// __DIR__ . '/../tests/_files/pdfs/1000.pdf',
Expand All @@ -37,13 +39,13 @@
$pageCount = $pdf->setSourceFile($file);

for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$pdf->AddPage('landscape', 'A3');
$pageId = $pdf->importPage($pageNo, Fpdi\PdfReader\PageBoundaries::MEDIA_BOX);
$pdf->AddPage('L', 'A3');
$pageId = $pdf->importPage($pageNo, Fpdi\PdfReader\PageBoundaries::MEDIA_BOX, true, true);
$pdf->useTemplate($pageId, 20, 20, 100, 100);
$pdf->Rect(20, 20, 100, 100);
#var_dump($pageId);

$pageId = $pdf->importPage($pageNo, Fpdi\PdfReader\PageBoundaries::ART_BOX);
$pageId = $pdf->importPage($pageNo, Fpdi\PdfReader\PageBoundaries::ART_BOX, true, true);
$s = $pdf->useTemplate($pageId, 120, 120, 100);
#$s = $pdf->useTemplate($pageId, null, null, 100, null, true);
$pdf->Rect(120, 120, $s['width'], $s['height']);
Expand Down
11 changes: 7 additions & 4 deletions src/FpdfTplTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Trait FpdfTplTrait
*
* This class adds a templating feature to tFPDF.
* This class adds a templating feature to FPDF and tFPDF.
*/
trait FpdfTplTrait
{
Expand Down Expand Up @@ -234,7 +234,9 @@ public function beginTemplate($width = null, $height = null, $groupXObject = fal
'lMargin' => $this->lMargin,
'rMargin' => $this->rMargin,
'h' => $this->h,
'hPt' => $this->hPt,
'w' => $this->w,
'wPt' => $this->wPt,
'FontFamily' => $this->FontFamily,
'FontStyle' => $this->FontStyle,
'FontSizePt' => $this->FontSizePt,
Expand All @@ -251,7 +253,9 @@ public function beginTemplate($width = null, $height = null, $groupXObject = fal
$this->currentTemplateId = $templateId;

$this->h = $height;
$this->hPt = $height / $this->k;
$this->w = $width;
$this->wPt = $width / $this->k;

$this->SetXY($this->lMargin, $this->tMargin);
$this->SetRightMargin($this->w - $width + $this->rMargin);
Expand Down Expand Up @@ -279,7 +283,9 @@ public function endTemplate()
$this->lMargin = $state['lMargin'];
$this->rMargin = $state['rMargin'];
$this->h = $state['h'];
$this->hPt = $state['hPt'];
$this->w = $state['w'];
$this->wPt = $state['wPt'];
$this->SetAutoPageBreak($state['AutoPageBreak'], $state['bMargin']);

$this->FontFamily = $state['FontFamily'];
Expand Down Expand Up @@ -406,9 +412,6 @@ public function SetFontSize($size)
}
}

/**
* @inheritdoc
*/
protected function _putimages()
{
parent::_putimages();
Expand Down
205 changes: 205 additions & 0 deletions src/FpdfTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<?php

/**
* This file is part of FPDI
*
* @package setasign\Fpdi
* @copyright Copyright (c) 2023 Setasign GmbH & Co. KG (https://www.setasign.com)
* @license http://opensource.org/licenses/mit-license The MIT License
*/

namespace setasign\Fpdi;

use setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException;
use setasign\Fpdi\PdfParser\PdfParserException;
use setasign\Fpdi\PdfParser\Type\PdfIndirectObject;
use setasign\Fpdi\PdfParser\Type\PdfNull;

/**
* This trait is used for the implementation of FPDI in FPDF and tFPDF.
*/
trait FpdfTrait
{
protected function _enddoc()
{
parent::_enddoc();
$this->cleanUp();
}

/**
* Draws an imported page or a template onto the page or another template.
*
* Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
* aspect ratio.
*
* @param mixed $tpl The template id
* @param float|int|array $x The abscissa of upper-left corner. Alternatively you could use an assoc array
* with the keys "x", "y", "width", "height", "adjustPageSize".
* @param float|int $y The ordinate of upper-left corner.
* @param float|int|null $width The width.
* @param float|int|null $height The height.
* @param bool $adjustPageSize
* @return array The size
* @see Fpdi::getTemplateSize()
*/
public function useTemplate($tpl, $x = 0, $y = 0, $width = null, $height = null, $adjustPageSize = false)
{
if (isset($this->importedPages[$tpl])) {
$size = $this->useImportedPage($tpl, $x, $y, $width, $height, $adjustPageSize);
if ($this->currentTemplateId !== null) {
$this->templates[$this->currentTemplateId]['resources']['templates']['importedPages'][$tpl] = $tpl;
}
return $size;
}

return parent::useTemplate($tpl, $x, $y, $width, $height, $adjustPageSize);
}

/**
* Get the size of an imported page or template.
*
* Give only one of the size parameters (width, height) to calculate the other one automatically in view to the
* aspect ratio.
*
* @param mixed $tpl The template id
* @param float|int|null $width The width.
* @param float|int|null $height The height.
* @return array|bool An array with following keys: width, height, 0 (=width), 1 (=height), orientation (L or P)
*/
public function getTemplateSize($tpl, $width = null, $height = null)
{
$size = parent::getTemplateSize($tpl, $width, $height);
if ($size === false) {
return $this->getImportedPageSize($tpl, $width, $height);
}

return $size;
}

/**
* @throws CrossReferenceException
* @throws PdfParserException
*/
protected function _putimages()
{
$this->currentReaderId = null;
parent::_putimages();

foreach ($this->importedPages as $key => $pageData) {
$this->_newobj();
$this->importedPages[$key]['objectNumber'] = $this->n;
$this->currentReaderId = $pageData['readerId'];
$this->writePdfType($pageData['stream']);
$this->_put('endobj');
}

foreach (\array_keys($this->readers) as $readerId) {
$parser = $this->getPdfReader($readerId)->getParser();
$this->currentReaderId = $readerId;

while (($objectNumber = \array_pop($this->objectsToCopy[$readerId])) !== null) {
try {
$object = $parser->getIndirectObject($objectNumber);
} catch (CrossReferenceException $e) {
if ($e->getCode() === CrossReferenceException::OBJECT_NOT_FOUND) {
$object = PdfIndirectObject::create($objectNumber, 0, new PdfNull());
} else {
throw $e;
}
}

$this->writePdfType($object);
}
}

$this->currentReaderId = null;
}

/**
* @inheritdoc
*/
protected function _putxobjectdict()
{
foreach ($this->importedPages as $pageData) {
$this->_put('/' . $pageData['id'] . ' ' . $pageData['objectNumber'] . ' 0 R');
}

parent::_putxobjectdict();
}

/**
* @param int $n
* @return void
* @throws PdfParser\Type\PdfTypeException
*/
protected function _putlinks($n)
{
foreach ($this->PageLinks[$n] as $pl) {
$this->_newobj();
$rect = sprintf('%.2F %.2F %.2F %.2F', $pl[0], $pl[1], $pl[0] + $pl[2], $pl[1] - $pl[3]);
$this->_put('<</Type /Annot /Subtype /Link /Rect [' . $rect . ']', false);
if (is_string($pl[4])) {
$this->_put('/A <</S /URI /URI ' . $this->_textstring($pl[4]) . '>>');
if (isset($pl['importedLink'])) {
$values = $pl['importedLink']['pdfObject']->value;
unset(
$values['P'],
$values['NM'],
$values['AP'],
$values['AS'],
$values['Type'],
$values['Subtype'],
$values['Rect'],
$values['A'],
$values['QuadPoints'],
$values['Rotate'],
$values['M'],
$values['StructParent']
);

foreach ($values as $name => $entry) {
$this->_put('/' . $name . ' ', false);
$this->writePdfType($entry);
}

if (isset($pl['quadPoints'])) {
$s = '/QuadPoints[';
foreach ($pl['quadPoints'] as $value) {
$s .= sprintf('%.2F ', $value);
}
$s .= ']';
$this->_put($s);
}
} else {
$this->_put('/Border [0 0 0]', false);
}
$this->_put('>>');
} else {
$this->_put('/Border [0 0 0] ', false);
$l = $this->links[$pl[4]];
if (isset($this->PageInfo[$l[0]]['size'])) {
$h = $this->PageInfo[$l[0]]['size'][1];
} else {
$h = ($this->DefOrientation === 'P')
? $this->DefPageSize[1] * $this->k
: $this->DefPageSize[0] * $this->k;
}
$this->_put(sprintf(
'/Dest [%d 0 R /XYZ 0 %.2F null]>>',
$this->PageInfo[$l[0]]['n'],
$h - $l[1] * $this->k
));
}
$this->_put('endobj');
}
}

protected function _put($s, $newLine = true)
{
if ($newLine) {
$this->buffer .= $s . "\n";
} else {
$this->buffer .= $s;
}
}
}
Loading

0 comments on commit 8e02c37

Please sign in to comment.