Index: /views/mods/mod_rechnungen/multi_invoice_pdf.phtml
===================================================================
--- /views/mods/mod_rechnungen/multi_invoice_pdf.phtml	(revision 8504)
+++ /views/mods/mod_rechnungen/multi_invoice_pdf.phtml	(revision 8505)
@@ -35,9 +35,15 @@
 	$prod_break					= $this->get_option('wpsg_rechnungen_pdfperpage');
 
-    /*
-    $arCalculation = $this->view['basket']['arCalculation'];
-    $data = $this->view['multi_data']['orders'][0]['data'];
-    $sum = $this->view['multi_data']['orders'][0]['sum'];
-	*/
+    // Unterkante nutzbarer Bereich (297 - Margin)
+    $PAGE_LIMIT_MM = 290;
+    // bestehender Abstand nach einer Bestellung
+    $ORDER_SPACING  = 30;
+
+
+/*
+$arCalculation = $this->view['basket']['arCalculation'];
+$data = $this->view['multi_data']['orders'][0]['data'];
+$sum = $this->view['multi_data']['orders'][0]['sum'];
+*/
 
     $arCalculation = array_values($this->view['multi_data']['order_data'])[0]['arCalculation'];
@@ -216,5 +222,139 @@
 		
 	}
-	 
+
+    // Hoehe einer kompletten Bestellung abschaetzen
+    $estimateOrderBlockHeight = function($shop, $pdf, $oOrder, $arCalculation, $bTaxCol) use ($ORDER_SPACING) {
+
+        // Spaltenbreite fÃŒr den Produkttext wie im Rendering
+        $produkt_text_cell_width = $bTaxCol ? 87 : 102;
+
+        // HÃ¶he: Kopf der Bestellung (ZÃ€hler + 3 Meta-Zeilen rechts)
+        // â1 Bestellungâ, Bestelldatum, Zahlungsbedingungen, Best. Nr.
+        $h = 12;        // Kopf
+        $h += 8;        // Tabellenkopf (Nr/Name/MwSt/Menge/Einzelpreis/Gesamtpreis)
+
+        foreach ($oOrder->getOrderProducts() as $oOrderProduct) {
+            $product_id = $oOrderProduct->getProductId();
+
+            // Produktname/Detailname wie im Code
+            $produkt_text = ($shop->get_option('wpsg_rechnungen_pdetailname') == '1')
+                ? $shop->getProductName($product_id, true)
+                : $shop->getProductName($product_id, false);
+
+            // Ãbersetzung
+            if ($shop->isOtherLang()) {
+                $trans_db = $shop->db->fetchRow(
+                    "SELECT * FROM `".WPSG_TBL_PRODUCTS."` WHERE `lang_parent` = '".wpsg_q($product_id)."' AND `lang_code` = '".wpsg_q($shop->getCurrentLanguageCode())."'"
+                );
+                if (is_array($trans_db) && sizeof($trans_db) > 0) {
+                    $produkt_text = $trans_db['name'];
+                }
+            }
+
+            // Artikelnummer zusÃ€tzlich?
+            if ($shop->get_option('wpsg_mod_rechnungen_anr') === '2') {
+                $anr = $shop->getProductAnr($oOrderProduct->getProductKey());
+                $produkt_text .= "\n".wpsg_translate(__('Artikelnummer: #1#', 'wpsg'), $anr);
+            }
+
+            // Produktvariablen optional anhÃ€ngen (wir rechnen ZeilenhÃ¶he mit)
+            if ($shop->hasMod('wpsg_mod_productvars') && $shop->get_option("wpsg_mod_rechnungen_showpv") == "1") {
+                $arPV = $shop->callMod('wpsg_mod_productvars', 'getAllProductVarValues', array($oOrder->getId(), $oOrderProduct->getProductIndex()));
+                if (wpsg_isSizedArray($arPV)) {
+                    foreach ($arPV as $pv) {
+                        if ($pv['value'] != '') {
+                            $produkt_text .= "\n".$pv['name'].': '.$pv['value'];
+                        }
+                    }
+                }
+            }
+
+            // Name/Text-Zeilen (MultiCell-SchÃ€tzung)
+            $txt    = str_replace(["\r\n", "\r"], "\n", (string)$produkt_text);
+            $manual = substr_count($txt, "\n") + 1;
+            $wrap   = max(1, (int)ceil($pdf->GetStringWidth(str_replace("\n", ' ', $txt)) / $produkt_text_cell_width));
+            $name_lines = max($manual, $wrap);
+
+            $rowH = 5 * $name_lines + 3;
+
+            // Kurzbeschreibung?
+            if ($shop->get_option("wpsg_rechnungen_pbeschreibung") == "1") {
+                $desc = trim(strip_tags($oOrderProduct->getProduct()->getShortDescription()));
+                if ($shop->isOtherLang() && isset($trans_db['beschreibung'])) {
+                    $desc = trim(strip_tags($trans_db['beschreibung']));
+                }
+                if ($desc !== '') {
+                    $descW = $pdf->GetStringWidth($desc);
+                    $rowH += 5 * max(1, (int)ceil($descW / $produkt_text_cell_width));
+                }
+            }
+
+            // Attribute?
+            if ($shop->get_option('wpsg_rechnungen_produktattribute') == '1') {
+                $attributeInfo = $shop->callMod('wpsg_mod_produktattribute', 'getProductAttributeByProductId', array($shop->getProduktId($oOrderProduct->getId())));
+                if (is_array($attributeInfo)) {
+                    foreach ($attributeInfo as $pa) {
+                        $pa_txt = $pa['name'].': '.$pa['value'];
+                        $paW = $pdf->GetStringWidth($pa_txt);
+                        $rowH += 5 * max(1, (int)ceil($paW / $produkt_text_cell_width));
+                    }
+                }
+            }
+
+            // Variante?
+            if (preg_match('/pv_(.*)/', $oOrderProduct->getProductKey())) {
+                $variInfo = $shop->callMod('wpsg_mod_productvariants', 'getVariantenInfoArray', array($oOrderProduct->getProductKey()));
+                $vtxt = wpsg_translate(__('Variante: #1#', 'wpsg'), $variInfo['key']);
+                $vW = $pdf->GetStringWidth($vtxt);
+                $rowH += 5 * max(1, (int)ceil($vW / $produkt_text_cell_width));
+            }
+
+            // Lieferdatum (bei mehreren Lieferscheinen)
+            if ($shop->hasMod('wpsg_mod_deliverynote')) {
+                $arDN_order = $shop->callMod('wpsg_mod_deliverynote', 'loadDeliveryNotesFromOrder', array($oOrder->getId()));
+                if (sizeof($arDN_order) > 1) {
+                    // eine Zeile
+                    $rowH += 5;
+                }
+            }
+
+            $h += $rowH;
+        }
+
+        // Gutschein(e)
+        if (!empty($arCalculation['voucher']) && is_array($arCalculation['voucher'])) {
+            $h += 8 * count($arCalculation['voucher']);
+        }
+
+        // Versandkosten
+        if (!empty($arCalculation['sum']['shipping_brutto']) && $arCalculation['sum']['shipping_brutto'] > 0) {
+            $h += 8;
+        }
+
+        // Zahlungskosten
+        $cost_payment = ((wpsg_ShopController::getShop()->getFrontendTaxview() === WPSG_BRUTTO)
+                        ? ($arCalculation['sum']['payment_brutto'] ?? 0)
+                        : ($arCalculation['sum']['payment_netto'] ?? 0));
+        if ($cost_payment > 0) { $h += 8; }
+
+        // Rabatt
+        if (!empty($arCalculation['sum']['discount_brutto']) && $arCalculation['sum']['discount_brutto'] < 0) {
+            $h += 8;
+        }
+
+        // Wertgutscheine (coupon)
+        if (!empty($arCalculation['coupon']) && is_array($arCalculation['coupon'])) {
+            $h += 8 * count($arCalculation['coupon']);
+        }
+
+        // âBestellbetrag âŠâ Block (Text + Zahl)
+        $h += 10;
+
+        // Abstand zur nÃ€chsten Bestellung
+        $h +=  $ORDER_SPACING?(int)$ORDER_SPACING:30;
+
+        return $h;
+    };
+
     // FOREACH ORDER START
     foreach ($this->view['multi_data']['order_data'] as $order_data) {
@@ -225,5 +365,14 @@
 		/** @var \wpsg\wpsg_calculation $arCalculation */
 		$arCalculation = $order_data['arCalculation'];
-		
+
+        $currentTop = $prod_top + $sum_multi_offset;
+        $need = $estimateOrderBlockHeight($this, $pdf, $oOrder, $arCalculation, $bTaxCol);
+
+        if (($currentTop + $need) > $PAGE_LIMIT_MM) {
+            AddRechnungPage($this, $pdf);
+            $sum_multi_offset = 0;           // Bestellung startet oben auf der neuen Seite
+            $currentTop = $prod_top;         // (nur zur VollstÃ€ndigkeit)
+        }
+
         // Sammelrechnung ZÃ€hler
         $pdf->SetFont('Arial', 'B', 12);
@@ -480,5 +629,5 @@
                 
             }
-            
+
         } // produkte
 	
