export.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. require_once '../../vendor/autoload.php';
  3. require_once '../path.php';
  4. require_once '../public/function.php';
  5. // Creating the new document...
  6. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  7. $phpWord->addTitleStyle(1, array('size' => 16), array('numStyle' => 'hNum', 'numLevel' => 0));
  8. $phpWord->addTitleStyle(2, array('size' => 14), array('numStyle' => 'hNum', 'numLevel' => 1));
  9. $phpWord->addTitleStyle(3, array('size' => 12), array('numStyle' => 'hNum', 'numLevel' => 2));
  10. /* Note: any element you append to a document must reside inside of a Section. */
  11. // Adding an empty Section to the document...
  12. $section = $phpWord->addSection();
  13. $section->addTitle('Article Title', 1);
  14. // Adding Text element to the Section having font styled by default...
  15. $section->addText(
  16. '"Learn from yesterday, live for today, hope for tomorrow. '
  17. . 'The important thing is not to stop questioning." '
  18. . '(Albert Einstein)'
  19. );
  20. /*
  21. * Note: it's possible to customize font style of the Text element you add in three ways:
  22. * - inline;
  23. * - using named font style (new font style object will be implicitly created);
  24. * - using explicitly created font style object.
  25. */
  26. // Adding Text element with font customized inline...
  27. $section->addText(
  28. '"Great achievement is usually born of great sacrifice, '
  29. . 'and is never the result of selfishness." '
  30. . '(Napoleon Hill)',
  31. array('name' => 'Tahoma', 'size' => 10)
  32. );
  33. $textrun = $section->addTextRun();
  34. $textrun->addText('Lead text.');
  35. $footnote = $textrun->addFootnote();
  36. $footnote->addText('Footnote text can have ');
  37. $footnote->addLink('http://test.com', 'links');
  38. $footnote->addText('.');
  39. $footnote->addTextBreak();
  40. $footnote->addText('And text break.');
  41. $textrun->addText('Trailing text.');
  42. // Adding Text element with font customized using named font style...
  43. $fontStyleName = 'oneUserDefinedStyle';
  44. $phpWord->addFontStyle(
  45. $fontStyleName,
  46. array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
  47. );
  48. $section->addText(
  49. '"The greatest accomplishment is not in never falling, '
  50. . 'but in rising again after you fall." '
  51. . '(Vince Lombardi)',
  52. $fontStyleName
  53. );
  54. // Adding Text element with font customized using explicitly created font style object...
  55. $fontStyle = new \PhpOffice\PhpWord\Style\Font();
  56. $fontStyle->setBold(true);
  57. $fontStyle->setName('Tahoma');
  58. $fontStyle->setSize(13);
  59. $myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
  60. $myTextElement->setFontStyle($fontStyle);
  61. // Saving the document as OOXML file...
  62. $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
  63. $tmpFileName = _DIR_TMP_EXPORT.'/'.UUID::v4().'.docx';
  64. $objWriter->save($tmpFileName);
  65. //4.从浏览器下载
  66. ob_clean();
  67. ob_start();
  68. $fp = fopen($tmpFileName,"r");
  69. $file_size = filesize($tmpFileName);
  70. Header("Content-type:application/octet-stream");
  71. Header("Accept-Ranges:bytes");
  72. Header("Accept-Length:".$file_size);
  73. Header("Content-Disposition:attchment; filename=".'wikipali.docx');
  74. $buffer = 1024;
  75. $file_count = 0;
  76. while (!feof($fp) && $file_count < $file_size){
  77. $file_con = fread($fp,$buffer);
  78. $file_count += $buffer;
  79. echo $file_con;
  80. }
  81. fclose($fp);
  82. ob_end_flush();
  83. unlink($tmpFileName);