
Originariamente inviata da
badaze
Per avere scritto un generatore di file Excel .xlsx ti posso dire che non è affatto un problema php.
Siccome sono file xml ti tocca unzipare il file odt dopo modifica e poi aprire ogni xml con il browser per vedere dove sta l’errore.
questo è il codice del mio file php che unzippa e zippa, il contenuto del file odt, e mi genera il file odt definitivo (ho controllato i file xml) sembra lo stesso.
lo puoi testare con un tuo file odt ?!
Codice PHP:
<?php
$zip = new ZipArchive;
$res = $zip->open('test.odt');
if ($res === TRUE) {
$zip->extractTo('dir_dove_scompatto');
//$source = file_get_contents('dirtmp/content.xml');
//$source = str_replace('@nome@', 'nome e cognome', $source);
//file_put_contents('dirtmp/content.xml', $source);
/* queste 3 righe sopra, sono quelle con cui faccio il replace.. ma ho fatto dei test anche semplicemente unzippando e zippando, senza modificare content.xml */
$zip->close();
$rootPath = realpath('dir_dove_scompatto');
$zip = new ZipArchive();
$zip->open('nuovoOdt.odt', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) {
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
if ( $relativePath != "" ) {
if (is_dir($file) === true) {
$zip->addEmptyDir(str_replace($source . '/', '', $relativePath . '/'));
}
else if (is_file($file) === true) {
$zip->addFromString(str_replace($source . '/', '', $relativePath), file_get_contents($file));
}
}
}
$zip->close();
}
else {
echo 'doh!';
}
?>