request = $request; $this->tempDirectory = Tenant::getTempDirectory(); } public function execute() : CreateInvoiceReceiptResponse { $response = new CreateInvoiceReceiptResponse(); $filename = $this->tempDirectory . $this->request->invoice->invoice_number . '.pdf'; if (!Storage::exists($this->tempDirectory)) { Storage::makeDirectory(Tenant::getTempDirectory() . '/' . $this->tempDirectory); } $receipt = $this->request->invoice->document_filename; if ($receipt === null) { Storage::put( $filename, $this->createPdf( $this->createHtml(), 'portrait', $filename, false ) ); $response->fileName = $filename; return $response; } $token = (string)rand(1000000000, 9999999999); $pdf_data = $this->createPdf( $this->createHtml(), 'portrait', $filename, false ); $tmpFileName = $this->tempDirectory . 'tmp-' . $token . '.pdf'; Storage::put($tmpFileName, $pdf_data); try { $merger = new PdfMergeProvider(); $merger ->add( storage_path('app/private/' . $tmpFileName )) ->add(storage_path('app/private/' . $receipt)) ->merge(storage_path('app/private/' .$filename) ); $response->fileName = $filename; } catch ( \Exception $e ) { $zip = new ZipArchive(); $zip->open( storage_path('app/private/' .$filename) . '.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE ); $zip->addFile(storage_path('app/private/' . $tmpFileName ), 'antrag.pdf'); $zip->addFile(storage_path('app/private/' . $receipt), 'beleg.pdf'); $zip->close(); $response->fileName = $filename . '.zip'; } finally { Storage::delete($tmpFileName); } return $response; } private function createHtml() : string { $invoiceReadable = new InvoiceResource($this->request->invoice)->toArray(); $travelPartTemplate = <<
| Abrechnungsnummer: | %2\$s |
| Name: | %3\$s |
| E-Mail: | %4\$s |
| Telefon: | %5\$s |
| Name der Kostenstelle: | %6\$s |
| Zahlungsgrund: | %7\$s |
| Wird der Betrag gespendet: | %8\$s |
| Gesamtbetrag: | %10\$s |
| Beleg digital eingereicht am: | %12\$s |
| Beleg akzeptiert am: | %13\$s |
| Beleg akzeptiert von: | %14\$s |
' . $this->request->invoice->changes . '
' : ''; return sprintf( $content, $invoiceReadable['invoiceTypeShort'], $invoiceReadable['invoiceNumber'], $invoiceReadable['contactName'], $invoiceReadable['contactEmail'], $invoiceReadable['contactPhone'], $invoiceReadable['costUnitName'], $invoiceReadable['invoiceType'], $invoiceReadable['donationText'], $paymentType, $invoiceReadable['amount'], $paymentInformation, $invoiceReadable['createdAt'], $invoiceReadable['approvedAt'], $invoiceReadable['approvedBy'], $changes ); } private function createPdf( string $htmlfile, string $orientation, string $filename, bool $download = true ) { $dompdf = new Dompdf(); $dompdf->loadHtml( $htmlfile, 'UTF-8' ); $dompdf->setPaper( 'A4', $orientation ); $dompdf->render(); if ( ! $download ) { return $dompdf->output(); } $dompdf->stream( $filename ); } }