From 6259cc3688ff942b072650fd66e35228ba13676b Mon Sep 17 00:00:00 2001 From: Stepan Date: Sat, 28 Jun 2025 11:25:24 +0200 Subject: [PATCH] Added export recipe to pdf endpoint --- .../Controllers/ExportPdfController.php | 46 ++++++++ apps/Recipes/Models/RecipeModel.php | 2 +- apps/Recipes/Templates/export-pdf.php | 1 + apps/Recipes/Utils/RecipePDF.php | 111 ++++++++++++++++++ apps/Recipes/urls.php | 1 + composer.json | 3 +- composer.lock | 48 +++++++- 7 files changed, 209 insertions(+), 3 deletions(-) create mode 100644 apps/Recipes/Controllers/ExportPdfController.php create mode 100644 apps/Recipes/Templates/export-pdf.php create mode 100644 apps/Recipes/Utils/RecipePDF.php diff --git a/apps/Recipes/Controllers/ExportPdfController.php b/apps/Recipes/Controllers/ExportPdfController.php new file mode 100644 index 0000000..1d20dbf --- /dev/null +++ b/apps/Recipes/Controllers/ExportPdfController.php @@ -0,0 +1,46 @@ +__model)) + return $this->__model; + + $this->__model = RecipeModel::get( + array( + [ + 'name' => 'obj.id', + 'type' => '=', + 'value' => $this->url_context['url_1'] + ] + ) + ); + if (empty($this->__model)) + throw new HttpExceptions\NotFound404('Recipe not found'); + + return $this->__model; + } + + public function get_context_data() + { + $context = parent::get_context_data(); + $recipe = $this->get_model(); + + $fpdf = new RecipePDF($recipe); + $fpdf->PrintRecipe(); + + $context['fpdf'] = $fpdf; + return $context; + } +} diff --git a/apps/Recipes/Models/RecipeModel.php b/apps/Recipes/Models/RecipeModel.php index 9d69626..dd4dd69 100644 --- a/apps/Recipes/Models/RecipeModel.php +++ b/apps/Recipes/Models/RecipeModel.php @@ -27,7 +27,7 @@ class RecipeModel extends BaseModel 'field' => [ 'tb1.name AS category_name' ], - 'join_table' => 'ingredients tb1 ON tb1.id = obj.category_id' + 'join_table' => 'categories tb1 ON tb1.id = obj.category_id' ], [ 'field' => [ diff --git a/apps/Recipes/Templates/export-pdf.php b/apps/Recipes/Templates/export-pdf.php new file mode 100644 index 0000000..42d52f9 --- /dev/null +++ b/apps/Recipes/Templates/export-pdf.php @@ -0,0 +1 @@ +Output('I', 'recipe.pdf') ?> \ No newline at end of file diff --git a/apps/Recipes/Utils/RecipePDF.php b/apps/Recipes/Utils/RecipePDF.php new file mode 100644 index 0000000..7943851 --- /dev/null +++ b/apps/Recipes/Utils/RecipePDF.php @@ -0,0 +1,111 @@ +recipe = $recipe; + } + function Header() + { + $recipe = $this->recipe; + + // Background + $this->SetTextColor(0, 139, 112); + + $this->SetFont('Arial', 'B', 16); + $this->Image(BASE_PATH . '/assets/images/fridgeLogo.png', 10, 6, 20); + $this->Cell(80); + $this->Cell(30, 10, $recipe->field_title, 0, 0, 'C'); + $this->Ln(8); + + $this->SetFont('Arial', '', 12); + $this->SetTextColor(0, 0, 0); + $this->Cell(80); + $this->Cell(30, 10, $recipe->category_name, 0, 0, 'C'); + $this->Ln(20); + } + function Footer() + { + // Position at 1.5 cm from bottom + $this->SetY(-15); + // Arial italic 8 + $this->SetFont('Arial', 'I', 8); + // Text color in gray + $this->SetTextColor(128); + // Page number + $this->Cell(0, 3, 'Page ' . $this->PageNo(), 0, 0, 'C'); + $this->Ln(); + $this->Cell(0, 10, 'LycoReco', 0, 0, 'C'); + } + + function RecipeTable() + { + $header = array('Ingredient', 'Count'); + + $ingredients = IngredientInRecipeModel::filter(array( + [ + 'name' => 'obj.recipe_id', + 'type' => '=', + 'value' => $this->recipe->get_id() + ] + )); + + // Colors, line width and bold font + $this->SetFillColor(0, 139, 122); + $this->SetTextColor(255); + $this->SetDrawColor(179, 179, 179); + $this->SetLineWidth(.3); + $this->SetFont('', 'B'); + // Header + $w = array(80, 35); + $this->Cell(35); + for ($i = 0; $i < count($header); $i++) + $this->Cell($w[$i], 7, $header[$i], 1, 0, 'C', true); + $this->Ln(); + + // Body + $this->SetFillColor(224,235,255); + $this->SetTextColor(0); + $this->SetFont(''); + foreach ($ingredients as $ing) { + $this->Cell(35); + $this->Cell($w[0], 6, $ing->ingredient_name, 'LR', 0, 'L', false); + $this->Cell($w[1], 6, $ing->get_count(), 'LR', 0, 'L', false); + $this->Ln(); + } + $this->Cell(35); + $this->Cell(array_sum($w),0,'','T'); + $this->Ln(10); + } + + function RecipeContent() + { + $recipe = $this->recipe; + + $this->SetFont('Times', '', 14); + $this->SetTextColor(0); + $this->MultiCell(0, 5, txt: $recipe->field_instruction); + $this->Ln(); + $this->SetFont('', 'I'); + $this->Cell(0, 5, '(Bon appetit!)'); + } + + + public function PrintRecipe() + { + $this->SetTitle($this->recipe->field_title); + $this->AddPage(); + $this->RecipeTable(); + $this->RecipeContent(); + } +} diff --git a/apps/Recipes/urls.php b/apps/Recipes/urls.php index 1445dc8..0b81f59 100644 --- a/apps/Recipes/urls.php +++ b/apps/Recipes/urls.php @@ -5,5 +5,6 @@ use Lycoreco\Includes\Routing\Path; $recipes_urls = [ new Path('/recipe/[:int]', new Controllers\SingleRecipeController(), 'single'), + new Path('/recipe/[:int]/export-pdf', new Controllers\ExportPdfController(), 'export-pdf'), new Path('/catalog', new Controllers\CatalogController(), 'catalog'), ]; diff --git a/composer.json b/composer.json index b6b3fe3..db4b3e6 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ } }, "require": { - "phpmailer/phpmailer": "^6.10" + "phpmailer/phpmailer": "^6.10", + "setasign/fpdf": "^1.8" } } diff --git a/composer.lock b/composer.lock index 462c06e..84bf483 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "a475eab76e37559e0cc7396ccf30c49e", + "content-hash": "a6db4a4cd2450cf3fbd46a573661b495", "packages": [ { "name": "phpmailer/phpmailer", @@ -86,6 +86,52 @@ } ], "time": "2025-04-24T15:19:31+00:00" + }, + { + "name": "setasign/fpdf", + "version": "1.8.6", + "source": { + "type": "git", + "url": "https://github.com/Setasign/FPDF.git", + "reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Setasign/FPDF/zipball/0838e0ee4925716fcbbc50ad9e1799b5edfae0a0", + "reference": "0838e0ee4925716fcbbc50ad9e1799b5edfae0a0", + "shasum": "" + }, + "require": { + "ext-gd": "*", + "ext-zlib": "*" + }, + "type": "library", + "autoload": { + "classmap": [ + "fpdf.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Olivier Plathey", + "email": "oliver@fpdf.org", + "homepage": "http://fpdf.org/" + } + ], + "description": "FPDF is a PHP class which allows to generate PDF files with pure PHP. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.", + "homepage": "http://www.fpdf.org", + "keywords": [ + "fpdf", + "pdf" + ], + "support": { + "source": "https://github.com/Setasign/FPDF/tree/1.8.6" + }, + "time": "2023-06-26T14:44:25+00:00" } ], "packages-dev": [], -- 2.34.1