RTF.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord\Writer;
  18. use PhpOffice\PhpWord\PhpWord;
  19. /**
  20. * RTF writer
  21. *
  22. * @since 0.7.0
  23. */
  24. class RTF extends AbstractWriter implements WriterInterface
  25. {
  26. /**
  27. * Last paragraph style
  28. *
  29. * @var mixed
  30. */
  31. private $lastParagraphStyle;
  32. /**
  33. * Create new instance
  34. *
  35. * @param \PhpOffice\PhpWord\PhpWord $phpWord
  36. */
  37. public function __construct(PhpWord $phpWord = null)
  38. {
  39. $this->setPhpWord($phpWord);
  40. $this->parts = array('Header', 'Document');
  41. foreach ($this->parts as $partName) {
  42. $partClass = get_class($this) . '\\Part\\' . $partName;
  43. if (class_exists($partClass)) {
  44. /** @var \PhpOffice\PhpWord\Writer\RTF\Part\AbstractPart $part Type hint */
  45. $part = new $partClass();
  46. $part->setParentWriter($this);
  47. $this->writerParts[strtolower($partName)] = $part;
  48. }
  49. }
  50. }
  51. /**
  52. * Save content to file.
  53. *
  54. * @param string $filename
  55. * @throws \PhpOffice\PhpWord\Exception\Exception
  56. */
  57. public function save($filename = null)
  58. {
  59. $this->writeFile($this->openFile($filename), $this->getContent());
  60. }
  61. /**
  62. * Get content
  63. *
  64. * @return string
  65. * @since 0.11.0
  66. */
  67. private function getContent()
  68. {
  69. $content = '';
  70. $content .= '{';
  71. $content .= '\rtf1' . PHP_EOL;
  72. $content .= $this->getWriterPart('Header')->write();
  73. $content .= $this->getWriterPart('Document')->write();
  74. $content .= '}';
  75. return $content;
  76. }
  77. /**
  78. * Get font table.
  79. *
  80. * @return array
  81. */
  82. public function getFontTable()
  83. {
  84. return $this->getWriterPart('Header')->getFontTable();
  85. }
  86. /**
  87. * Get color table.
  88. *
  89. * @return array
  90. */
  91. public function getColorTable()
  92. {
  93. return $this->getWriterPart('Header')->getColorTable();
  94. }
  95. /**
  96. * Get last paragraph style.
  97. *
  98. * @return mixed
  99. */
  100. public function getLastParagraphStyle()
  101. {
  102. return $this->lastParagraphStyle;
  103. }
  104. /**
  105. * Set last paragraph style.
  106. *
  107. * @param mixed $value
  108. */
  109. public function setLastParagraphStyle($value = '')
  110. {
  111. $this->lastParagraphStyle = $value;
  112. }
  113. }