暫無描述

ODText.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @link https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2014 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer;
  18. use PhpOffice\PhpWord\Media;
  19. use PhpOffice\PhpWord\PhpWord;
  20. /**
  21. * ODText writer
  22. *
  23. * @since 0.7.0
  24. */
  25. class ODText extends AbstractWriter implements WriterInterface
  26. {
  27. /**
  28. * Create new ODText writer
  29. *
  30. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  31. */
  32. public function __construct(PhpWord $phpWord = null)
  33. {
  34. // Assign PhpWord
  35. $this->setPhpWord($phpWord);
  36. // Create parts
  37. $this->parts = array(
  38. 'Mimetype' => 'mimetype',
  39. 'Content' => 'content.xml',
  40. 'Meta' => 'meta.xml',
  41. 'Styles' => 'styles.xml',
  42. 'Manifest' => 'META-INF/manifest.xml',
  43. );
  44. foreach (array_keys($this->parts) as $partName) {
  45. $partClass = get_class($this) . '\\Part\\' . $partName;
  46. if (class_exists($partClass)) {
  47. /** @var $partObject \PhpOffice\PhpWord\Writer\ODText\Part\AbstractPart Type hint */
  48. $partObject = new $partClass();
  49. $partObject->setParentWriter($this);
  50. $this->writerParts[strtolower($partName)] = $partObject;
  51. }
  52. }
  53. // Set package paths
  54. $this->mediaPaths = array('image' => 'Pictures/');
  55. }
  56. /**
  57. * Save PhpWord to file.
  58. *
  59. * @param string $filename
  60. * @return void
  61. */
  62. public function save($filename = null)
  63. {
  64. $filename = $this->getTempFile($filename);
  65. $zip = $this->getZipArchive($filename);
  66. // Add section media files
  67. $sectionMedia = Media::getElements('section');
  68. if (!empty($sectionMedia)) {
  69. $this->addFilesToPackage($zip, $sectionMedia);
  70. }
  71. // Write parts
  72. foreach ($this->parts as $partName => $fileName) {
  73. if ($fileName != '') {
  74. $zip->addFromString($fileName, $this->getWriterPart($partName)->write());
  75. }
  76. }
  77. // Close zip archive and cleanup temp file
  78. $zip->close();
  79. $this->cleanupTempFile();
  80. }
  81. }