暂无描述

HomeController.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. class HomeController extends BaseController {
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Default Home Controller
  6. |--------------------------------------------------------------------------
  7. |
  8. | You may wish to use controllers instead of, or in addition to, Closure
  9. | based routes. That's great! Here is an example controller method to
  10. | get you started. To route to this controller, just add the route:
  11. |
  12. | Route::get('/', 'HomeController@showWelcome');
  13. |
  14. */
  15. public function showWelcome()
  16. {
  17. try {
  18. require_once 'PHPWord-0.12.1/src/PhpWord/Autoloader.php';
  19. \PhpOffice\PhpWord\Autoloader::register();
  20. // Creating the new document...
  21. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  22. $styleTable = array('borderColor'=>'006699',
  23. 'borderSize'=>6,
  24. 'cellMargin'=>50);
  25. $styleFirstRow = array('bgColor'=>'66BBFF');
  26. $phpWord->addTableStyle('myTable', $styleTable, $styleFirstRow);
  27. /* Note: any element you append to a document must reside inside of a Section. */
  28. // Adding an empty Section to the document...
  29. $section = $phpWord->addSection();
  30. // Adding Text element to the Section having font styled by default...
  31. $section->addText(
  32. htmlspecialchars(
  33. '"Learn from yesterday, live for today, hope for tomorrow. '
  34. . 'The important thing is not to stop questioning." '
  35. . '(Albert Einstein)'
  36. )
  37. );
  38. $table = $section->addTable('myTable');
  39. $table->addRow(3);
  40. $table->addCell(1200)->addText("Col 1");
  41. $table->addCell(1200)->addText("Col 2");
  42. $table->addCell(1200)
  43. ->addTable('myTable')
  44. ->addRow(2)->addCell(1200)
  45. ->addText("Col 1");
  46. /*
  47. * Note: it's possible to customize font style of the Text element you add in three ways:
  48. * - inline;
  49. * - using named font style (new font style object will be implicitly created);
  50. * - using explicitly created font style object.
  51. */
  52. // Adding Text element with font customized inline...
  53. $section->addText(
  54. htmlspecialchars(
  55. '"Great achievement is usually born of great sacrifice, '
  56. . 'and is never the result of selfishness." '
  57. . '(Napoleon Hill)'
  58. ),
  59. array('name' => 'Tahoma', 'size' => 10)
  60. );
  61. // Adding Text element with font customized using named font style...
  62. $fontStyleName = 'oneUserDefinedStyle';
  63. $phpWord->addFontStyle(
  64. $fontStyleName,
  65. array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
  66. );
  67. $section->addText(
  68. htmlspecialchars(
  69. '"The greatest accomplishment is not in never falling, '
  70. . 'but in rising again after you fall." '
  71. . '(Vince Lombardi)'
  72. ),
  73. $fontStyleName
  74. );
  75. // Adding Text element with font customized using explicitly created font style object...
  76. $fontStyle = new \PhpOffice\PhpWord\Style\Font();
  77. $fontStyle->setBold(true);
  78. $fontStyle->setName('Tahoma');
  79. $fontStyle->setSize(13);
  80. $myTextElement = $section->addText(
  81. htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor Roosevelt)')
  82. );
  83. $myTextElement->setFontStyle($fontStyle);
  84. // Saving the document as OOXML file...
  85. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
  86. $objWriter->save('helloWorld2.docx');
  87. return Response::download('helloWorld2.docx');
  88. }
  89. catch(Exception $e)
  90. {
  91. }
  92. }
  93. }