|
| 1 | +## PHPWord Settings |
| 2 | + |
| 3 | +The ``PhpOffice\PhpWord\Settings`` class provides some options that will |
| 4 | +affect the behavior of PHPWord. Below are the options. |
| 5 | + |
| 6 | +### XML Writer compatibility |
| 7 | + |
| 8 | +This option sets [XMLWriter::setIndent](http://www.php.net/manual/en/function.xmlwriter-set-indent.php) and [XMLWriter::setIndentString](http://www.php.net/manual/en/function.xmlwriter-set-indent-string.php>). The default value of this option is ``true`` (compatible), which is [required for OpenOffice](https://github.com/PHPOffice/PHPWord/issues/103) to render OOXML document correctly. You can set this option to ``false`` during development to make the resulting XML file easier to read. |
| 9 | + |
| 10 | +``` php |
| 11 | +<?php |
| 12 | + |
| 13 | +\PhpOffice\PhpWord\Settings::setCompatibility(false); |
| 14 | +``` |
| 15 | + |
| 16 | +### Zip class |
| 17 | + |
| 18 | +By default, PHPWord uses [Zip extension](http://php.net/manual/en/book.zip.php) to deal with ZIP compressed archives and files inside them. If you can't have Zip extension installed on your server, you can use pure PHP library alternative, `[PclZip](http://www.phpconcept.net/pclzip/)``, which is included in PHPWord. |
| 19 | + |
| 20 | +``` php |
| 21 | +<?php |
| 22 | + |
| 23 | +\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP); |
| 24 | +``` |
| 25 | + |
| 26 | +### Output escaping |
| 27 | + |
| 28 | +Writing documents of some formats, especially XML-based, requires correct output escaping. |
| 29 | +Without it your document may become broken when you put special characters like ampersand, quotes, and others in it. |
| 30 | + |
| 31 | +Escaping can be performed in two ways: outside of the library by a software developer and inside of the library by built-in mechanism. |
| 32 | +By default, the built-in mechanism is disabled for backward compatibility with versions prior to v0.13.0. |
| 33 | +To turn it on set ``outputEscapingEnabled`` option to ``true`` in your PHPWord configuration file or use the following instruction at runtime: |
| 34 | + |
| 35 | +``` php |
| 36 | +<?php |
| 37 | + |
| 38 | +\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true); |
| 39 | +``` |
| 40 | + |
| 41 | +### Default Paper |
| 42 | + |
| 43 | +By default, all sections of the document will print on A4 paper. |
| 44 | +You can alter the default paper by using the following function: |
| 45 | + |
| 46 | +``` php |
| 47 | +<?php |
| 48 | + |
| 49 | +\PhpOffice\PhpWord\Settings::setDefaultPaper('Letter'); |
| 50 | +``` |
| 51 | + |
| 52 | +### Default font |
| 53 | + |
| 54 | +By default, every text appears in Arial 10 point in the color black (000000). |
| 55 | +You can alter the default font by using the following functions: |
| 56 | + |
| 57 | +``` php |
| 58 | +<?php |
| 59 | + |
| 60 | +$phpWord->setDefaultFontName('Times New Roman'); |
| 61 | +$phpWord->setDefaultFontColor('FF0000'); |
| 62 | +$phpWord->setDefaultFontSize(12); |
| 63 | +``` |
| 64 | + |
| 65 | +Or you can specify Asian Font |
| 66 | + |
| 67 | +``` php |
| 68 | +<?php |
| 69 | + |
| 70 | +$phpWord->setDefaultAsianFontName('標楷體'); |
| 71 | +``` |
| 72 | + |
| 73 | +## Document settings |
| 74 | + |
| 75 | +Settings for the generated document can be set using ``$phpWord->getSettings()`` |
| 76 | + |
| 77 | +### Magnification Setting |
| 78 | + |
| 79 | +The default zoom value is 100 percent. This can be changed either to another percentage |
| 80 | + |
| 81 | +``` php |
| 82 | +<?php |
| 83 | + |
| 84 | +$phpWord->getSettings()->setZoom(75); |
| 85 | +``` |
| 86 | + |
| 87 | +Or to predefined values ``fullPage``, ``bestFit``, ``textFit`` |
| 88 | + |
| 89 | +``` php |
| 90 | +<?php |
| 91 | + |
| 92 | +$phpWord->getSettings()->setZoom(Zoom::BEST_FIT); |
| 93 | +``` |
| 94 | + |
| 95 | +### Mirroring the Page Margins |
| 96 | + |
| 97 | +Use mirror margins to set up facing pages for double-sided documents, such as books or magazines. |
| 98 | + |
| 99 | +``` php |
| 100 | +<?php |
| 101 | + |
| 102 | +$phpWord->getSettings()->setMirrorMargins(true); |
| 103 | +``` |
| 104 | + |
| 105 | +!!! note annotate "Don't forget to set both paper size and page size" |
| 106 | + |
| 107 | + For example, to print a document on A4 paper (landscape) and fold it into A5 pages (portrait), use this section style: |
| 108 | + |
| 109 | + ``` php |
| 110 | + <?php |
| 111 | + |
| 112 | + use PhpOffice\PhpWord\Shared\Converter; |
| 113 | + |
| 114 | + $phpWord->getSettings()->setMirrorMargins(true); |
| 115 | + $phpWord->addSection([ |
| 116 | + 'paperSize' => 'A4', |
| 117 | + 'orientation' => 'landscape', |
| 118 | + 'pageSizeW' => Converter::cmToTwip(14.85), |
| 119 | + 'pageSizeH' => Converter::cmToTwip(21), |
| 120 | + ]); |
| 121 | + ``` |
| 122 | + |
| 123 | +### Printing as folded booklet |
| 124 | + |
| 125 | +Use book-fold printing to set up documents to be printed as foldable pages. |
| 126 | + |
| 127 | +``` php |
| 128 | +<?php |
| 129 | + |
| 130 | +$phpWord->getSettings()->setBookFoldPrinting(true); |
| 131 | +``` |
| 132 | + |
| 133 | +### Spelling and grammatical checks |
| 134 | + |
| 135 | +By default spelling and grammatical errors are shown as soon as you open a word document. |
| 136 | +For big documents this can slow down the opening of the document. You can hide the spelling and/or grammatical errors with: |
| 137 | + |
| 138 | +``` php |
| 139 | +<?php |
| 140 | + |
| 141 | +$phpWord->getSettings()->setHideGrammaticalErrors(true); |
| 142 | +$phpWord->getSettings()->setHideSpellingErrors(true); |
| 143 | +``` |
| 144 | + |
| 145 | +You can also specify the status of the spell and grammar checks, marking spelling or grammar as dirty will force a re-check when opening the document. |
| 146 | + |
| 147 | +``` php |
| 148 | +<?php |
| 149 | + |
| 150 | +$proofState = new \PhpOffice\PhpWord\ComplexType\ProofState(); |
| 151 | +$proofState->setGrammar(\PhpOffice\PhpWord\ComplexType\ProofState::CLEAN); |
| 152 | +$proofState->setSpelling(\PhpOffice\PhpWord\ComplexType\ProofState::DIRTY); |
| 153 | + |
| 154 | +$phpWord->getSettings()->setProofState($proofState); |
| 155 | +``` |
| 156 | + |
| 157 | +### Track Revisions |
| 158 | + |
| 159 | +Track changes can be activated using ``setTrackRevisions``, you can furture specify |
| 160 | + |
| 161 | +- Not to use move syntax, instead moved items will be seen as deleted in one place and added in another |
| 162 | +- Not track formatting revisions |
| 163 | + |
| 164 | +``` php |
| 165 | +<?php |
| 166 | + |
| 167 | +$phpWord->getSettings()->setTrackRevisions(true); |
| 168 | +$phpWord->getSettings()->setDoNotTrackMoves(true); |
| 169 | +$phpWord->getSettings()->setDoNotTrackFormatting(true); |
| 170 | +``` |
| 171 | + |
| 172 | +### Decimal Symbol |
| 173 | + |
| 174 | +The default symbol to represent a decimal figure is the ``.`` in english. In french you might want to change it to ``,`` for instance. |
| 175 | + |
| 176 | +``` php |
| 177 | +<?php |
| 178 | + |
| 179 | +$phpWord->getSettings()->setDecimalSymbol(','); |
| 180 | +``` |
| 181 | + |
| 182 | +### Document Language |
| 183 | + |
| 184 | +The default language of the document can be change with the following. |
| 185 | + |
| 186 | +``` php |
| 187 | +<?php |
| 188 | + |
| 189 | +$phpWord->getSettings()->setThemeFontLang(new Language(Language::FR_BE)); |
| 190 | +``` |
| 191 | + |
| 192 | +``Language`` has 3 parameters, one for Latin languages, one for East Asian languages and one for Complex (Bi-Directional) languages. |
| 193 | +A couple of language codes are provided in the ``PhpOffice\PhpWord\Style\Language`` class but any valid code/ID can be used. |
| 194 | + |
| 195 | +In case you are generating an RTF document the language need to be set differently. |
| 196 | + |
| 197 | +``` php |
| 198 | +<?php |
| 199 | + |
| 200 | +$lang = new Language(); |
| 201 | +$lang->setLangId(Language::EN_GB_ID); |
| 202 | +$phpWord->getSettings()->setThemeFontLang($lang); |
| 203 | +``` |
| 204 | + |
| 205 | +## Document information |
| 206 | + |
| 207 | +You can set the document information such as title, creator, and company |
| 208 | +name. Use the following functions: |
| 209 | + |
| 210 | +``` php |
| 211 | +<?php |
| 212 | + |
| 213 | +$properties = $phpWord->getDocInfo(); |
| 214 | +$properties->setCreator('My name'); |
| 215 | +$properties->setCompany('My factory'); |
| 216 | +$properties->setTitle('My title'); |
| 217 | +$properties->setDescription('My description'); |
| 218 | +$properties->setCategory('My category'); |
| 219 | +$properties->setLastModifiedBy('My name'); |
| 220 | +$properties->setCreated(mktime(0, 0, 0, 3, 12, 2014)); |
| 221 | +$properties->setModified(mktime(0, 0, 0, 3, 14, 2014)); |
| 222 | +$properties->setSubject('My subject'); |
| 223 | +$properties->setKeywords('my, key, word'); |
| 224 | +``` |
| 225 | + |
| 226 | +## Measurement units |
| 227 | + |
| 228 | +The base length unit in Open Office XML is twip. Twip means "TWentieth |
| 229 | +of an Inch Point", i.e. 1 twip = 1/1440 inch. |
| 230 | + |
| 231 | +You can use PHPWord helper functions to convert inches, centimeters, or |
| 232 | +points to twip. |
| 233 | + |
| 234 | +``` php |
| 235 | +<?php |
| 236 | + |
| 237 | +// Paragraph with 6 points space after |
| 238 | +$phpWord->addParagraphStyle('My Style', array( |
| 239 | + 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6)) |
| 240 | +); |
| 241 | + |
| 242 | +$section = $phpWord->addSection(); |
| 243 | +$sectionStyle = $section->getStyle(); |
| 244 | +// half inch left margin |
| 245 | +$sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5)); |
| 246 | +// 2 cm right margin |
| 247 | +$sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2)); |
| 248 | +``` |
| 249 | + |
| 250 | +## Document protection |
| 251 | + |
| 252 | +The document (or parts of it) can be password protected. |
| 253 | + |
| 254 | +``` php |
| 255 | +<?php |
| 256 | + |
| 257 | +$documentProtection = $phpWord->getSettings()->getDocumentProtection(); |
| 258 | +$documentProtection->setEditing(DocProtect::READ_ONLY); |
| 259 | +$documentProtection->setPassword('myPassword'); |
| 260 | +``` |
| 261 | + |
| 262 | +## Automatically Recalculate Fields on Open |
| 263 | + |
| 264 | +To force an update of the fields present in the document, set updateFields to true |
| 265 | + |
| 266 | +``` php |
| 267 | +<?php |
| 268 | + |
| 269 | +$phpWord->getSettings()->setUpdateFields(true); |
| 270 | +``` |
| 271 | + |
| 272 | +## Hyphenation |
| 273 | + |
| 274 | +Hyphenation describes the process of breaking words with hyphens. There are several options to control hyphenation. |
| 275 | + |
| 276 | +### Auto hyphenation |
| 277 | + |
| 278 | +To automatically hyphenate text set ``autoHyphenation`` to ``true``. |
| 279 | + |
| 280 | +``` php |
| 281 | +<?php |
| 282 | + |
| 283 | +$phpWord->getSettings()->setAutoHyphenation(true); |
| 284 | +``` |
| 285 | + |
| 286 | +### Consecutive Hyphen Limit |
| 287 | + |
| 288 | +The maximum number of consecutive lines of text ending with a hyphen can be controlled by the ``consecutiveHyphenLimit`` option. |
| 289 | +There is no limit if the option is not set or the provided value is ``0``. |
| 290 | + |
| 291 | +``` php |
| 292 | +<?php |
| 293 | + |
| 294 | +$phpWord->getSettings()->setConsecutiveHyphenLimit(2); |
| 295 | +``` |
| 296 | + |
| 297 | +### Hyphenation Zone |
| 298 | + |
| 299 | +The hyphenation zone (in *twip*) is the allowed amount of whitespace before hyphenation is applied. |
| 300 | +The smaller the hyphenation zone the more words are hyphenated. Or in other words, the wider the hyphenation zone the less words are hyphenated. |
| 301 | + |
| 302 | +``` php |
| 303 | +<?php |
| 304 | + |
| 305 | +$phpWord->getSettings()->setHyphenationZone(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(1)); |
| 306 | +``` |
| 307 | + |
| 308 | +### Hyphenate Caps |
| 309 | + |
| 310 | +To control whether or not words in all capital letters shall be hyphenated use the `doNotHyphenateCaps` option. |
| 311 | + |
| 312 | +``` php |
| 313 | +<?php |
| 314 | + |
| 315 | +$phpWord->getSettings()->setDoNotHyphenateCaps(true); |
| 316 | +``` |
0 commit comments