Files
nutzwerte_fi4/app/Actions/Generate/PdfGenerator.php
T
2026-02-26 09:59:49 +01:00

187 lines
6.4 KiB
PHP

<?php
namespace App\Actions\Generate;
use App\Actions\Generate\Pdf\Deckblatt;
use App\Actions\Generate\Pdf\DigitaleSignatur;
use App\Actions\Generate\Pdf\Inhalt;
use App\Actions\Generate\Pdf\Platzhalter;
use App\Actions\Generate\Pdf\Ueberschrift;
use App\Actions\Generate\Tabellen\Allgemeinflaechen;
use App\Actions\Generate\Tabellen\Nutzwertberechnung;
use App\Actions\Generate\Tabellen\Paragraph6;
use App\Actions\Generate\Tabellen\Zusammenstellung;
use App\Actions\Generate\Tabellen\ZuUndAbschlaege;
use App\Actions\Generate\Utilities\Format;
use App\Models\Auftraggeber;
use App\Models\Gutachter;
use App\Models\Projekte;
use App\Models\ProjektInhaltsverzeichnis;
use Filament\Forms\Components\RichEditor\RichContentRenderer;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
use Mpdf\HTMLParserMode;
use Mpdf\Mpdf;
class PdfGenerator
{
public static $projekt;
public static $gutachter;
public static $auftraggeber;
public static $zusammenstellung = [];
public static function make($projekte_id, $part)
{
self::$projekt = Projekte::where('id', $projekte_id)->first();
self::$gutachter = Gutachter::where('id', self::$projekt->gutachter_id)->first();
self::$auftraggeber = Auftraggeber::where('id', self::$projekt->auftraggeber_id)->first();
$headers = [
'Content-Type: application/pdf',
'Cache-Control: max-age=0',
'Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT',
'Cache-Control: cache, must-revalidate',
];
$filepath = Storage::disk('public')->path("/download/$projekte_id/");
$filename = \ucfirst($part).' '.preg_replace("'[^0-9a-zA-Z]'", '', self::$projekt->interner_name).'.pdf';
if (! File::isDirectory($filepath)) {
File::makeDirectory($filepath, 0777, true, true);
}
$file = $filepath.$filename;
ob_start();
$mpdf = new Mpdf(
[
'debug' => true,
'use_kwt' => true,
'mode' => 'utf-8',
'format' => 'A4-P',
'margin_left' => 10,
'margin_right' => 10,
'margin_top' => 12,
'margin_bottom' => 20,
'margin_header' => 0,
'margin_footer' => 0,
]
);
$stylesheet = file_get_contents(__DIR__.'/../../../resources/css/pdf-download.css');
$colors = [];
if (self::$projekt?->projekt_farben_verwenden) {
$colors["'###BG_DECKBLATT###'"] = self::$projekt->deckblatt_bg;
$colors["'###BG_HEADER###'"] = self::$projekt->schmuckfarbe_header;
$colors["'###FARBE_RAHMEN###'"] = self::$projekt->farbe_borders;
} else {
$colors["'###BG_DECKBLATT###'"] = self::$gutachter->deckblatt_bg;
$colors["'###BG_HEADER###'"] = self::$gutachter->schmuckfarbe_header;
$colors["'###FARBE_RAHMEN###'"] = self::$gutachter->farbe_borders;
}
$stylesheet = Platzhalter::replace($colors, $stylesheet);
// dd($stylesheet);
$mpdf->WriteHTML($stylesheet, HTMLParserMode::HEADER_CSS);
$mpdf->SetTitle($filename);
$mpdf->SetCreator('nutzwerte.at');
$mpdf->SetAuthor(self::$gutachter->title.' '.self::$gutachter->gender.' '.self::$gutachter->name);
$inhaltesverzeichnis = ProjektInhaltsverzeichnis::where('projekte_id', $projekte_id)->where('part', $part)->orderBy('sort')->get();
$platzhalter = Platzhalter::load($projekte_id);
foreach ($inhaltesverzeichnis as $inhalt) {
if ($inhalt->neue_seite) {
if ($part == 'nutzwerte') {
$mpdf->AddPage('P', '', $inhalt->seite);
} else {
$mpdf->AddPage();
}
}
$html = '';
$html = Ueberschrift::load($inhalt);
if ($inhalt->textblock) {
$textblock = Platzhalter::replace($platzhalter, $inhalt->textblock);
$textblock = Format::fix_str($textblock);
if ($inhalt->inhaltselement_id != '9') {
$html .= $textblock;
}
}
if ($inhalt->textblock2) {
$textblock2 = Platzhalter::replace($platzhalter, $inhalt->textblock2);
$textblock2 = Format::fix_str($textblock2);
}
switch ($inhalt->inhaltselement_id) {
case '1':
$html .= Inhalt::load($projekte_id);
break;
case '3':
$html .= Paragraph6::load($projekte_id);
break;
case '4':
$html .= Allgemeinflaechen::load($projekte_id);
break;
case '5':
$html .= ZuUndAbschlaege::load($projekte_id);
break;
case '6':
$html .= Nutzwertberechnung::load($projekte_id);
break;
case '7':
// $html .= Zusammenstellung::load($projekte_id);
break;
case '8':
// AUFTRAGGEBER
$str = RichContentRenderer::make(self::$auftraggeber->referenz_formatting)->toHtml();
$html .= Format::fix_str($str);
break;
case '9':
$html .= Deckblatt::load($inhalt, $textblock, $textblock2);
break;
case '10':
$html .= DigitaleSignatur::load($projekte_id);
break;
}
if ($inhalt->textblock2 and $inhalt->inhaltselement_id != 9) {
$html .= $textblock2;
}
if ($inhalt->seite != 0) {
$mpdf->SetHTMLFooter('<div class="center">Seite {PAGENO} <br><br></div>');
}
if ($html) {
$mpdf->WriteHTML($html, HTMLParserMode::HTML_BODY);
}
}
if ($part == 'standard') {
$inhaltesverzeichnis = ProjektInhaltsverzeichnis::where('projekte_id', $projekte_id)->where('part', 'nutzwerte')->orderBy('sort')->first();
$inhaltesverzeichnis->seite = $mpdf->page + 1;
$inhaltesverzeichnis->save();
}
ob_clean();
$mpdf->Output($file, 'F');
return Response::download($file, $filename, $headers);
}
}