2
0

export.php 3.6 KB

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