OpenXML - Read, Write and Create Word documents in PHP.
PHPWord is a library written in pure PHP and providing a set of classes that allow you to write to and read from different document file formats, like Word (.docx), WordPad (.rtf), Libre/OpenOffice Writer (.odt).
No Windows operating system is needed for usage because the resulting DOCX, ODT, or RTF files can be opened by all major word processing softwares.
PHPWord is an open source project licensed under LGPL. PHPWord is unit tested to make sure that the released versions are stable.
Want to contribute? Fork us!
- Set document properties, e.g. title, subject, and creator.
- Create document sections with different settings, e.g. portrait/landscape, page size, and page numbering
- Create header and footer for each sections
- Set default font type, font size, and paragraph style
- Use UTF-8 and East Asia fonts/characters
- Define custom font styles (e.g. bold, italic, color) and paragraph styles (e.g. centered, multicolumns, spacing) either as named style or inline in text
- Insert paragraphs, either as a simple text or complex one (a text run) that contains other elements
- Insert titles (headers) and table of contents
- Insert text breaks and page breaks
- Insert and format images, either local, remote, or as page watermarks
- Insert binary OLE Objects such as Excel or Visio
- Insert and format table with customized properties for each rows (e.g. repeat as header row) and cells (e.g. background color, rowspan, colspan)
- Insert list items as bulleted, numbered, or multilevel
- Insert hyperlinks
- Create document from templates
- Use XSL 1.0 style sheets to transform main document part of OOXML template
- ... and many more features on progress
- PHP 5.3+
- PHP Zip extension
- PHP XML Parser extension
It is recommended that you install the PHPWord library through composer. To do so, add
the following lines to your composer.json
.
{
"require": {
"phpoffice/phpword": "dev-master"
}
}
We're reorganizing our documentation. Below are some of the most important things that you needed to get PHPWord creates document for you in no time.
The following is a basic example of the PHPWord library. More examples are provided in the samples folder.
$PHPWord = new PHPWord();
// Every element you want to append to the word document is placed in a section.
// To create a basic section:
$section = $PHPWord->createSection();
// After creating a section, you can append elements:
$section->addText('Hello world!');
// You can directly style your text by giving the addText function an array:
$section->addText('Hello world! I am formatted.',
array('name'=>'Tahoma', 'size'=>16, 'bold'=>true));
// If you often need the same style again you can create a user defined style
// to the word document and give the addText function the name of the style:
$PHPWord->addFontStyle('myOwnStyle',
array('name'=>'Verdana', 'size'=>14, 'color'=>'1B2232'));
$section->addText('Hello world! I am formatted by a user defined style',
'myOwnStyle');
// You can also put the appended element to local object like this:
$fontStyle = new PHPWord_Style_Font();
$fontStyle->setBold(true);
$fontStyle->setName('Verdana');
$fontStyle->setSize(22);
$myTextElement = $section->addText('Hello World!');
$myTextElement->setFontStyle($fontStyle);
// Finally, write the document:
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('helloWorld.docx');
The base length unit in Open Office XML is twip. Twip means "TWentieth of an Inch Point", i.e. 1 twip = 1/1440 inch.
You can use PHPWord helper functions to convert inches, centimeters, or points to twips.
// Paragraph with 6 points space after
$PHPWord->addParagraphStyle('My Style', array(
'spaceAfter' => PHPWord_Shared_Font::pointSizeToTwips(6))
);
$section = $PHPWord->createSection();
$sectionStyle = $section->getSettings();
// half inch left margin
$sectionStyle->setMarginLeft(PHPWord_Shared_Font::inchSizeToTwips(.5));
// 2 cm right margin
$sectionStyle->setMarginRight(PHPWord_Shared_Font::centimeterSizeToTwips(2));
Every visible element in word is placed inside of a section. To create a section, use the following code:
$section = $PHPWord->createSection($sectionSettings);
The $sectionSettings
is an optional associative array that sets the section. Example:
$sectionSettings = array(
'orientation' => 'landscape',
'marginTop' => 600,
'colsNum' => 2,
);
Below are the available settings for section:
orientation
Page orientation, i.e. 'portrait' (default) or 'landscape'marginTop
Page margin top in twipsmarginLeft
Page margin left in twipsmarginRight
Page margin right in twipsmarginBottom
Page margin bottom in twipsborderTopSize
Border top size in twipsborderTopColor
Border top colorborderLeftSize
Border left size in twipsborderLeftColor
Border left colorborderRightSize
Border right size in twipsborderRightColor
Border right colorborderBottomSize
Border bottom size in twipsborderBottomColor
Border bottom colorheaderHeight
Spacing to top of headerfooterHeight
Spacing to bottom of footercolsNum
Number of columnscolsSpace
Spacing between columnsbreakType
Section break type (nextPage, nextColumn, continuous, evenPage, oddPage)
The following two settings are automatically set by the use of the orientation
setting. You can alter them but that's not recommended.
pageSizeW
Page width in twipspageSizeH
Page height in twips
You can change a section page numbering.
$section = $PHPWord->createSection();
$section->getSettings()->setPageNumberingStart(1);
Text can be added by using addText
and createTextRun
method. addText
is used for creating simple paragraphs that only contain texts with the same style. createTextRun
is used for creating complex paragraphs that contain text with different style (some bold, other italics, etc) or other elements, e.g. images or links.
addText
sample:
$fontStyle = array('name' => 'Times New Roman', 'size' => 9);
$paragraphStyle = array('align' => 'both');
$section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);
createTextRun
sample:
$textrun = $section->createTextRun();
$textrun->addText('I am bold', array('bold' => true));
$textrun->addText('I am italic, array('italic' => true));
$textrun->addText('I am colored, array('color' => 'AACC00'));
size
text size, e.g. 20, 22,name
font name, e.g. Arialbold
text is bold, true or falseitalic
text is italic, true or falsesuperScript
text is super script, true or falsesubScript
text is sub script, true or falseunderline
text is underline, true or falsestrikethrough
text is strikethrough, true or falsecolor
text color, e.g. FF0000fgColor
fgColorline-height
text line height, e.g. 1.0, 1.5, ect...
line-height
text line height, e.g. 1.0, 1.5, ect...align
paragraph alignment, left, right or centerspaceBefore
space before ParagraphspaceAfter
space after Paragraphtabs
set of Custom Tab Stopsindent
indent by how much
The following illustrates how to create a table.
$table = $section->addTable();
$table->addRow();
$table->addCell();
You can span a cell on multiple columms.
$cell = $table->addCell(200);
$cell->getStyle()->setGridSpan(5);
You can add images easily using the following example.
$section = $PHPWord->createSection();
$section->addImage('mars.jpg');
width
width in pixelsheight
height in pixelsalign
image alignment, left, right or centermarginTop
top margin in inches, can be negativemarginLeft
left margin in inches, can be negativewrappingStyle
can be inline, square, tight, behind, infront
To add an image with attributes, consider the following example.
$section->addImage(
'mars.jpg',
array(
'width' => 100,
'height' => 100,
'marginTop' => -1,
'marginLeft' => -1,
'wrappingStyle' => 'behind'
)
);