open($templateOdt) !== TRUE) { die("Fehler beim Öffnen von $templateOdt\n"); } $zip->extractTo($tmpDir); $zip->close(); // 2. Bild nach Pictures kopieren if (!is_dir("$tmpDir/Pictures")) { mkdir("$tmpDir/Pictures"); } copy($imageFile, "$tmpDir/Pictures/qrcode.png"); // 3. manifest.xml aktualisieren (WICHTIG!) $manifestFile = "$tmpDir/META-INF/manifest.xml"; $manifest = file_get_contents($manifestFile); // Prüfen ob Eintrag schon existiert if (strpos($manifest, 'Pictures/qrcode.png') === false) { $newEntry = ''; $manifest = str_replace('', $newEntry . "\n", $manifest); file_put_contents($manifestFile, $manifest); } // 4. content.xml laden $contentFile = "$tmpDir/content.xml"; $content = file_get_contents($contentFile); // 5. Bild-XML (ODF-konform) $imageXml = '' . '' . ''; // {qrcode} ersetzen $content = str_replace('{qrcode}', $imageXml, $content); file_put_contents($contentFile, $content); // 6. Neue ODT bauen $zip = new ZipArchive(); if ($zip->open($outputOdt, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) { die("Fehler beim Erstellen von $outputOdt\n"); } // 6a. mimetype MUSS zuerst & unkomprimiert $zip->addFile("$tmpDir/mimetype", "mimetype"); $zip->setCompressionName("mimetype", ZipArchive::CM_STORE); // 6b. Restliche Dateien rekursiv hinzufügen $files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($tmpDir, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::LEAVES_ONLY ); foreach ($files as $file) { if (!$file->isFile()) continue; $filePath = $file->getRealPath(); $relativePath = substr($filePath, strlen($tmpDir) + 1); // mimetype überspringen (schon hinzugefügt) if ($relativePath === 'mimetype') continue; // Backslashes zu Forward-Slashes (für Windows-Kompatibilität) $relativePath = str_replace('\\', '/', $relativePath); $zip->addFile($filePath, $relativePath); } $zip->close(); // 7. Cleanup function deleteDir($dir) { if (!is_dir($dir)) return; foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; $path = "$dir/$item"; is_dir($path) ? deleteDir($path) : unlink($path); } rmdir($dir); } deleteDir($tmpDir); echo "Fertig: $outputOdt\n";