Skip to content

Commit

Permalink
Adds a draft for Header and Footer ViewHelper support
Browse files Browse the repository at this point in the history
  • Loading branch information
maechler committed Sep 1, 2018
1 parent 2ead3fc commit bb942c3
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 0 deletions.
118 changes: 118 additions & 0 deletions Classes/Model/BasePDF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Bithost\Pdfviewhelpers\Model;

/***
*
* This file is part of the "PDF ViewHelpers" Extension for TYPO3 CMS.
*
* (c) 2016 Markus Mächler <markus.maechler@bithost.ch>, Bithost GmbH
* Esteban Marin <esteban.marin@bithost.ch>, Bithost GmbH
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***/

use FPDI;
use Closure;

/**
* BasePDF
*
* @author Markus Mächler <markus.maechler@bithost.ch>, Esteban Marin <esteban.marin@bithost.ch>
*/
class BasePDF extends FPDI
{
/**
* Indicating whether the current page is using a template.
* This is needed to automatically add the template on an automatic page break.
*
* @var bool
*/
protected $importTemplateOnThisPage = false;

/**
* @var Closure
*/
protected $headerClosure = null;

/**
* @var Closure
*/
protected $footerClosure = null;

/**
* @return void
*/
public function Header() // phpcs:ignore
{
if ($this->headerClosure instanceof Closure) {
$this->headerClosure->__invoke();
}
}

/**
* @return void
*/
public function Footer() // phpcs:ignore
{
if ($this->footerClosure instanceof Closure) {
$this->footerClosure->__invoke();
}
}

/**
* Fixes importPage not working with autoPageBreak=1, see https://github.com/bithost-gmbh/pdfviewhelpers/issues/41
*
* @return void
*/
public function AddPage($orientation = '', $format = '', $rotationOrKeepmargins = false, $tocpage = false) // phpcs:ignore
{
parent::AddPage($orientation, $format, $rotationOrKeepmargins, $tocpage);

if ($this->importTemplateOnThisPage && $this->tpl !== 0) {
$this->useTemplate($this->tpl);
}
}

/**
* @param bool $importTemplateOnThisPage
*
* @return void
*/
public function setImportTemplateOnThisPage($importTemplateOnThisPage)
{
$this->importTemplateOnThisPage = $importTemplateOnThisPage;
}

/**
* @return void
*/
public function setHeaderClosure(Closure $closure)
{
$this->headerClosure = $closure;
}

/**
* @return void
*/
public function setFooterClosure(Closure $closure)
{
$this->footerClosure = $closure;
}
}
75 changes: 75 additions & 0 deletions Classes/ViewHelpers/FooterViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Bithost\Pdfviewhelpers\ViewHelpers;

/* * *
*
* This file is part of the "PDF ViewHelpers" Extension for TYPO3 CMS.
*
* (c) 2016 Markus Mächler <markus.maechler@bithost.ch>, Bithost GmbH
* Esteban Marin <esteban.marin@bithost.ch>, Bithost GmbH
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* * */

use Bithost\Pdfviewhelpers\Exception\Exception;
use Bithost\Pdfviewhelpers\Model\BasePDF;

/**
* FooterViewHelper
*
* @author Markus Mächler <markus.maechler@bithost.ch>, Esteban Marin <esteban.marin@bithost.ch>
*/
class FooterViewHelper extends AbstractContentElementViewHelper
{
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();

$this->overrideArgument('posY', 'integer', '', false, $this->settings['footer']['posY']);
}

/**
* @throws Exception
*
* @return void
*/
public function render()
{
if (!($this->getPDF() instanceof BasePDF)) {
throw new Exception("Your PDF class must be an instance of Bithost\\Pdfviewhelpers\\Model\\BasePDF in order to support Header and Footer ViewHelper.");
}

$arguments = $this->arguments;
$renderChildrenClosure = $this->buildRenderChildrenClosure();
$pdf = $this->getPDF();
$footerClosure = function() use ($pdf, $arguments, $renderChildrenClosure) {
if ($arguments['posY']) {
$pdf->SetY($arguments['posY']);
}

$renderChildrenClosure();
};

$this->getPDF()->setFooterClosure($footerClosure);
}
}
45 changes: 45 additions & 0 deletions Classes/ViewHelpers/GetPageNumberAliasViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Bithost\Pdfviewhelpers\ViewHelpers;

/* * *
*
* This file is part of the "PDF ViewHelpers" Extension for TYPO3 CMS.
*
* (c) 2016 Markus Mächler <markus.maechler@bithost.ch>, Bithost GmbH
* Esteban Marin <esteban.marin@bithost.ch>, Bithost GmbH
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* * */

/**
* GetPageNumberAliasViewHelper
*
* @author Markus Mächler <markus.maechler@bithost.ch>, Esteban Marin <esteban.marin@bithost.ch>
*/
class GetPageNumberAliasViewHelper extends AbstractPDFViewHelper
{
/**
* @return string
*/
public function render()
{
return $this->getPDF()->getAliasNumPage();
}
}
45 changes: 45 additions & 0 deletions Classes/ViewHelpers/GetTotalNumberOfPagesAliasViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Bithost\Pdfviewhelpers\ViewHelpers;

/* * *
*
* This file is part of the "PDF ViewHelpers" Extension for TYPO3 CMS.
*
* (c) 2016 Markus Mächler <markus.maechler@bithost.ch>, Bithost GmbH
* Esteban Marin <esteban.marin@bithost.ch>, Bithost GmbH
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* * */

/**
* GetTotalNumberOfPagesAliasViewHelper
*
* @author Markus Mächler <markus.maechler@bithost.ch>, Esteban Marin <esteban.marin@bithost.ch>
*/
class GetTotalNumberOfPagesAliasViewHelper extends AbstractPDFViewHelper
{
/**
* @return string
*/
public function render()
{
return $this->getPDF()->getAliasNbPages();
}
}
75 changes: 75 additions & 0 deletions Classes/ViewHelpers/HeaderViewHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Bithost\Pdfviewhelpers\ViewHelpers;

/* * *
*
* This file is part of the "PDF ViewHelpers" Extension for TYPO3 CMS.
*
* (c) 2016 Markus Mächler <markus.maechler@bithost.ch>, Bithost GmbH
* Esteban Marin <esteban.marin@bithost.ch>, Bithost GmbH
*
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
* * */

use Bithost\Pdfviewhelpers\Exception\Exception;
use Bithost\Pdfviewhelpers\Model\BasePDF;

/**
* HeaderViewHelper
*
* @author Markus Mächler <markus.maechler@bithost.ch>, Esteban Marin <esteban.marin@bithost.ch>
*/
class HeaderViewHelper extends AbstractContentElementViewHelper
{
/**
* @return void
*/
public function initializeArguments()
{
parent::initializeArguments();

$this->overrideArgument('posY', 'integer', '', false, $this->settings['header']['posY']);
}

/**
* @throws Exception
*
* @return void
*/
public function render()
{
if (!($this->getPDF() instanceof BasePDF)) {
throw new Exception("Your PDF class must be an instance of Bithost\\Pdfviewhelpers\\Model\\BasePDF in order to support Header and Footer ViewHelper.");
}

$arguments = $this->arguments;
$renderChildrenClosure = $this->buildRenderChildrenClosure();
$pdf = $this->getPDF();
$headerClosure = function() use ($pdf, $arguments, $renderChildrenClosure) {
if ($arguments['posY']) {
$pdf->SetY($arguments['posY']);
}

$renderChildrenClosure();
};

$this->getPDF()->setHeaderClosure($headerClosure);
}
}
6 changes: 6 additions & 0 deletions Configuration/TypoScript/setup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ plugin.tx_pdfviewhelpers.settings {
orientation = P
format = A4
}
header {
posY = 5
}
footer {
posY = -10
}
generalText {
trim = 1
removeDoubleWhitespace = 1
Expand Down
15 changes: 15 additions & 0 deletions Resources/Public/Examples/HeaderFooter/Template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{namespace pdf=Bithost\Pdfviewhelpers\ViewHelpers}

<pdf:document outputDestination="I" title="Bithost Example">
<pdf:header>
<pdf:text>Header - page {pdf:getPageNumberAlias()} of {pdf:getTotalNumberOfPagesAlias()}</pdf:text>
</pdf:header>
<pdf:footer>
<pdf:text>Footer - page {pdf:getPageNumberAlias()} of {pdf:getTotalNumberOfPagesAlias()}</pdf:text>
</pdf:footer>
<pdf:page>
<pdf:text>Content goes here: page 1</pdf:text>
<pdf:pageBreak />
<pdf:text>Content goes here: page 2</pdf:text>
</pdf:page>
</pdf:document>
Loading

0 comments on commit bb942c3

Please sign in to comment.