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();
?> |
Partager