Bonjour tout le monde,

J'ai un petit problème à vous soumettre. Je me lance à la conquête de OpenXML en commençant par créer un document docx. Pour ca je me suis appuyé sur l'article dans la faq de Guillaume Rossolini (qui est très bien faite merci beaucoup).

Mon code correspond donc exactement à ca :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
 
$types = new DOMDocument('1.0', 'utf-8');
 
$XMLTypes = $types->createElement('Types');
$XMLTypes->setAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/content-types');
$types->appendChild($XMLTypes);
 
$XMLType = $types->createElement('Default');
$XMLType->setAttribute('Extension', 'rels');
$XMLType->setAttribute('ContentType', 'application/vnd.openxmlformats-package.relationships+xml');
$XMLTypes->appendChild($XMLType);
 
$XMLType = $types->createElement('Default');
$XMLType->setAttribute('Extension', 'xml');
$XMLType->setAttribute('ContentType', 'application/xml');
$XMLTypes->appendChild($XMLType);
 
$XMLType = $types->createElement('Override');
$XMLType->setAttribute('PartName', '/word/document.xml');
$XMLType->setAttribute('ContentType', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml');
$XMLTypes->appendChild($XMLType);
 
 
$rels = new DOMDocument('1.0', 'utf-8');
 
$XMLRels = $rels->createElement('Relationships');
$XMLRels->setAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
$rels->appendChild($XMLRels);
 
$XMLRel = $rels->createElement('Relationship');
$XMLRel->setAttribute('Id', 'rId1');
$XMLRel->setAttribute('Type', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument');
$XMLRel->setAttribute('Target', 'word/document.xml');
$XMLRels->appendChild($XMLRel);
 
 
$word = new DOMDocument('1.0', 'utf-8');
 
$XMLDocument = $word->createElement('w:document');
$XMLDocument->setAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main');
$word->appendChild($XMLDocument);
 
$XMLBody = $word->createElement('w:body');
$XMLDocument->appendChild($XMLBody);
 
$XMLParagraph = $word->createElement('w:p');
$XMLBody->appendChild($XMLParagraph);
 
$XMLRun = $word->createElement('w:r');
$XMLParagraph->appendChild($XMLRun);
 
$XMLText = $word->createElement('w:t', utf8_encode("Hello world!"));
$XMLRun->appendChild($XMLText);
 
 
$document = new ZipArchive();
$document->open('hello-world.docx', ZIPARCHIVE::CREATE);
 
$document->addFromString('[Content_Types].xml', $types->saveXML());
$document->addFromString('_rels/.rels', $rels->saveXML());
$document->addFromString('word/document.xml', $word->saveXML());
 
$document->close();
 
?>
J'ai donc juste changé cette ligne là :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
$document->open('hello-world.docx', ZIPARCHIVE::CREATE);
par rapport au code d'origine. (d'ailleur petite parenthèse je pense qu'il serait pertinent de mettre une petite astérisque dans la FAQ pour expliquer que si le document n'existe pas déja, il faut remplacer le ZIPARCHIVE::OVERWRITE par ZIPARCHIVE::CREATE, ca pourra faire gagner un peu de temps au novice de mon genre )

Le script génère bien un fichier hello-world.docx, mais impossible de l'ouvrir, j'ai une erreur qui me dit que j'ai un problème avec le 'contents'.

Quand je renomme mon docx en zip et que j'essaie de décompresser le dossier, je n'arrive pas à décompresser le fichier [Content_Types].xml. J'ai un échec CRC.
Ma config: apache 2.0.63; php 5.2.8

Dans mon php.ini, mes extension php_zip.dll, xsl.dll sont bien activées.

Si quelqu'un a une idée elle est la bienvenue.

Merci par avance,

Vincent