Skip to content

Instantly share code, notes, and snippets.

@r-sal
Created December 18, 2012 02:39
Show Gist options
  • Save r-sal/4324524 to your computer and use it in GitHub Desktop.
Save r-sal/4324524 to your computer and use it in GitHub Desktop.
PHPWord Snippets

PHPWord Snippets

// 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)

File Formats

// 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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment