Files
nutzwerte_fi4/app/Actions/Generate/Pdf.php
T
2025-11-03 06:12:59 +01:00

153 lines
5.2 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\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 App\Models\ProjektPlatzhalter;
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 Pdf
{
public static $projekt;
public static $gutachter;
public static $auftraggeber;
public static function make($projekte_id, $type, $mode)
{
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($type).' '.preg_replace("'[^0-9a-zA-Z]'", '', self::$projekt->interner_name).'.pdf';
// if ($type == 'nutzwerte') {
// if ($mode == 'new') {
// Projekte::where('id', $projekte_id)->update(['pdf_nutzwerte_name' => '', 'pdf_nutzwerte_status' => 1]);
// } else {
// return Response::download($file, $filename, $headers);
// }
// }
if (! File::isDirectory($filepath)) {
File::makeDirectory($filepath, 0777, true, true);
}
$file = $filepath.$filename;
ob_start();
$mpdf = new Mpdf(
[
'debug' => 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');
$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', $type)->orderBy('sort')->get();
$platzhalter = ProjektPlatzhalter::where('projekte_id', $projekte_id)->pluck('platzhaltertext', 'platzhalter')->toArray();
foreach ($inhaltesverzeichnis as $inhalt) {
if ($inhalt->neue_seite) {
$mpdf->AddPage();
}
if ($inhalt->textblock) {
$textblock = Platzhalter::replace($platzhalter, $inhalt->textblock);
$textblock = Format::fix_str($textblock);
}
$html = Ueberschrift::create($inhalt);
if ($inhalt->name == 'DECKBLATT') {
$html .= Deckblatt::create($textblock);
} elseif ($inhalt->name == 'INHALTSVERZEICHNIS') {
$html .= Inhalt::create($projekte_id);
} else {
if ($inhalt->textblock) {
$html .= $textblock;
}
switch ($inhalt->tabellen_id) {
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 '7':
$html .= Zusammenstellung::load($projekte_id);
break;
case '8':
$str = RichContentRenderer::make(self::$auftraggeber->referenz_formatting)->toHtml();
$html .= Format::fix_str($str);
break;
case '11':
$html .= DigitaleSignatur::load($projekte_id);
break;
}
}
if ($inhalt->seitenzahl_anzeige == 1) {
$mpdf->SetHTMLFooter('<div class="center">Seite {PAGENO}<br><br></div>');
}
if ($html) {
$mpdf->WriteHTML($html, HTMLParserMode::HTML_BODY);
}
}
ob_clean();
$mpdf->Output($file, 'F');
return Response::download($file, $filename, $headers);
}
}