123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- .. _general:
-
- General usage
- =============
-
- Basic example
- -------------
-
- The following is a basic example of the PHPWord library. More examples
- are provided in the `samples
- folder <https:
-
- .. code-block:: php
-
- <?php
- require_once 'src/PhpWord/Autoloader.php';
- \PhpOffice\PhpWord\Autoloader::register();
-
-
- $phpWord = new \PhpOffice\PhpWord\PhpWord();
-
-
-
-
- $section = $phpWord->addSection();
-
- $section->addText(
- htmlspecialchars(
- '"Learn from yesterday, live for today, hope for tomorrow. '
- . 'The important thing is not to stop questioning." '
- . '(Albert Einstein)'
- )
- );
-
-
-
-
-
- $section->addText(
- htmlspecialchars(
- '"Great achievement is usually born of great sacrifice, '
- . 'and is never the result of selfishness." '
- . '(Napoleon Hill)'
- ),
- array('name' => 'Tahoma', 'size' => 10)
- );
-
-
- $fontStyleName = 'oneUserDefinedStyle';
- $phpWord->addFontStyle(
- $fontStyleName,
- array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
- );
- $section->addText(
- htmlspecialchars(
- '"The greatest accomplishment is not in never falling, '
- . 'but in rising again after you fall." '
- . '(Vince Lombardi)'
- ),
- $fontStyleName
- );
-
-
- $fontStyle = new \PhpOffice\PhpWord\Style\Font();
- $fontStyle->setBold(true);
- $fontStyle->setName('Tahoma');
- $fontStyle->setSize(13);
- $myTextElement = $section->addText(
- htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)')
- );
- $myTextElement->setFontStyle($fontStyle);
-
-
- $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
- $objWriter->save('helloWorld.docx');
-
-
- $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
- $objWriter->save('helloWorld.odt');
-
-
- $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
- $objWriter->save('helloWorld.html');
-
-
-
-
- Settings
- --------
-
- The ``PhpOffice\PhpWord\Settings`` class provides some options that will
- affect the behavior of PHPWord. Below are the options.
-
- XML Writer compatibility
- ~~~~~~~~~~~~~~~~~~~~~~~~
-
- This option sets
- `XMLWriter::setIndent <http:
- and
- `XMLWriter::setIndentString <http:
- The default value of this option is ``true`` (compatible), which is
- `required for
- OpenOffice <https:
- render OOXML document correctly. You can set this option to ``false``
- during development to make the resulting XML file easier to read.
-
- .. code-block:: php
-
- \PhpOffice\PhpWord\Settings::setCompatibility(false);
-
- Zip class
- ~~~~~~~~~
-
- By default, PHPWord uses `Zip extension <http:
- 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:
- included with PHPWord.
-
- .. code-block:: php
-
- \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
-
- Default font
- ------------
-
- By default, every text appears in Arial 10 point. You can alter the
- default font by using the following two functions:
-
- .. code-block:: php
-
- $phpWord->setDefaultFontName('Times New Roman');
- $phpWord->setDefaultFontSize(12);
-
- Document information
- --------------------
-
- You can set the document information such as title, creator, and company
- name. Use the following functions:
-
- .. code-block:: php
-
- $properties = $phpWord->getDocInfo();
- $properties->setCreator('My name');
- $properties->setCompany('My factory');
- $properties->setTitle('My title');
- $properties->setDescription('My description');
- $properties->setCategory('My category');
- $properties->setLastModifiedBy('My name');
- $properties->setCreated(mktime(0, 0, 0, 3, 12, 2014));
- $properties->setModified(mktime(0, 0, 0, 3, 14, 2014));
- $properties->setSubject('My subject');
- $properties->setKeywords('my, key, word');
-
- Measurement units
- -----------------
-
- 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.
-
- .. code-block:: php
-
-
- $phpWord->addParagraphStyle('My Style', array(
- 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6))
- );
-
- $section = $phpWord->addSection();
- $sectionStyle = $section->getStyle();
-
- $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
-
- $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));
|