Skip to content

Instantly share code, notes, and snippets.

@mcustiel
Last active December 13, 2017 12:21
Show Gist options
  • Save mcustiel/76816123d911d694e03aac51ac74a8d1 to your computer and use it in GitHub Desktop.
Save mcustiel/76816123d911d694e03aac51ac74a8d1 to your computer and use it in GitHub Desktop.
Fpdi extensions from the internet
<?php
namespace Mcustiel\Tests;
use setasign\Fpdi\Fpdi;
class MyFpdi extends Fpdi
{
const NEW_LINE = "\n";
const CARRIAGE_RETURN = "\r";
const SPACE = ' ';
/**
* @var float
*/
public $angle = 0;
/**
* @param float $c
* @param float $m
* @param float $y
* @param float $k
*/
public function SetTextColorCMYK($c, $m, $y, $k)
{
// Set color for text
$this->TextColor = sprintf('%.3F %.3F %.3F %.3F k', $c / 100, $m / 100, $y / 100, $k / 100);
$this->ColorFlag = ($this->FillColor != $this->TextColor);
}
/**
* @param float $angle
*/
public function Rotate($angle)
{
if ($this->angle != 0) {
$this->_out('Q');
}
$this->angle = $angle;
if ($angle != 0) {
$angle = deg2rad($angle);
$cos = cos($angle);
$sin = sin($angle);
$horizontalTranslation = $this->x * $this->k;
$verticalTranslation = ($this->h - $this->y) * $this->k;
$this->_out(
sprintf(
'q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',
$cos,
$sin,
-$sin,
$cos,
$horizontalTranslation,
$verticalTranslation,
-$horizontalTranslation,
-$verticalTranslation
)
);
}
}
public function _endpage()
{
if ($this->angle != 0) {
$this->angle = 0;
$this->_out('Q');
}
parent::_endpage();
}
// TODO: Here we can simulate horizontal alignments from InDesign
// TODO: Check what happens it we try to simulate vertical alignment of the text in its own line
public function TextBox($text, $width, $height, $horizontalAlign = 'L', $verticalAlign = 'T', $lineHeightScale = 1.0)
{
$cellMargin = $this->cMargin;
$this->cMargin = 0;
$rowHeight = $this->FontSize * $lineHeightScale;
$lines = explode(self::NEW_LINE, $text);
$contentBlockHeight = $height - $rowHeight * count($lines);
$yPos = $this->getTextTopPosition($verticalAlign, $contentBlockHeight, $cellMargin);
$xPos = $this->GetX();
foreach ($lines as $line) {
$this->SetY($yPos);
$this->SetX($xPos);
$this->Cell($width, $rowHeight, trim($line), 0, 0, $horizontalAlign);
$yPos += $rowHeight - $this->LineWidth / 2.0;
}
$this->SetX($xPos);
$this->setY($yPos + $height);
$this->cMargin = $cellMargin;
}
/**
* @param string $verticalAlign
* @param float $contentBlockHeight
* @param float $cellMargin
*
* @return float
*/
private function getTextTopPosition($verticalAlign, $contentBlockHeight, $cellMargin)
{
switch ($verticalAlign) {
case 'B':
$yPos = $this->GetY() + $contentBlockHeight;
break;
case 'M':
$yPos = $this->GetY() + $contentBlockHeight / 2.0;
break;
default:
$yPos = $this->GetY();
}
$yPos += $cellMargin / 2.0;
return $yPos;
}
}
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use Mcustiel\Tests\MyFpdi;
$fpdi = new MyFpdi('L', 'mm', [100, 200]);
$fpdi->AddPage();
$templateId = $fpdi->beginTemplate();
$fpdi->SetFont('Arial');
$fpdi->SetFontSize(16);
$fpdi->SetTextColorCMYK(10, 25, 85, 0);
$fpdi->SetY(0);
$fpdi->TextBox('I am a potato', 60, 20);
$fpdi->Rect($fpdi->GetX(), 0, 60, 20);
$fpdi->SetY(25);
$fpdi->SetTextColorCMYK(0, 0, 0, 77);
$fpdi->TextBox('I am a potato', 60, 20, 'C', 'M');
$fpdi->Rect($fpdi->GetX(), 25, 60, 20);
$fpdi->SetY(50);
$fpdi->SetTextColorCMYK(0, 11, 12, 0);
$fpdi->TextBox('I am a potato', 60, 20, 'R', 'B');
$fpdi->Rect($fpdi->GetX(), 50, 60, 20);
$fpdi->endTemplate();
$fpdi->useTemplate($templateId);
$fpdi->Output('F', __DIR__ . '/out.pdf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment