Keine Beschreibung

general.rst 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. .. _general:
  2. General usage
  3. =============
  4. Basic example
  5. -------------
  6. The following is a basic example of the PHPWord library. More examples
  7. are provided in the `samples
  8. folder <https://github.com/PHPOffice/PHPWord/tree/master/samples/>`__.
  9. .. code-block:: php
  10. <?php
  11. require_once 'src/PhpWord/Autoloader.php';
  12. \PhpOffice\PhpWord\Autoloader::register();
  13. // Creating the new document...
  14. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  15. /* Note: any element you append to a document must reside inside of a Section. */
  16. // Adding an empty Section to the document...
  17. $section = $phpWord->addSection();
  18. // Adding Text element to the Section having font styled by default...
  19. $section->addText(
  20. htmlspecialchars(
  21. '"Learn from yesterday, live for today, hope for tomorrow. '
  22. . 'The important thing is not to stop questioning." '
  23. . '(Albert Einstein)'
  24. )
  25. );
  26. /*
  27. * Note: it's possible to customize font style of the Text element you add in three ways:
  28. * - inline;
  29. * - using named font style (new font style object will be implicitly created);
  30. * - using explicitly created font style object.
  31. */
  32. // Adding Text element with font customized inline...
  33. $section->addText(
  34. htmlspecialchars(
  35. '"Great achievement is usually born of great sacrifice, '
  36. . 'and is never the result of selfishness." '
  37. . '(Napoleon Hill)'
  38. ),
  39. array('name' => 'Tahoma', 'size' => 10)
  40. );
  41. // Adding Text element with font customized using named font style...
  42. $fontStyleName = 'oneUserDefinedStyle';
  43. $phpWord->addFontStyle(
  44. $fontStyleName,
  45. array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
  46. );
  47. $section->addText(
  48. htmlspecialchars(
  49. '"The greatest accomplishment is not in never falling, '
  50. . 'but in rising again after you fall." '
  51. . '(Vince Lombardi)'
  52. ),
  53. $fontStyleName
  54. );
  55. // Adding Text element with font customized using explicitly created font style object...
  56. $fontStyle = new \PhpOffice\PhpWord\Style\Font();
  57. $fontStyle->setBold(true);
  58. $fontStyle->setName('Tahoma');
  59. $fontStyle->setSize(13);
  60. $myTextElement = $section->addText(
  61. htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)')
  62. );
  63. $myTextElement->setFontStyle($fontStyle);
  64. // Saving the document as OOXML file...
  65. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
  66. $objWriter->save('helloWorld.docx');
  67. // Saving the document as ODF file...
  68. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
  69. $objWriter->save('helloWorld.odt');
  70. // Saving the document as HTML file...
  71. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
  72. $objWriter->save('helloWorld.html');
  73. /* Note: we skip RTF, because it's not XML-based and requires a different example. */
  74. /* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */
  75. Settings
  76. --------
  77. The ``PhpOffice\PhpWord\Settings`` class provides some options that will
  78. affect the behavior of PHPWord. Below are the options.
  79. XML Writer compatibility
  80. ~~~~~~~~~~~~~~~~~~~~~~~~
  81. This option sets
  82. `XMLWriter::setIndent <http://www.php.net/manual/en/function.xmlwriter-set-indent.php>`__
  83. and
  84. `XMLWriter::setIndentString <http://www.php.net/manual/en/function.xmlwriter-set-indent-string.php>`__.
  85. The default value of this option is ``true`` (compatible), which is
  86. `required for
  87. OpenOffice <https://github.com/PHPOffice/PHPWord/issues/103>`__ to
  88. render OOXML document correctly. You can set this option to ``false``
  89. during development to make the resulting XML file easier to read.
  90. .. code-block:: php
  91. \PhpOffice\PhpWord\Settings::setCompatibility(false);
  92. Zip class
  93. ~~~~~~~~~
  94. By default, PHPWord uses `Zip extension <http://php.net/manual/en/book.zip.php>`__
  95. to deal with ZIP compressed archives and files inside them. If you can't have
  96. Zip extension installed on your server, you can use pure PHP library
  97. alternative, `PclZip <http://www.phpconcept.net/pclzip/>`__, which
  98. included with PHPWord.
  99. .. code-block:: php
  100. \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
  101. Default font
  102. ------------
  103. By default, every text appears in Arial 10 point. You can alter the
  104. default font by using the following two functions:
  105. .. code-block:: php
  106. $phpWord->setDefaultFontName('Times New Roman');
  107. $phpWord->setDefaultFontSize(12);
  108. Document information
  109. --------------------
  110. You can set the document information such as title, creator, and company
  111. name. Use the following functions:
  112. .. code-block:: php
  113. $properties = $phpWord->getDocInfo();
  114. $properties->setCreator('My name');
  115. $properties->setCompany('My factory');
  116. $properties->setTitle('My title');
  117. $properties->setDescription('My description');
  118. $properties->setCategory('My category');
  119. $properties->setLastModifiedBy('My name');
  120. $properties->setCreated(mktime(0, 0, 0, 3, 12, 2014));
  121. $properties->setModified(mktime(0, 0, 0, 3, 14, 2014));
  122. $properties->setSubject('My subject');
  123. $properties->setKeywords('my, key, word');
  124. Measurement units
  125. -----------------
  126. The base length unit in Open Office XML is twip. Twip means "TWentieth
  127. of an Inch Point", i.e. 1 twip = 1/1440 inch.
  128. You can use PHPWord helper functions to convert inches, centimeters, or
  129. points to twips.
  130. .. code-block:: php
  131. // Paragraph with 6 points space after
  132. $phpWord->addParagraphStyle('My Style', array(
  133. 'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(6))
  134. );
  135. $section = $phpWord->addSection();
  136. $sectionStyle = $section->getStyle();
  137. // half inch left margin
  138. $sectionStyle->setMarginLeft(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(.5));
  139. // 2 cm right margin
  140. $sectionStyle->setMarginRight(\PhpOffice\PhpWord\Shared\Converter::cmToTwip(2));