PHPWordChajian.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * PHPWord类
  4. */
  5. class PHPWordChajian extends Chajian{
  6. private $vendorbool=false;
  7. protected function initChajian()
  8. {
  9. $path = ''.ROOT_PATH.'/include/vendor/autoload.php';
  10. if(file_exists($path)){
  11. require_once($path);
  12. $this->vendorbool = true;
  13. }
  14. }
  15. public function isbool()
  16. {
  17. return $this->vendorbool;
  18. }
  19. public function test()
  20. {
  21. if(!$this->vendorbool)return;
  22. \PhpOffice\PhpWord\Settings::loadConfig();
  23. \PhpOffice\PhpWord\Settings::setPdfRenderer(\PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF, ''.ROOT_PATH.'/include/vendor/dompdf/dompdf');
  24. \PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
  25. $languageEnGb = new \PhpOffice\PhpWord\Style\Language(\PhpOffice\PhpWord\Style\Language::EN_GB);
  26. $phpWord = new \PhpOffice\PhpWord\PhpWord();
  27. $phpWord->getSettings()->setThemeFontLang($languageEnGb);
  28. $section = $phpWord->addSection();
  29. // Adding Text element to the Section having font styled by default...
  30. $section->addText(
  31. '"Learn from yesterday, live for today, hope for tomorrow. '
  32. . 'The important thing is信呼 not to stop questioning." '
  33. . '(Albert Einstein)'
  34. );
  35. //$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
  36. //$objWriter->save('wfewew.docx');
  37. $phpWord->save('helloWorld'.time().'.pdf','PDF');
  38. }
  39. /**
  40. * 只能替换.docx
  41. */
  42. public function replaceWord($path, $data=array(), $npath='')
  43. {
  44. if(!$this->vendorbool)return returnerror('未安装插件');
  45. $PhpWord = new \PhpOffice\PhpWord\TemplateProcessor($path);
  46. foreach($data as $k=>$v)$PhpWord->setValue($k, $v);
  47. if($npath=='')$npath = str_replace('.docx',''.rand(1000,9999).'.docx', $path);
  48. $PhpWord->saveAs($npath);
  49. return returnsuccess($npath);
  50. }
  51. }
粤ICP备19079148号