Head.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\HTML\Part;
  18. use PhpOffice\PhpWord\Settings;
  19. use PhpOffice\PhpWord\Style;
  20. use PhpOffice\PhpWord\Style\Font;
  21. use PhpOffice\PhpWord\Style\Paragraph;
  22. use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
  23. use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
  24. use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
  25. /**
  26. * RTF head part writer
  27. *
  28. * @since 0.11.0
  29. */
  30. class Head extends AbstractPart
  31. {
  32. /**
  33. * Write part
  34. *
  35. * @return string
  36. */
  37. public function write()
  38. {
  39. $docProps = $this->getParentWriter()->getPhpWord()->getDocInfo();
  40. $propertiesMapping = array(
  41. 'creator' => 'author',
  42. 'title' => '',
  43. 'description' => '',
  44. 'subject' => '',
  45. 'keywords' => '',
  46. 'category' => '',
  47. 'company' => '',
  48. 'manager' => '',
  49. );
  50. $title = $docProps->getTitle();
  51. $title = ($title != '') ? $title : 'PHPWord';
  52. $content = '';
  53. $content .= '<head>' . PHP_EOL;
  54. $content .= '<meta charset="UTF-8" />' . PHP_EOL;
  55. $content .= '<title>' . $title . '</title>' . PHP_EOL;
  56. foreach ($propertiesMapping as $key => $value) {
  57. $value = ($value == '') ? $key : $value;
  58. $method = 'get' . $key;
  59. if ($docProps->$method() != '') {
  60. $content .= '<meta name="' . $value . '"'
  61. . ' content="' . (Settings::isOutputEscapingEnabled() ? $this->escaper->escapeHtmlAttr($docProps->$method()) : $docProps->$method()) . '"'
  62. . ' />' . PHP_EOL;
  63. }
  64. }
  65. $content .= $this->writeStyles();
  66. $content .= '</head>' . PHP_EOL;
  67. return $content;
  68. }
  69. /**
  70. * Get styles
  71. *
  72. * @return string
  73. */
  74. private function writeStyles()
  75. {
  76. $css = '<style>' . PHP_EOL;
  77. // Default styles
  78. $defaultStyles = array(
  79. '*' => array(
  80. 'font-family' => Settings::getDefaultFontName(),
  81. 'font-size' => Settings::getDefaultFontSize() . 'pt',
  82. ),
  83. 'a.NoteRef' => array(
  84. 'text-decoration' => 'none',
  85. ),
  86. 'hr' => array(
  87. 'height' => '1px',
  88. 'padding' => '0',
  89. 'margin' => '1em 0',
  90. 'border' => '0',
  91. 'border-top' => '1px solid #CCC',
  92. ),
  93. 'table' => array(
  94. 'border' => '1px solid black',
  95. 'border-spacing' => '0px',
  96. 'width ' => '100%',
  97. ),
  98. 'td' => array(
  99. 'border' => '1px solid black',
  100. ),
  101. );
  102. foreach ($defaultStyles as $selector => $style) {
  103. $styleWriter = new GenericStyleWriter($style);
  104. $css .= $selector . ' {' . $styleWriter->write() . '}' . PHP_EOL;
  105. }
  106. // Custom styles
  107. $customStyles = Style::getStyles();
  108. if (is_array($customStyles)) {
  109. foreach ($customStyles as $name => $style) {
  110. if ($style instanceof Font) {
  111. $styleWriter = new FontStyleWriter($style);
  112. if ($style->getStyleType() == 'title') {
  113. $name = str_replace('Heading_', 'h', $name);
  114. } else {
  115. $name = '.' . $name;
  116. }
  117. $css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
  118. } elseif ($style instanceof Paragraph) {
  119. $styleWriter = new ParagraphStyleWriter($style);
  120. $name = '.' . $name;
  121. $css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
  122. }
  123. }
  124. }
  125. $css .= '</style>' . PHP_EOL;
  126. return $css;
  127. }
  128. }