// New Word Document
$PHPWord = new PHPWord();
// New portrait section
$section = $PHPWord->createSection();
// Add text elements
$section->addText('Hello World!');
$section->addTextBreak(2);
// Write to Word2007 format
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save(str_replace('.php', '.docx', __FILE__));
addText($text, $styleFont = null, $styleParagraph = null)
// Write to Word2007 format
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('wordFile.docx');
// Write to OpenDocumentText format
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText');
$objWriter->save('wordFile.odt');
// Write to RTF format
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF');
$objWriter->save('wordFile.rtf');
Outputting to browser.
Content types:
- application/rtf
- application/msword
$filename = "browser-download.docx";
header('Content-Type: application/msword');
header('Content-Disposition: attachment;filename="'. $filename .'"');
header('Cache-Control: max-age=0');
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');