No Description

Sample_12_HeaderFooter.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. include_once 'Sample_Header.php';
  3. // New Word document
  4. echo date('H:i:s'), ' Create new PhpWord object', EOL;
  5. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  6. // New portrait section
  7. $section = $phpWord->addSection();
  8. // Add first page header
  9. $header = $section->addHeader();
  10. $header->firstPage();
  11. $table = $header->addTable();
  12. $table->addRow();
  13. $cell = $table->addCell(4500);
  14. $textrun = $cell->addTextRun();
  15. $textrun->addText(htmlspecialchars('This is the header with '));
  16. $textrun->addLink('http://google.com', htmlspecialchars('link to Google'));
  17. $table->addCell(4500)->addImage(
  18. 'resources/PhpWord.png',
  19. array('width' => 80, 'height' => 80, 'align' => 'right')
  20. );
  21. // Add header for all other pages
  22. $subsequent = $section->addHeader();
  23. $subsequent->addText(htmlspecialchars('Subsequent pages in Section 1 will Have this!'));
  24. $subsequent->addImage('resources/_mars.jpg', array('width' => 80, 'height' => 80));
  25. // Add footer
  26. $footer = $section->addFooter();
  27. $footer->addPreserveText(htmlspecialchars('Page {PAGE} of {NUMPAGES}.'), null, array('align' => 'center'));
  28. $footer->addLink('http://google.com', htmlspecialchars('Direct Google'));
  29. // Write some text
  30. $section->addTextBreak();
  31. $section->addText(htmlspecialchars('Some text...'));
  32. // Create a second page
  33. $section->addPageBreak();
  34. // Write some text
  35. $section->addTextBreak();
  36. $section->addText(htmlspecialchars('Some text...'));
  37. // Create a third page
  38. $section->addPageBreak();
  39. // Write some text
  40. $section->addTextBreak();
  41. $section->addText(htmlspecialchars('Some text...'));
  42. // New portrait section
  43. $section2 = $phpWord->addSection();
  44. $sec2Header = $section2->addHeader();
  45. $sec2Header->addText(htmlspecialchars('All pages in Section 2 will Have this!'));
  46. // Write some text
  47. $section2->addTextBreak();
  48. $section2->addText(htmlspecialchars('Some text...'));
  49. // Save file
  50. echo write($phpWord, basename(__FILE__, '.php'), $writers);
  51. if (!CLI) {
  52. include_once 'Sample_Footer.php';
  53. }