Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed handling of imported pages in templates with another unit than "pt" #217

Merged
merged 2 commits into from
Sep 2, 2024
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
30 changes: 30 additions & 0 deletions local-tests/page-in-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use setasign\Fpdi\Fpdi;

require_once '../vendor/autoload.php';

$pdf = new Fpdi('P', 'mm');
$pdf->setSourceFile(__DIR__ . '/../tests/_files/pdfs/Boombastic-Box.pdf');
$pageId = $pdf->importPage(1);

$pdf->beginTemplate();
$size = $pdf->useImportedPage($pageId, 10, 10, 100);
$pdf->SetDrawColor(0, 255, 0);
$pdf->Rect(10, 10, $size['width'], $size['height']);
$tplId = $pdf->endTemplate();

$pdf->AddPage();
$x = 10;
$y = 20;
$width = 190;

$size = $pdf->useTemplate($tplId, $x, $y, $width);

$pdf->SetDrawColor(255, 0, 0);
$pdf->Rect($x, $y, $size['width'], $size['height']);

$pdf->Output('F', 'page-in-template.pdf');

?>
<iframe src="http://pdfanalyzer2.dev1.setasign.local/plugin?file=<?php echo urlencode(realpath('page-in-template.pdf')); ?>" width="100%" height="98%"></iframe>
4 changes: 2 additions & 2 deletions src/FpdfTplTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ public function beginTemplate($width = null, $height = null, $groupXObject = fal
$this->currentTemplateId = $templateId;

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

$this->SetXY($this->lMargin, $this->tMargin);
$this->SetRightMargin($this->w - $width + $this->rMargin);
Expand Down
37 changes: 35 additions & 2 deletions tests/visual/FpdfTplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function getClassFile()
return __FILE__;
}

public function getInstance()
public function getInstance($unit = 'pt')
{
return new FpdfTpl('P', 'pt');
return new FpdfTpl('P', $unit);
}

public function createProvider()
Expand All @@ -32,6 +32,14 @@ public function createProvider()
0.1,
72 // dpi
],
[
[
'_method' => 'templateInTemplateMm',
'tmpPath' => 'templateInTemplateMm',
],
0.1,
72 // dpi
],
[
[
'_method' => 'fontHandlingA',
Expand Down Expand Up @@ -109,6 +117,31 @@ public function templateInTemplate($inputData, $outputFile)
$pdf->Output($outputFile, 'F');
}

public function templateInTemplateMm($inputData, $outputFile)
{
$pdf = $this->getInstance('mm');
$pdf->AddPage();

$tplIdx = $pdf->beginTemplate(36, 7);
$pdf->SetFont('Helvetica', '', 12);
$pdf->SetXY(0, 0);
$pdf->Cell(36, 7, 'My Test Template', 1);
$pdf->endTemplate();

$tplIdx2 = $pdf->beginTemplate(20, 6);
$pdf->useTemplate($tplIdx, 0, 0, 20);
$pdf->endTemplate();

$tplIdx3 = $pdf->beginTemplate();
$size = $pdf->useTemplate($tplIdx);
$pdf->useTemplate($tplIdx2, 0, $size['height']);
$pdf->endTemplate();

$pdf->useTemplate($tplIdx3, 10, 10);

$pdf->Output($outputFile, 'F');
}

/**
* No font was defined in the pages content scope. So it is needed to re-define it.
*
Expand Down
Binary file not shown.
175 changes: 175 additions & 0 deletions tests/visual/FpdiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

namespace setasign\Fpdi\visual;

use setasign\Fpdi\Fpdi;

class FpdiTest extends VisualTestCase
{
/**
* Should return __FILE__
*
* @return string
*/
public function getClassFile()
{
return __FILE__;
}

public function getInstance($unit = 'pt')
{
return new Fpdi('P', $unit);
}

public function createProvider()
{
return [
[
[
'_method' => 'importedPageInTemplatePt1',
'tmpPath' => 'importedPageInTemplatePt1',
],
0.1,
72 // dpi
],
[
[
'_method' => 'importedPageInTemplateMm1',
'tmpPath' => 'importedPageInTemplateMm1',
],
0.1,
72 // dpi
],
[
[
'_method' => 'importedPageInTemplatePt2',
'tmpPath' => 'importedPageInTemplatePt2',
],
0.1,
72 // dpi
],
[
[
'_method' => 'importedPageInTemplateMm2',
'tmpPath' => 'importedPageInTemplateMm2',
],
0.1,
72 // dpi
],
];
}

public function importedPageInTemplatePt1($inputData, $outputFile)
{
$pdf = $this->getInstance();

$pdf->setSourceFile(__DIR__ . '/../_files/pdfs/Boombastic-Box.pdf');
$pageIdx = $pdf->importPage(1);

// page looks identically
$pdf->AddPage();
$pdf->beginTemplate();
$size = $pdf->useTemplate($pageIdx);
$pdf->SetDrawColor(255, 0, 255);
$pdf->Rect(0, 0, $size['width'], $size['height']);
$tplIdx = $pdf->endTemplate();
$pdf->useTemplate($tplIdx);

// draw template with an offset and different size
$pdf->AddPage();
$size = $pdf->useTemplate($tplIdx, 10, 50, 250);
$pdf->SetDrawColor(0, 255, 0);
$pdf->Rect(10, 50, $size['width'], $size['height']);

$pdf->Output('F', $outputFile);
}

public function importedPageInTemplateMm1($inputData, $outputFile)
{
$pdf = $this->getInstance('mm');

$pdf->setSourceFile(__DIR__ . '/../_files/pdfs/Boombastic-Box.pdf');
$pageIdx = $pdf->importPage(1);

// page looks identically
$pdf->AddPage();
$pdf->beginTemplate();
$size = $pdf->useTemplate($pageIdx);
$pdf->SetDrawColor(255, 0, 255);
$pdf->Rect(0, 0, $size['width'], $size['height']);
$tplIdx = $pdf->endTemplate();
$pdf->useTemplate($tplIdx);

// draw template with an offset and different size
$pdf->AddPage();
$size = $pdf->useTemplate($tplIdx, 10, 20, 100);
$pdf->SetDrawColor(0, 255, 0);
$pdf->Rect(10, 20, $size['width'], $size['height']);

$pdf->Output('F', $outputFile);
}

public function importedPageInTemplatePt2($inputData, $outputFile)
{
$pdf = $this->getInstance();

$pdf->setSourceFile(__DIR__ . '/../_files/pdfs/Boombastic-Box.pdf');
$pageIdx = $pdf->importPage(1);

// draw the page onto another templage
$pdf->AddPage();
$pdf->beginTemplate(100, 200);
$pdf->SetDrawColor(255, 0, 0);
$pdf->Rect(0, 0, 100, 200);
$size = $pdf->useTemplate($pageIdx, 10, 10, 80, 180);
$pdf->SetDrawColor(0, 0, 255);
$pdf->Rect(10, 10, $size['width'], $size['height']);
$tplIdx = $pdf->endTemplate();
$pdf->useTemplate($tplIdx, 10, 10);

// create an additional template and draw the previous template onto this
$pdf->AddPage();
$pdf->beginTemplate(150, 200);
$pdf->SetDrawColor(255, 0, 0);
$pdf->Rect(0, 0, 150, 200);
$size = $pdf->useTemplate($tplIdx, 10, 10, null, 180);
$pdf->SetDrawColor(0, 255, 0);
$pdf->Rect(10, 10, $size['width'], 180);
$tplIdx2 = $pdf->endTemplate();
$pdf->useTemplate($tplIdx2, 30, 100);

$pdf->Output('F', $outputFile);
}

public function importedPageInTemplateMm2($inputData, $outputFile)
{
$pdf = $this->getInstance('mm');

$pdf->setSourceFile(__DIR__ . '/../_files/pdfs/Boombastic-Box.pdf');
$pageIdx = $pdf->importPage(1);

// draw the page onto another templage
$pdf->AddPage();
$pdf->beginTemplate(100, 200);
$pdf->SetDrawColor(255, 0, 0);
$pdf->Rect(0, 0, 100, 200);
$size = $pdf->useTemplate($pageIdx, 10, 10, 80, 180);
$pdf->SetDrawColor(0, 0, 255);
$pdf->Rect(10, 10, $size['width'], $size['height']);
$tplIdx = $pdf->endTemplate();
$pdf->useTemplate($tplIdx, 10, 10);

// create an additional template and draw the previous template onto this
$pdf->AddPage();
$pdf->beginTemplate(100, 150);
$pdf->SetDrawColor(255, 0, 0);
$pdf->Rect(0, 0, 100, 150);
$size = $pdf->useTemplate($tplIdx, 10, 10, null, 130);
$pdf->SetDrawColor(0, 255, 0);
$pdf->Rect(10, 10, $size['width'], 130);
$tplIdx2 = $pdf->endTemplate();
$pdf->useTemplate($tplIdx2, 10, 30);

$pdf->Output('F', $outputFile);
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/visual/Tfpdf/FpdfTplTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public function getClassFile()
return __FILE__;
}

public function getInstance()
public function getInstance($unit = 'pt')
{
return new FpdfTpl('P', 'pt');
return new FpdfTpl('P', $unit);
}
}
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/visual/Tfpdf/FpdiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Tfpdf;

use setasign\Fpdi\Tfpdf\Fpdi;

class FpdiTest extends \setasign\Fpdi\visual\FpdiTest
{
/**
* Should return __FILE__
*
* @return string
*/
public function getClassFile()
{
return __FILE__;
}

public function getInstance($unit = 'pt')
{
return new Fpdi('P', $unit);
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading