Html.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Reader;
  3. use DOMDocument;
  4. use DOMElement;
  5. use DOMNode;
  6. use DOMText;
  7. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  8. use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. use PhpOffice\PhpSpreadsheet\Style\Border;
  11. use PhpOffice\PhpSpreadsheet\Style\Color;
  12. use PhpOffice\PhpSpreadsheet\Style\Fill;
  13. use PhpOffice\PhpSpreadsheet\Style\Font;
  14. use PhpOffice\PhpSpreadsheet\Style\Style;
  15. use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
  16. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  17. /** PhpSpreadsheet root directory */
  18. class Html extends BaseReader
  19. {
  20. /**
  21. * Sample size to read to determine if it's HTML or not.
  22. */
  23. const TEST_SAMPLE_SIZE = 2048;
  24. /**
  25. * Input encoding.
  26. *
  27. * @var string
  28. */
  29. protected $inputEncoding = 'ANSI';
  30. /**
  31. * Sheet index to read.
  32. *
  33. * @var int
  34. */
  35. protected $sheetIndex = 0;
  36. /**
  37. * Formats.
  38. *
  39. * @var array
  40. */
  41. protected $formats = [
  42. 'h1' => [
  43. 'font' => [
  44. 'bold' => true,
  45. 'size' => 24,
  46. ],
  47. ], // Bold, 24pt
  48. 'h2' => [
  49. 'font' => [
  50. 'bold' => true,
  51. 'size' => 18,
  52. ],
  53. ], // Bold, 18pt
  54. 'h3' => [
  55. 'font' => [
  56. 'bold' => true,
  57. 'size' => 13.5,
  58. ],
  59. ], // Bold, 13.5pt
  60. 'h4' => [
  61. 'font' => [
  62. 'bold' => true,
  63. 'size' => 12,
  64. ],
  65. ], // Bold, 12pt
  66. 'h5' => [
  67. 'font' => [
  68. 'bold' => true,
  69. 'size' => 10,
  70. ],
  71. ], // Bold, 10pt
  72. 'h6' => [
  73. 'font' => [
  74. 'bold' => true,
  75. 'size' => 7.5,
  76. ],
  77. ], // Bold, 7.5pt
  78. 'a' => [
  79. 'font' => [
  80. 'underline' => true,
  81. 'color' => [
  82. 'argb' => Color::COLOR_BLUE,
  83. ],
  84. ],
  85. ], // Blue underlined
  86. 'hr' => [
  87. 'borders' => [
  88. 'bottom' => [
  89. 'borderStyle' => Border::BORDER_THIN,
  90. 'color' => [
  91. Color::COLOR_BLACK,
  92. ],
  93. ],
  94. ],
  95. ], // Bottom border
  96. 'strong' => [
  97. 'font' => [
  98. 'bold' => true,
  99. ],
  100. ], // Bold
  101. 'b' => [
  102. 'font' => [
  103. 'bold' => true,
  104. ],
  105. ], // Bold
  106. 'i' => [
  107. 'font' => [
  108. 'italic' => true,
  109. ],
  110. ], // Italic
  111. 'em' => [
  112. 'font' => [
  113. 'italic' => true,
  114. ],
  115. ], // Italic
  116. ];
  117. protected $rowspan = [];
  118. /**
  119. * Create a new HTML Reader instance.
  120. */
  121. public function __construct()
  122. {
  123. $this->readFilter = new DefaultReadFilter();
  124. $this->securityScanner = XmlScanner::getInstance($this);
  125. }
  126. /**
  127. * Validate that the current file is an HTML file.
  128. *
  129. * @param string $pFilename
  130. *
  131. * @return bool
  132. */
  133. public function canRead($pFilename)
  134. {
  135. // Check if file exists
  136. try {
  137. $this->openFile($pFilename);
  138. } catch (Exception $e) {
  139. return false;
  140. }
  141. $beginning = $this->readBeginning();
  142. $startWithTag = self::startsWithTag($beginning);
  143. $containsTags = self::containsTags($beginning);
  144. $endsWithTag = self::endsWithTag($this->readEnding());
  145. fclose($this->fileHandle);
  146. return $startWithTag && $containsTags && $endsWithTag;
  147. }
  148. private function readBeginning()
  149. {
  150. fseek($this->fileHandle, 0);
  151. return fread($this->fileHandle, self::TEST_SAMPLE_SIZE);
  152. }
  153. private function readEnding()
  154. {
  155. $meta = stream_get_meta_data($this->fileHandle);
  156. $filename = $meta['uri'];
  157. $size = filesize($filename);
  158. if ($size === 0) {
  159. return '';
  160. }
  161. $blockSize = self::TEST_SAMPLE_SIZE;
  162. if ($size < $blockSize) {
  163. $blockSize = $size;
  164. }
  165. fseek($this->fileHandle, $size - $blockSize);
  166. return fread($this->fileHandle, $blockSize);
  167. }
  168. private static function startsWithTag($data)
  169. {
  170. return '<' === substr(trim($data), 0, 1);
  171. }
  172. private static function endsWithTag($data)
  173. {
  174. return '>' === substr(trim($data), -1, 1);
  175. }
  176. private static function containsTags($data)
  177. {
  178. return strlen($data) !== strlen(strip_tags($data));
  179. }
  180. /**
  181. * Loads Spreadsheet from file.
  182. *
  183. * @param string $pFilename
  184. *
  185. * @throws Exception
  186. *
  187. * @return Spreadsheet
  188. */
  189. public function load($pFilename)
  190. {
  191. // Create new Spreadsheet
  192. $spreadsheet = new Spreadsheet();
  193. // Load into this instance
  194. return $this->loadIntoExisting($pFilename, $spreadsheet);
  195. }
  196. /**
  197. * Set input encoding.
  198. *
  199. * @param string $pValue Input encoding, eg: 'ANSI'
  200. *
  201. * @return Html
  202. */
  203. public function setInputEncoding($pValue)
  204. {
  205. $this->inputEncoding = $pValue;
  206. return $this;
  207. }
  208. /**
  209. * Get input encoding.
  210. *
  211. * @return string
  212. */
  213. public function getInputEncoding()
  214. {
  215. return $this->inputEncoding;
  216. }
  217. // Data Array used for testing only, should write to Spreadsheet object on completion of tests
  218. protected $dataArray = [];
  219. protected $tableLevel = 0;
  220. protected $nestedColumn = ['A'];
  221. protected function setTableStartColumn($column)
  222. {
  223. if ($this->tableLevel == 0) {
  224. $column = 'A';
  225. }
  226. ++$this->tableLevel;
  227. $this->nestedColumn[$this->tableLevel] = $column;
  228. return $this->nestedColumn[$this->tableLevel];
  229. }
  230. protected function getTableStartColumn()
  231. {
  232. return $this->nestedColumn[$this->tableLevel];
  233. }
  234. protected function releaseTableStartColumn()
  235. {
  236. --$this->tableLevel;
  237. return array_pop($this->nestedColumn);
  238. }
  239. protected function flushCell(Worksheet $sheet, $column, $row, &$cellContent)
  240. {
  241. if (is_string($cellContent)) {
  242. // Simple String content
  243. if (trim($cellContent) > '') {
  244. // Only actually write it if there's content in the string
  245. // Write to worksheet to be done here...
  246. // ... we return the cell so we can mess about with styles more easily
  247. $sheet->setCellValue($column . $row, $cellContent);
  248. $this->dataArray[$row][$column] = $cellContent;
  249. }
  250. } else {
  251. // We have a Rich Text run
  252. // TODO
  253. $this->dataArray[$row][$column] = 'RICH TEXT: ' . $cellContent;
  254. }
  255. $cellContent = (string) '';
  256. }
  257. /**
  258. * @param DOMNode $element
  259. * @param Worksheet $sheet
  260. * @param int $row
  261. * @param string $column
  262. * @param string $cellContent
  263. */
  264. protected function processDomElement(DOMNode $element, Worksheet $sheet, &$row, &$column, &$cellContent)
  265. {
  266. foreach ($element->childNodes as $child) {
  267. if ($child instanceof DOMText) {
  268. $domText = preg_replace('/\s+/u', ' ', trim($child->nodeValue));
  269. if (is_string($cellContent)) {
  270. // simply append the text if the cell content is a plain text string
  271. $cellContent .= $domText;
  272. }
  273. // but if we have a rich text run instead, we need to append it correctly
  274. // TODO
  275. } elseif ($child instanceof DOMElement) {
  276. $attributeArray = [];
  277. foreach ($child->attributes as $attribute) {
  278. $attributeArray[$attribute->name] = $attribute->value;
  279. }
  280. switch ($child->nodeName) {
  281. case 'meta':
  282. foreach ($attributeArray as $attributeName => $attributeValue) {
  283. // Extract character set, so we can convert to UTF-8 if required
  284. if ($attributeName === 'charset') {
  285. $this->setInputEncoding($attributeValue);
  286. }
  287. }
  288. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  289. break;
  290. case 'title':
  291. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  292. $sheet->setTitle($cellContent, true, false);
  293. $cellContent = '';
  294. break;
  295. case 'span':
  296. case 'div':
  297. case 'font':
  298. case 'i':
  299. case 'em':
  300. case 'strong':
  301. case 'b':
  302. if (isset($attributeArray['class']) && $attributeArray['class'] === 'comment') {
  303. $sheet->getComment($column . $row)
  304. ->getText()
  305. ->createTextRun($child->textContent);
  306. break;
  307. }
  308. if ($cellContent > '') {
  309. $cellContent .= ' ';
  310. }
  311. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  312. if ($cellContent > '') {
  313. $cellContent .= ' ';
  314. }
  315. if (isset($this->formats[$child->nodeName])) {
  316. $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
  317. }
  318. break;
  319. case 'hr':
  320. $this->flushCell($sheet, $column, $row, $cellContent);
  321. ++$row;
  322. if (isset($this->formats[$child->nodeName])) {
  323. $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
  324. } else {
  325. $cellContent = '----------';
  326. $this->flushCell($sheet, $column, $row, $cellContent);
  327. }
  328. ++$row;
  329. // Add a break after a horizontal rule, simply by allowing the code to dropthru
  330. // no break
  331. case 'br':
  332. if ($this->tableLevel > 0) {
  333. // If we're inside a table, replace with a \n
  334. $cellContent .= "\n";
  335. } else {
  336. // Otherwise flush our existing content and move the row cursor on
  337. $this->flushCell($sheet, $column, $row, $cellContent);
  338. ++$row;
  339. }
  340. break;
  341. case 'a':
  342. foreach ($attributeArray as $attributeName => $attributeValue) {
  343. switch ($attributeName) {
  344. case 'href':
  345. $sheet->getCell($column . $row)->getHyperlink()->setUrl($attributeValue);
  346. if (isset($this->formats[$child->nodeName])) {
  347. $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
  348. }
  349. break;
  350. case 'class':
  351. if ($attributeValue === 'comment-indicator') {
  352. break; // Ignore - it's just a red square.
  353. }
  354. }
  355. }
  356. $cellContent .= ' ';
  357. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  358. break;
  359. case 'h1':
  360. case 'h2':
  361. case 'h3':
  362. case 'h4':
  363. case 'h5':
  364. case 'h6':
  365. case 'ol':
  366. case 'ul':
  367. case 'p':
  368. if ($this->tableLevel > 0) {
  369. // If we're inside a table, replace with a \n
  370. $cellContent .= "\n";
  371. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  372. } else {
  373. if ($cellContent > '') {
  374. $this->flushCell($sheet, $column, $row, $cellContent);
  375. ++$row;
  376. }
  377. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  378. $this->flushCell($sheet, $column, $row, $cellContent);
  379. if (isset($this->formats[$child->nodeName])) {
  380. $sheet->getStyle($column . $row)->applyFromArray($this->formats[$child->nodeName]);
  381. }
  382. ++$row;
  383. $column = 'A';
  384. }
  385. break;
  386. case 'li':
  387. if ($this->tableLevel > 0) {
  388. // If we're inside a table, replace with a \n
  389. $cellContent .= "\n";
  390. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  391. } else {
  392. if ($cellContent > '') {
  393. $this->flushCell($sheet, $column, $row, $cellContent);
  394. }
  395. ++$row;
  396. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  397. $this->flushCell($sheet, $column, $row, $cellContent);
  398. $column = 'A';
  399. }
  400. break;
  401. case 'img':
  402. $this->insertImage($sheet, $column, $row, $attributeArray);
  403. break;
  404. case 'table':
  405. $this->flushCell($sheet, $column, $row, $cellContent);
  406. $column = $this->setTableStartColumn($column);
  407. if ($this->tableLevel > 1) {
  408. --$row;
  409. }
  410. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  411. $column = $this->releaseTableStartColumn();
  412. if ($this->tableLevel > 1) {
  413. ++$column;
  414. } else {
  415. ++$row;
  416. }
  417. break;
  418. case 'thead':
  419. case 'tbody':
  420. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  421. break;
  422. case 'tr':
  423. $column = $this->getTableStartColumn();
  424. $cellContent = '';
  425. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  426. if (isset($attributeArray['height'])) {
  427. $sheet->getRowDimension($row)->setRowHeight($attributeArray['height']);
  428. }
  429. ++$row;
  430. break;
  431. case 'th':
  432. case 'td':
  433. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  434. // apply inline style
  435. $this->applyInlineStyle($sheet, $row, $column, $attributeArray);
  436. while (isset($this->rowspan[$column . $row])) {
  437. ++$column;
  438. }
  439. $this->flushCell($sheet, $column, $row, $cellContent);
  440. if (isset($attributeArray['rowspan'], $attributeArray['colspan'])) {
  441. //create merging rowspan and colspan
  442. $columnTo = $column;
  443. for ($i = 0; $i < $attributeArray['colspan'] - 1; ++$i) {
  444. ++$columnTo;
  445. }
  446. $range = $column . $row . ':' . $columnTo . ($row + $attributeArray['rowspan'] - 1);
  447. foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) {
  448. $this->rowspan[$value] = true;
  449. }
  450. $sheet->mergeCells($range);
  451. $column = $columnTo;
  452. } elseif (isset($attributeArray['rowspan'])) {
  453. //create merging rowspan
  454. $range = $column . $row . ':' . $column . ($row + $attributeArray['rowspan'] - 1);
  455. foreach (Coordinate::extractAllCellReferencesInRange($range) as $value) {
  456. $this->rowspan[$value] = true;
  457. }
  458. $sheet->mergeCells($range);
  459. } elseif (isset($attributeArray['colspan'])) {
  460. //create merging colspan
  461. $columnTo = $column;
  462. for ($i = 0; $i < $attributeArray['colspan'] - 1; ++$i) {
  463. ++$columnTo;
  464. }
  465. $sheet->mergeCells($column . $row . ':' . $columnTo . $row);
  466. $column = $columnTo;
  467. } elseif (isset($attributeArray['bgcolor'])) {
  468. $sheet->getStyle($column . $row)->applyFromArray(
  469. [
  470. 'fill' => [
  471. 'fillType' => Fill::FILL_SOLID,
  472. 'color' => ['rgb' => $attributeArray['bgcolor']],
  473. ],
  474. ]
  475. );
  476. }
  477. if (isset($attributeArray['width'])) {
  478. $sheet->getColumnDimension($column)->setWidth($attributeArray['width']);
  479. }
  480. if (isset($attributeArray['height'])) {
  481. $sheet->getRowDimension($row)->setRowHeight($attributeArray['height']);
  482. }
  483. if (isset($attributeArray['align'])) {
  484. $sheet->getStyle($column . $row)->getAlignment()->setHorizontal($attributeArray['align']);
  485. }
  486. if (isset($attributeArray['valign'])) {
  487. $sheet->getStyle($column . $row)->getAlignment()->setVertical($attributeArray['valign']);
  488. }
  489. if (isset($attributeArray['data-format'])) {
  490. $sheet->getStyle($column . $row)->getNumberFormat()->setFormatCode($attributeArray['data-format']);
  491. }
  492. ++$column;
  493. break;
  494. case 'body':
  495. $row = 1;
  496. $column = 'A';
  497. $cellContent = '';
  498. $this->tableLevel = 0;
  499. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  500. break;
  501. default:
  502. $this->processDomElement($child, $sheet, $row, $column, $cellContent);
  503. }
  504. }
  505. }
  506. }
  507. /**
  508. * Loads PhpSpreadsheet from file into PhpSpreadsheet instance.
  509. *
  510. * @param string $pFilename
  511. * @param Spreadsheet $spreadsheet
  512. *
  513. * @throws Exception
  514. *
  515. * @return Spreadsheet
  516. */
  517. public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
  518. {
  519. // Validate
  520. if (!$this->canRead($pFilename)) {
  521. throw new Exception($pFilename . ' is an Invalid HTML file.');
  522. }
  523. // Create new sheet
  524. while ($spreadsheet->getSheetCount() <= $this->sheetIndex) {
  525. $spreadsheet->createSheet();
  526. }
  527. $spreadsheet->setActiveSheetIndex($this->sheetIndex);
  528. // Create a new DOM object
  529. $dom = new DOMDocument();
  530. // Reload the HTML file into the DOM object
  531. $loaded = $dom->loadHTML(mb_convert_encoding($this->securityScanner->scanFile($pFilename), 'HTML-ENTITIES', 'UTF-8'));
  532. if ($loaded === false) {
  533. throw new Exception('Failed to load ' . $pFilename . ' as a DOM Document');
  534. }
  535. // Discard white space
  536. $dom->preserveWhiteSpace = false;
  537. $row = 0;
  538. $column = 'A';
  539. $content = '';
  540. $this->rowspan = [];
  541. $this->processDomElement($dom, $spreadsheet->getActiveSheet(), $row, $column, $content);
  542. // Return
  543. return $spreadsheet;
  544. }
  545. /**
  546. * Get sheet index.
  547. *
  548. * @return int
  549. */
  550. public function getSheetIndex()
  551. {
  552. return $this->sheetIndex;
  553. }
  554. /**
  555. * Set sheet index.
  556. *
  557. * @param int $pValue Sheet index
  558. *
  559. * @return HTML
  560. */
  561. public function setSheetIndex($pValue)
  562. {
  563. $this->sheetIndex = $pValue;
  564. return $this;
  565. }
  566. /**
  567. * Apply inline css inline style.
  568. *
  569. * NOTES :
  570. * Currently only intended for td & th element,
  571. * and only takes 'background-color' and 'color'; property with HEX color
  572. *
  573. * TODO :
  574. * - Implement to other propertie, such as border
  575. *
  576. * @param Worksheet $sheet
  577. * @param int $row
  578. * @param string $column
  579. * @param array $attributeArray
  580. */
  581. private function applyInlineStyle(&$sheet, $row, $column, $attributeArray)
  582. {
  583. if (!isset($attributeArray['style'])) {
  584. return;
  585. }
  586. $cellStyle = $sheet->getStyle($column . $row);
  587. // add color styles (background & text) from dom element,currently support : td & th, using ONLY inline css style with RGB color
  588. $styles = explode(';', $attributeArray['style']);
  589. foreach ($styles as $st) {
  590. $value = explode(':', $st);
  591. $styleName = isset($value[0]) ? trim($value[0]) : null;
  592. $styleValue = isset($value[1]) ? trim($value[1]) : null;
  593. if (!$styleName) {
  594. continue;
  595. }
  596. switch ($styleName) {
  597. case 'background':
  598. case 'background-color':
  599. $styleColor = $this->getStyleColor($styleValue);
  600. if (!$styleColor) {
  601. continue 2;
  602. }
  603. $cellStyle->applyFromArray(['fill' => ['fillType' => Fill::FILL_SOLID, 'color' => ['rgb' => $styleColor]]]);
  604. break;
  605. case 'color':
  606. $styleColor = $this->getStyleColor($styleValue);
  607. if (!$styleColor) {
  608. continue 2;
  609. }
  610. $cellStyle->applyFromArray(['font' => ['color' => ['rgb' => $styleColor]]]);
  611. break;
  612. case 'border':
  613. $this->setBorderStyle($cellStyle, $styleValue, 'allBorders');
  614. break;
  615. case 'border-top':
  616. $this->setBorderStyle($cellStyle, $styleValue, 'top');
  617. break;
  618. case 'border-bottom':
  619. $this->setBorderStyle($cellStyle, $styleValue, 'bottom');
  620. break;
  621. case 'border-left':
  622. $this->setBorderStyle($cellStyle, $styleValue, 'left');
  623. break;
  624. case 'border-right':
  625. $this->setBorderStyle($cellStyle, $styleValue, 'right');
  626. break;
  627. case 'font-size':
  628. $cellStyle->getFont()->setSize(
  629. (float) $styleValue
  630. );
  631. break;
  632. case 'font-weight':
  633. if ($styleValue === 'bold' || $styleValue >= 500) {
  634. $cellStyle->getFont()->setBold(true);
  635. }
  636. break;
  637. case 'font-style':
  638. if ($styleValue === 'italic') {
  639. $cellStyle->getFont()->setItalic(true);
  640. }
  641. break;
  642. case 'font-family':
  643. $cellStyle->getFont()->setName(str_replace('\'', '', $styleValue));
  644. break;
  645. case 'text-decoration':
  646. switch ($styleValue) {
  647. case 'underline':
  648. $cellStyle->getFont()->setUnderline(Font::UNDERLINE_SINGLE);
  649. break;
  650. case 'line-through':
  651. $cellStyle->getFont()->setStrikethrough(true);
  652. break;
  653. }
  654. break;
  655. case 'text-align':
  656. $cellStyle->getAlignment()->setHorizontal($styleValue);
  657. break;
  658. case 'vertical-align':
  659. $cellStyle->getAlignment()->setVertical($styleValue);
  660. break;
  661. case 'width':
  662. $sheet->getColumnDimension($column)->setWidth(
  663. str_replace('px', '', $styleValue)
  664. );
  665. break;
  666. case 'height':
  667. $sheet->getRowDimension($row)->setRowHeight(
  668. str_replace('px', '', $styleValue)
  669. );
  670. break;
  671. case 'word-wrap':
  672. $cellStyle->getAlignment()->setWrapText(
  673. $styleValue === 'break-word'
  674. );
  675. break;
  676. case 'text-indent':
  677. $cellStyle->getAlignment()->setIndent(
  678. (int) str_replace(['px'], '', $styleValue)
  679. );
  680. break;
  681. }
  682. }
  683. }
  684. /**
  685. * Check if has #, so we can get clean hex.
  686. *
  687. * @param $value
  688. *
  689. * @return null|string
  690. */
  691. public function getStyleColor($value)
  692. {
  693. if (strpos($value, '#') === 0) {
  694. return substr($value, 1);
  695. }
  696. return null;
  697. }
  698. /**
  699. * @param Worksheet $sheet
  700. * @param string $column
  701. * @param int $row
  702. * @param array $attributes
  703. *
  704. * @throws \PhpOffice\PhpSpreadsheet\Exception
  705. */
  706. private function insertImage(Worksheet $sheet, $column, $row, array $attributes)
  707. {
  708. if (!isset($attributes['src'])) {
  709. return;
  710. }
  711. $src = urldecode($attributes['src']);
  712. $width = isset($attributes['width']) ? (float) $attributes['width'] : null;
  713. $height = isset($attributes['height']) ? (float) $attributes['height'] : null;
  714. $name = isset($attributes['alt']) ? (float) $attributes['alt'] : null;
  715. $drawing = new Drawing();
  716. $drawing->setPath($src);
  717. $drawing->setWorksheet($sheet);
  718. $drawing->setCoordinates($column . $row);
  719. $drawing->setOffsetX(0);
  720. $drawing->setOffsetY(10);
  721. $drawing->setResizeProportional(true);
  722. if ($name) {
  723. $drawing->setName($name);
  724. }
  725. if ($width) {
  726. $drawing->setWidth((int) $width);
  727. }
  728. if ($height) {
  729. $drawing->setHeight((int) $height);
  730. }
  731. $sheet->getColumnDimension($column)->setWidth(
  732. $drawing->getWidth() / 6
  733. );
  734. $sheet->getRowDimension($row)->setRowHeight(
  735. $drawing->getHeight() * 0.9
  736. );
  737. }
  738. /**
  739. * Map html border style to PhpSpreadsheet border style.
  740. *
  741. * @param string $style
  742. *
  743. * @return null|string
  744. */
  745. public function getBorderStyle($style)
  746. {
  747. switch ($style) {
  748. case 'solid':
  749. return Border::BORDER_THIN;
  750. case 'dashed':
  751. return Border::BORDER_DASHED;
  752. case 'dotted':
  753. return Border::BORDER_DOTTED;
  754. case 'medium':
  755. return Border::BORDER_MEDIUM;
  756. case 'thick':
  757. return Border::BORDER_THICK;
  758. case 'none':
  759. return Border::BORDER_NONE;
  760. case 'dash-dot':
  761. return Border::BORDER_DASHDOT;
  762. case 'dash-dot-dot':
  763. return Border::BORDER_DASHDOTDOT;
  764. case 'double':
  765. return Border::BORDER_DOUBLE;
  766. case 'hair':
  767. return Border::BORDER_HAIR;
  768. case 'medium-dash-dot':
  769. return Border::BORDER_MEDIUMDASHDOT;
  770. case 'medium-dash-dot-dot':
  771. return Border::BORDER_MEDIUMDASHDOTDOT;
  772. case 'medium-dashed':
  773. return Border::BORDER_MEDIUMDASHED;
  774. case 'slant-dash-dot':
  775. return Border::BORDER_SLANTDASHDOT;
  776. }
  777. return null;
  778. }
  779. /**
  780. * @param Style $cellStyle
  781. * @param string $styleValue
  782. * @param string $type
  783. */
  784. private function setBorderStyle(Style $cellStyle, $styleValue, $type)
  785. {
  786. list(, $borderStyle, $color) = explode(' ', $styleValue);
  787. $cellStyle->applyFromArray([
  788. 'borders' => [
  789. $type => [
  790. 'borderStyle' => $this->getBorderStyle($borderStyle),
  791. 'color' => ['rgb' => $this->getStyleColor($color)],
  792. ],
  793. ],
  794. ]);
  795. }
  796. }