HTML.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * HTML writer
  21. *
  22. * Not supported: PreserveText, PageBreak, Object
  23. * @since 0.10.0
  24. */
  25. class HTML extends AbstractWriter implements WriterInterface
  26. {
  27. /**
  28. * Is the current writer creating PDF?
  29. *
  30. * @var bool
  31. */
  32. protected $isPdf = false;
  33. /**
  34. * Footnotes and endnotes collection
  35. *
  36. * @var array
  37. */
  38. protected $notes = array();
  39. /**
  40. * Create new instance
  41. */
  42. public function __construct(PhpWord $phpWord = null)
  43. {
  44. $this->setPhpWord($phpWord);
  45. $this->parts = array('Head', 'Body');
  46. foreach ($this->parts as $partName) {
  47. $partClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Part\\' . $partName;
  48. if (class_exists($partClass)) {
  49. /** @var \PhpOffice\PhpWord\Writer\HTML\Part\AbstractPart $part Type hint */
  50. $part = new $partClass();
  51. $part->setParentWriter($this);
  52. $this->writerParts[strtolower($partName)] = $part;
  53. }
  54. }
  55. }
  56. /**
  57. * Save PhpWord to file.
  58. *
  59. * @param string $filename
  60. *
  61. * @throws \PhpOffice\PhpWord\Exception\Exception
  62. */
  63. public function save($filename = null)
  64. {
  65. $this->writeFile($this->openFile($filename), $this->getContent());
  66. }
  67. /**
  68. * Get content
  69. *
  70. * @return string
  71. * @since 0.11.0
  72. */
  73. public function getContent()
  74. {
  75. $content = '';
  76. $content .= '<!DOCTYPE html>' . PHP_EOL;
  77. $content .= '<!-- Generated by PHPWord -->' . PHP_EOL;
  78. $content .= '<html>' . PHP_EOL;
  79. $content .= $this->getWriterPart('Head')->write();
  80. $content .= $this->getWriterPart('Body')->write();
  81. $content .= '</html>' . PHP_EOL;
  82. return $content;
  83. }
  84. /**
  85. * Get is PDF
  86. *
  87. * @return bool
  88. */
  89. public function isPdf()
  90. {
  91. return $this->isPdf;
  92. }
  93. /**
  94. * Get notes
  95. *
  96. * @return array
  97. */
  98. public function getNotes()
  99. {
  100. return $this->notes;
  101. }
  102. /**
  103. * Add note.
  104. *
  105. * @param int $noteId
  106. * @param string $noteMark
  107. */
  108. public function addNote($noteId, $noteMark)
  109. {
  110. $this->notes[$noteId] = $noteMark;
  111. }
  112. /**
  113. * Write document
  114. *
  115. * @deprecated 0.11.0
  116. *
  117. * @return string
  118. *
  119. * @codeCoverageIgnore
  120. */
  121. public function writeDocument()
  122. {
  123. return $this->getContent();
  124. }
  125. }