暫無描述

recipes.rst 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. .. _recipes:
  2. Recipes
  3. =======
  4. Create float left image
  5. -----------------------
  6. Use absolute positioning relative to margin horizontally and to line
  7. vertically.
  8. .. code-block:: php
  9. $imageStyle = array(
  10. 'width' => 40,
  11. 'height' => 40,
  12. 'wrappingStyle' => 'square',
  13. 'positioning' => 'absolute',
  14. 'posHorizontalRel' => 'margin',
  15. 'posVerticalRel' => 'line',
  16. );
  17. $textrun->addImage('resources/_earth.jpg', $imageStyle);
  18. $textrun->addText($lipsumText);
  19. Download the produced file automatically
  20. ----------------------------------------
  21. Use ``php://output`` as the filename.
  22. .. code-block:: php
  23. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  24. $section = $phpWord->createSection();
  25. $section->addText('Hello World!');
  26. $file = 'HelloWorld.docx';
  27. header("Content-Description: File Transfer");
  28. header('Content-Disposition: attachment; filename="' . $file . '"');
  29. header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
  30. header('Content-Transfer-Encoding: binary');
  31. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  32. header('Expires: 0');
  33. $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
  34. $xmlWriter->save("php://output");
  35. Create numbered headings
  36. ------------------------
  37. Define a numbering style and title styles, and match the two styles (with ``pStyle`` and ``numStyle``) like below.
  38. .. code-block:: php
  39. $phpWord->addNumberingStyle(
  40. 'hNum',
  41. array('type' => 'multilevel', 'levels' => array(
  42. array('pStyle' => 'Heading1', 'format' => 'decimal', 'text' => '%1'),
  43. array('pStyle' => 'Heading2', 'format' => 'decimal', 'text' => '%1.%2'),
  44. array('pStyle' => 'Heading3', 'format' => 'decimal', 'text' => '%1.%2.%3'),
  45. )
  46. )
  47. );
  48. $phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
  49. $phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
  50. $phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
  51. $section->addTitle('Heading 1', 1);
  52. $section->addTitle('Heading 2', 2);
  53. $section->addTitle('Heading 3', 3);
  54. Add a link within a title
  55. -------------------------
  56. Apply 'HeadingN' paragraph style to TextRun or Link. Sample code:
  57. .. code-block:: php
  58. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  59. $phpWord->addTitleStyle(1, array('size' => 16, 'bold' => true));
  60. $phpWord->addTitleStyle(2, array('size' => 14, 'bold' => true));
  61. $phpWord->addFontStyle('Link', array('color' => '0000FF', 'underline' => 'single'));
  62. $section = $phpWord->addSection();
  63. // Textrun
  64. $textrun = $section->addTextRun('Heading1');
  65. $textrun->addText('The ');
  66. $textrun->addLink('https://github.com/PHPOffice/PHPWord', 'PHPWord', 'Link');
  67. // Link
  68. $section->addLink('https://github.com/', 'GitHub', 'Link', 'Heading2');
  69. Remove [Compatibility Mode] text in the MS Word title bar
  70. ---------------------------------------------------------
  71. Use the ``Metadata\Compatibility\setOoxmlVersion(n)`` method with ``n`` is the version of Office (14 = Office 2010, 15 = Office 2013).
  72. .. code-block:: php
  73. $phpWord->getCompatibility()->setOoxmlVersion(15);