diff --git a/local-tests/page-in-template.php b/local-tests/page-in-template.php new file mode 100644 index 0000000..29a0072 --- /dev/null +++ b/local-tests/page-in-template.php @@ -0,0 +1,30 @@ +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'); + +?> + diff --git a/src/FpdfTplTrait.php b/src/FpdfTplTrait.php index 2da9d55..8d415f2 100644 --- a/src/FpdfTplTrait.php +++ b/src/FpdfTplTrait.php @@ -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); diff --git a/tests/visual/FpdfTplTest.php b/tests/visual/FpdfTplTest.php index 5f5018c..e84f403 100644 --- a/tests/visual/FpdfTplTest.php +++ b/tests/visual/FpdfTplTest.php @@ -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() @@ -32,6 +32,14 @@ public function createProvider() 0.1, 72 // dpi ], + [ + [ + '_method' => 'templateInTemplateMm', + 'tmpPath' => 'templateInTemplateMm', + ], + 0.1, + 72 // dpi + ], [ [ '_method' => 'fontHandlingA', @@ -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. * diff --git a/tests/visual/FpdfTplTest/templateInTemplateMm/original/result.pdf b/tests/visual/FpdfTplTest/templateInTemplateMm/original/result.pdf new file mode 100644 index 0000000..5dd7d46 Binary files /dev/null and b/tests/visual/FpdfTplTest/templateInTemplateMm/original/result.pdf differ diff --git a/tests/visual/FpdiTest.php b/tests/visual/FpdiTest.php new file mode 100644 index 0000000..b4b2356 --- /dev/null +++ b/tests/visual/FpdiTest.php @@ -0,0 +1,175 @@ + '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); + } +} \ No newline at end of file diff --git a/tests/visual/FpdiTest/importedPageInTemplateMm1/original/result.pdf b/tests/visual/FpdiTest/importedPageInTemplateMm1/original/result.pdf new file mode 100644 index 0000000..b6bcb63 Binary files /dev/null and b/tests/visual/FpdiTest/importedPageInTemplateMm1/original/result.pdf differ diff --git a/tests/visual/FpdiTest/importedPageInTemplateMm2/original/result.pdf b/tests/visual/FpdiTest/importedPageInTemplateMm2/original/result.pdf new file mode 100644 index 0000000..6a639dd Binary files /dev/null and b/tests/visual/FpdiTest/importedPageInTemplateMm2/original/result.pdf differ diff --git a/tests/visual/FpdiTest/importedPageInTemplatePt1/original/result.pdf b/tests/visual/FpdiTest/importedPageInTemplatePt1/original/result.pdf new file mode 100644 index 0000000..3eae413 Binary files /dev/null and b/tests/visual/FpdiTest/importedPageInTemplatePt1/original/result.pdf differ diff --git a/tests/visual/FpdiTest/importedPageInTemplatePt2/original/result.pdf b/tests/visual/FpdiTest/importedPageInTemplatePt2/original/result.pdf new file mode 100644 index 0000000..e169405 Binary files /dev/null and b/tests/visual/FpdiTest/importedPageInTemplatePt2/original/result.pdf differ diff --git a/tests/visual/Tfpdf/FpdfTplTest.php b/tests/visual/Tfpdf/FpdfTplTest.php index 12257cf..8a8354a 100644 --- a/tests/visual/Tfpdf/FpdfTplTest.php +++ b/tests/visual/Tfpdf/FpdfTplTest.php @@ -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); } } diff --git a/tests/visual/Tfpdf/FpdfTplTest/templateInTemplateMm/original/result.pdf b/tests/visual/Tfpdf/FpdfTplTest/templateInTemplateMm/original/result.pdf new file mode 100644 index 0000000..f2a03ad Binary files /dev/null and b/tests/visual/Tfpdf/FpdfTplTest/templateInTemplateMm/original/result.pdf differ diff --git a/tests/visual/Tfpdf/FpdiTest.php b/tests/visual/Tfpdf/FpdiTest.php new file mode 100644 index 0000000..049f1c4 --- /dev/null +++ b/tests/visual/Tfpdf/FpdiTest.php @@ -0,0 +1,23 @@ +