Refactor: Apply composite design pattern to HTML writer
This commit is contained in:
parent
03ff0fb7cd
commit
324994137f
|
|
@ -13,26 +13,18 @@ use PhpOffice\PhpWord\Endnotes;
|
|||
use PhpOffice\PhpWord\Footnotes;
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
use PhpOffice\PhpWord\Style;
|
||||
use PhpOffice\PhpWord\Element\Endnote;
|
||||
use PhpOffice\PhpWord\Element\Footnote;
|
||||
use PhpOffice\PhpWord\Element\Image;
|
||||
use PhpOffice\PhpWord\Element\Link;
|
||||
use PhpOffice\PhpWord\Element\ListItem;
|
||||
use PhpOffice\PhpWord\Element\Object;
|
||||
use PhpOffice\PhpWord\Element\PageBreak;
|
||||
use PhpOffice\PhpWord\Element\PreserveText;
|
||||
use PhpOffice\PhpWord\Element\Table;
|
||||
use PhpOffice\PhpWord\Element\Text;
|
||||
use PhpOffice\PhpWord\Element\TextBreak;
|
||||
use PhpOffice\PhpWord\Element\TextRun;
|
||||
use PhpOffice\PhpWord\Element\Title;
|
||||
use PhpOffice\PhpWord\Element\AbstractElement;
|
||||
use PhpOffice\PhpWord\Exception\Exception;
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
use PhpOffice\PhpWord\Writer\HTML\Element\Element as ElementWriter;
|
||||
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
|
||||
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
|
||||
|
||||
/**
|
||||
* HTML writer
|
||||
*
|
||||
* Not supported: PreserveText, PageBreak, Object
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class HTML extends AbstractWriter implements WriterInterface
|
||||
|
|
@ -154,39 +146,15 @@ class HTML extends AbstractWriter implements WriterInterface
|
|||
|
||||
$sections = $phpWord->getSections();
|
||||
$countSections = count($sections);
|
||||
$pSection = 0;
|
||||
|
||||
if ($countSections > 0) {
|
||||
foreach ($sections as $section) {
|
||||
$pSection++;
|
||||
$contents = $section->getElements();
|
||||
foreach ($contents as $element) {
|
||||
if ($element instanceof Text) {
|
||||
$html .= $this->writeText($element);
|
||||
} elseif ($element instanceof TextRun) {
|
||||
$html .= $this->writeTextRun($element);
|
||||
} elseif ($element instanceof Link) {
|
||||
$html .= $this->writeLink($element);
|
||||
} elseif ($element instanceof Title) {
|
||||
$html .= $this->writeTitle($element);
|
||||
} elseif ($element instanceof PreserveText) {
|
||||
$html .= $this->writePreserveText($element);
|
||||
} elseif ($element instanceof TextBreak) {
|
||||
$html .= $this->writeTextBreak($element);
|
||||
} elseif ($element instanceof PageBreak) {
|
||||
$html .= $this->writePageBreak($element);
|
||||
} elseif ($element instanceof Table) {
|
||||
$html .= $this->writeTable($element);
|
||||
} elseif ($element instanceof ListItem) {
|
||||
$html .= $this->writeListItem($element);
|
||||
} elseif ($element instanceof Image) {
|
||||
$html .= $this->writeImage($element);
|
||||
} elseif ($element instanceof Object) {
|
||||
$html .= $this->writeObject($element);
|
||||
} elseif ($element instanceof Endnote) {
|
||||
$html .= $this->writeEndnote($element);
|
||||
} elseif ($element instanceof Footnote) {
|
||||
$html .= $this->writeFootnote($element);
|
||||
$elements = $section->getElements();
|
||||
foreach ($elements as $element) {
|
||||
if ($element instanceof AbstractElement) {
|
||||
$elementWriter = new ElementWriter($element, false);
|
||||
$elementWriter->setParentWriter($this);
|
||||
$html .= $elementWriter->write();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -196,7 +164,7 @@ class HTML extends AbstractWriter implements WriterInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* Write footnote/endnote contents
|
||||
* Write footnote/endnote contents as textruns
|
||||
*/
|
||||
private function writeNotes()
|
||||
{
|
||||
|
|
@ -204,7 +172,7 @@ class HTML extends AbstractWriter implements WriterInterface
|
|||
$endnote = Endnotes::getElements();
|
||||
$html = '';
|
||||
|
||||
if (count($this->notes) > 0) {
|
||||
if (!empty($this->notes)) {
|
||||
$html .= "<hr />";
|
||||
foreach ($this->notes as $noteId => $noteMark) {
|
||||
$noteAnchor = "note-{$noteId}";
|
||||
|
|
@ -212,7 +180,9 @@ class HTML extends AbstractWriter implements WriterInterface
|
|||
$collection = $$noteType;
|
||||
if (array_key_exists($noteTypeId, $collection)) {
|
||||
$element = $collection[$noteTypeId];
|
||||
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>" . $this->writeTextRun($element, true);
|
||||
$elmWriter = new TextRunWriter($element, true);
|
||||
$elmWriter->setParentWriter($this);
|
||||
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>" . $elmWriter->write();
|
||||
$html .= "<p><a name=\"{$noteAnchor}\" />{$content}</p>" . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
|
@ -221,351 +191,6 @@ class HTML extends AbstractWriter implements WriterInterface
|
|||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Text $text
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writeText($text, $withoutP = false)
|
||||
{
|
||||
$html = '';
|
||||
$paragraphStyle = $text->getParagraphStyle();
|
||||
$spIsObject = ($paragraphStyle instanceof Paragraph);
|
||||
$fontStyle = $text->getFontStyle();
|
||||
$sfIsObject = ($fontStyle instanceof Font);
|
||||
|
||||
if ($paragraphStyle && !$withoutP) {
|
||||
$html .= '<p';
|
||||
if (!$spIsObject) {
|
||||
$html .= ' class="' . $paragraphStyle . '"';
|
||||
} else {
|
||||
$html .= ' style="' . $this->writeParagraphStyle($paragraphStyle) . '"';
|
||||
}
|
||||
$html .= '>';
|
||||
}
|
||||
if ($fontStyle) {
|
||||
$html .= '<span';
|
||||
if (!$sfIsObject) {
|
||||
$html .= ' class="' . $fontStyle . '"';
|
||||
} else {
|
||||
$html .= ' style="' . $this->writeFontStyle($fontStyle) . '"';
|
||||
}
|
||||
$html .= '>';
|
||||
}
|
||||
$html .= htmlspecialchars($text->getText());
|
||||
if ($fontStyle) {
|
||||
$html .= '</span>';
|
||||
}
|
||||
if ($paragraphStyle && !$withoutP) {
|
||||
$html .= '</p>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write text run content
|
||||
*
|
||||
* @param mixed $textrun
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writeTextRun($textrun, $withoutP = false)
|
||||
{
|
||||
$html = '';
|
||||
$elements = $textrun->getElements();
|
||||
if (count($elements) > 0) {
|
||||
$paragraphStyle = $textrun->getParagraphStyle();
|
||||
$spIsObject = ($paragraphStyle instanceof Paragraph);
|
||||
|
||||
$html .= $withoutP ? '<span' : '<p';
|
||||
if ($paragraphStyle) {
|
||||
if (!$spIsObject) {
|
||||
$html .= ' class="' . $paragraphStyle . '"';
|
||||
} else {
|
||||
$html .= ' style="' . $this->writeParagraphStyle($paragraphStyle) . '"';
|
||||
}
|
||||
}
|
||||
$html .= '>';
|
||||
foreach ($elements as $element) {
|
||||
if ($element instanceof Text) {
|
||||
$html .= $this->writeText($element, true);
|
||||
} elseif ($element instanceof Link) {
|
||||
$html .= $this->writeLink($element, true);
|
||||
} elseif ($element instanceof TextBreak) {
|
||||
$html .= $this->writeTextBreak($element, true);
|
||||
} elseif ($element instanceof Image) {
|
||||
$html .= $this->writeImage($element, true);
|
||||
} elseif ($element instanceof Endnote) {
|
||||
$html .= $this->writeEndnote($element);
|
||||
} elseif ($element instanceof Footnote) {
|
||||
$html .= $this->writeFootnote($element);
|
||||
}
|
||||
}
|
||||
$html .= $withoutP ? '</span>' : '</p>';
|
||||
$html .= PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write link
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Link $element
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writeLink($element, $withoutP = false)
|
||||
{
|
||||
$url = $element->getLinkSrc();
|
||||
$text = $element->getLinkName();
|
||||
if ($text == '') {
|
||||
$text = $url;
|
||||
}
|
||||
$html = '';
|
||||
if (!$withoutP) {
|
||||
$html .= "<p>" . PHP_EOL;
|
||||
}
|
||||
$html .= "<a href=\"{$url}\">{$text}</a>" . PHP_EOL;
|
||||
if (!$withoutP) {
|
||||
$html .= "</p>" . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write heading
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Title $element
|
||||
* @return string
|
||||
*/
|
||||
private function writeTitle($element)
|
||||
{
|
||||
$tag = 'h' . $element->getDepth();
|
||||
$text = htmlspecialchars($element->getText());
|
||||
$html = "<{$tag}>{$text}</{$tag}>" . PHP_EOL;
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write preserve text
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\PreserveText $element
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writePreserveText($element, $withoutP = false)
|
||||
{
|
||||
return $this->writeUnsupportedElement($element, $withoutP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get text break
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\TextBreak $element
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writeTextBreak($element, $withoutP = false)
|
||||
{
|
||||
if ($withoutP) {
|
||||
$html = '<br />' . PHP_EOL;
|
||||
} else {
|
||||
$html = '<p> </p>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write page break
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\PageBreak $element
|
||||
* @return string
|
||||
*/
|
||||
private function writePageBreak($element)
|
||||
{
|
||||
return $this->writeUnsupportedElement($element, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write list item
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\ListItem $element
|
||||
* @return string
|
||||
*/
|
||||
private function writeListItem($element)
|
||||
{
|
||||
$text = htmlspecialchars($element->getTextObject()->getText());
|
||||
$html = '<p>' . $text . '</{$p}>' . PHP_EOL;
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write table
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Table $element
|
||||
* @return string
|
||||
*/
|
||||
private function writeTable($element)
|
||||
{
|
||||
$html = '';
|
||||
$rows = $element->getRows();
|
||||
$cRows = count($rows);
|
||||
if ($cRows > 0) {
|
||||
$html .= "<table>" . PHP_EOL;
|
||||
foreach ($rows as $row) {
|
||||
// $height = $row->getHeight();
|
||||
$rowStyle = $row->getStyle();
|
||||
$tblHeader = $rowStyle->getTblHeader();
|
||||
$html .= "<tr>" . PHP_EOL;
|
||||
foreach ($row->getCells() as $cell) {
|
||||
$cellTag = $tblHeader ? 'th' : 'td';
|
||||
$cellContents = $cell->getElements();
|
||||
$html .= "<{$cellTag}>" . PHP_EOL;
|
||||
if (count($cellContents) > 0) {
|
||||
foreach ($cellContents as $content) {
|
||||
if ($content instanceof Text) {
|
||||
$html .= $this->writeText($content);
|
||||
} elseif ($content instanceof TextRun) {
|
||||
$html .= $this->writeTextRun($content);
|
||||
} elseif ($content instanceof Link) {
|
||||
$html .= $this->writeLink($content);
|
||||
} elseif ($content instanceof PreserveText) {
|
||||
$html .= $this->writePreserveText($content);
|
||||
} elseif ($content instanceof TextBreak) {
|
||||
$html .= $this->writeTextBreak($content);
|
||||
} elseif ($content instanceof ListItem) {
|
||||
$html .= $this->writeListItem($content);
|
||||
} elseif ($content instanceof Image) {
|
||||
$html .= $this->writeImage($content);
|
||||
} elseif ($content instanceof Object) {
|
||||
$html .= $this->writeObject($content);
|
||||
} elseif ($element instanceof Endnote) {
|
||||
$html .= $this->writeEndnote($element);
|
||||
} elseif ($element instanceof Footnote) {
|
||||
$html .= $this->writeFootnote($element);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$html .= $this->writeTextBreak(new TextBreak());
|
||||
}
|
||||
$html .= "</td>" . PHP_EOL;
|
||||
}
|
||||
$html .= "</tr>" . PHP_EOL;
|
||||
}
|
||||
$html .= "</table>" . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write image
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Image $element
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writeImage($element, $withoutP = false)
|
||||
{
|
||||
$html = $this->writeUnsupportedElement($element, $withoutP);
|
||||
if (!$this->isPdf) {
|
||||
$imageData = $this->getBase64ImageData($element);
|
||||
if (!is_null($imageData)) {
|
||||
$style = $this->assembleCss(array(
|
||||
'width' => $element->getStyle()->getWidth() . 'px',
|
||||
'height' => $element->getStyle()->getHeight() . 'px',
|
||||
));
|
||||
$html = "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
|
||||
if (!$withoutP) {
|
||||
$html = "<p>{$html}</p>" . PHP_EOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write object
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Object $element
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writeObject($element, $withoutP = false)
|
||||
{
|
||||
return $this->writeUnsupportedElement($element, $withoutP);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write footnote
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Footnote $element
|
||||
* @return string
|
||||
*/
|
||||
private function writeFootnote($element)
|
||||
{
|
||||
return $this->writeNote($element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write endnote
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Element\Endnote $element
|
||||
* @return string
|
||||
*/
|
||||
private function writeEndnote($element)
|
||||
{
|
||||
return $this->writeNote($element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write footnote/endnote marks
|
||||
*
|
||||
* @param mixed $element
|
||||
* @return string
|
||||
*/
|
||||
private function writeNote($element)
|
||||
{
|
||||
$index = count($this->notes) + 1;
|
||||
$prefix = ($element instanceof Endnote) ? 'endnote' : 'footnote';
|
||||
$noteMark = $prefix . '-' . $element->getRelationId();
|
||||
$noteAnchor = "note-{$index}";
|
||||
$this->notes[$index] = $noteMark;
|
||||
$html = "<a name=\"{$noteMark}\"><a href=\"#{$noteAnchor}\" class=\"NoteRef\"><sup>{$index}</sup></a>";
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write unsupported element
|
||||
*
|
||||
* @param mixed $element
|
||||
* @param boolean $withoutP
|
||||
* @return string
|
||||
*/
|
||||
private function writeUnsupportedElement($element, $withoutP = false)
|
||||
{
|
||||
$elementClass = get_class($element);
|
||||
$elementMark = str_replace('PhpOffice\\PhpWord\\Element\\', '', $elementClass);
|
||||
$elementMark = htmlentities("<{$elementMark}>");
|
||||
if ($withoutP) {
|
||||
$html = "<span class=\"other-elm\">{$elementMark}</span>" . PHP_EOL;
|
||||
} else {
|
||||
$html = "<p>{$elementMark}</p>" . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get styles
|
||||
*
|
||||
|
|
@ -593,23 +218,26 @@ class HTML extends AbstractWriter implements WriterInterface
|
|||
),
|
||||
);
|
||||
foreach ($defaultStyles as $selector => $style) {
|
||||
$css .= $selector . ' ' . $this->assembleCss($style, true) . PHP_EOL;
|
||||
$styleWriter = new StyleWriter();
|
||||
$css .= $selector . ' ';
|
||||
$css .= $styleWriter->assembleCss($style, true) . PHP_EOL;
|
||||
}
|
||||
|
||||
// Custom styles
|
||||
$customStyles = Style::getStyles();
|
||||
if (is_array($customStyles)) {
|
||||
foreach ($customStyles as $name => $style) {
|
||||
$styleWriter = new StyleWriter($this, $style, true);
|
||||
if ($style instanceof Font) {
|
||||
if ($style->getStyleType() == 'title') {
|
||||
$name = str_replace('Heading_', 'h', $name);
|
||||
} else {
|
||||
$name = '.' . $name;
|
||||
}
|
||||
$css .= "{$name} " . $this->writeFontStyle($style, true) . PHP_EOL;
|
||||
$css .= "{$name} " . $styleWriter->write() . PHP_EOL;
|
||||
} elseif ($style instanceof Paragraph) {
|
||||
$name = '.' . $name;
|
||||
$css .= "{$name} " . $this->writeParagraphStyle($style, true) . PHP_EOL;
|
||||
$css .= "{$name} " . $styleWriter->write() . PHP_EOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -619,140 +247,33 @@ class HTML extends AbstractWriter implements WriterInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* Get font style
|
||||
* Get is PDF
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Style\Font $style
|
||||
* @param boolean $curlyBracket
|
||||
* @return string
|
||||
* @return bool
|
||||
*/
|
||||
private function writeFontStyle($style, $curlyBracket = false)
|
||||
public function isPdf()
|
||||
{
|
||||
$css = array();
|
||||
if (PHPWord::DEFAULT_FONT_NAME != $style->getName()) {
|
||||
$css['font-family'] = "'" . $style->getName() . "'";
|
||||
}
|
||||
if (PHPWord::DEFAULT_FONT_SIZE != $style->getSize()) {
|
||||
$css['font-size'] = $style->getSize() . 'pt';
|
||||
}
|
||||
if (PHPWord::DEFAULT_FONT_COLOR != $style->getColor()) {
|
||||
$css['color'] = '#' . $style->getColor();
|
||||
}
|
||||
$css['background'] = $style->getFgColor();
|
||||
if ($style->getBold()) {
|
||||
$css['font-weight'] = 'bold';
|
||||
}
|
||||
if ($style->getItalic()) {
|
||||
$css['font-style'] = 'italic';
|
||||
}
|
||||
if ($style->getSuperScript()) {
|
||||
$css['vertical-align'] = 'super';
|
||||
} elseif ($style->getSubScript()) {
|
||||
$css['vertical-align'] = 'sub';
|
||||
}
|
||||
$css['text-decoration'] = '';
|
||||
if ($style->getUnderline() != Font::UNDERLINE_NONE) {
|
||||
$css['text-decoration'] .= 'underline ';
|
||||
}
|
||||
if ($style->getStrikethrough()) {
|
||||
$css['text-decoration'] .= 'line-through ';
|
||||
}
|
||||
|
||||
return $this->assembleCss($css, $curlyBracket);
|
||||
return $this->isPdf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get paragraph style
|
||||
* Get notes
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Style\Paragraph $style
|
||||
* @param boolean $curlyBracket
|
||||
* @return string
|
||||
* @return array
|
||||
*/
|
||||
private function writeParagraphStyle($style, $curlyBracket = false)
|
||||
public function getNotes()
|
||||
{
|
||||
$css = array();
|
||||
if ($style->getAlign()) {
|
||||
$css['text-align'] = $style->getAlign();
|
||||
}
|
||||
|
||||
return $this->assembleCss($css, $curlyBracket);
|
||||
return $this->notes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes array where of CSS properties / values and converts to CSS string
|
||||
* Add note
|
||||
*
|
||||
* @param array $css
|
||||
* @param boolean $curlyBracket
|
||||
* @return string
|
||||
* @param int $noteId
|
||||
* @param string $noteMark
|
||||
*/
|
||||
private function assembleCss($css, $curlyBracket = false)
|
||||
public function addNote($noteId, $noteMark)
|
||||
{
|
||||
$pairs = array();
|
||||
foreach ($css as $key => $value) {
|
||||
if ($value != '') {
|
||||
$pairs[] = $key . ': ' . $value;
|
||||
}
|
||||
}
|
||||
$string = implode('; ', $pairs);
|
||||
if ($curlyBracket) {
|
||||
$string = '{ ' . $string . ' }';
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Base64 image data
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getBase64ImageData(Image $element)
|
||||
{
|
||||
$source = $element->getSource();
|
||||
$imageType = $element->getImageType();
|
||||
$imageData = null;
|
||||
$imageBinary = null;
|
||||
$actualSource = null;
|
||||
|
||||
// Get actual source from archive image or other source
|
||||
// Return null if not found
|
||||
if ($element->getSourceType() == Image::SOURCE_ARCHIVE) {
|
||||
$source = substr($source, 6);
|
||||
list($zipFilename, $imageFilename) = explode('#', $source);
|
||||
|
||||
$zipClass = \PhpOffice\PhpWord\Settings::getZipClass();
|
||||
$zip = new $zipClass();
|
||||
if ($zip->open($zipFilename) !== false) {
|
||||
if ($zip->locateName($imageFilename)) {
|
||||
$zip->extractTo($this->getTempDir(), $imageFilename);
|
||||
$actualSource = $this->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
} else {
|
||||
$actualSource = $source;
|
||||
}
|
||||
if (is_null($actualSource)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Read image binary data and convert into Base64
|
||||
if ($element->getSourceType() == Image::SOURCE_GD) {
|
||||
$imageResource = call_user_func($element->getImageCreateFunction(), $actualSource);
|
||||
ob_start();
|
||||
call_user_func($element->getImageFunction(), $imageResource);
|
||||
$imageBinary = ob_get_contents();
|
||||
ob_end_clean();
|
||||
} else {
|
||||
if ($fileHandle = fopen($actualSource, 'rb', false)) {
|
||||
$imageBinary = fread($fileHandle, filesize($actualSource));
|
||||
fclose($fileHandle);
|
||||
}
|
||||
}
|
||||
if (!is_null($imageBinary)) {
|
||||
$base64 = chunk_split(base64_encode($imageBinary));
|
||||
$imageData = 'data:' . $imageType . ';base64,' . $base64;
|
||||
}
|
||||
|
||||
return $imageData;
|
||||
$this->notes[$noteId] = $noteMark;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,101 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Element\AbstractElement;
|
||||
use PhpOffice\PhpWord\Writer\HTML;
|
||||
|
||||
/**
|
||||
* Generic element HTML writer
|
||||
*
|
||||
* Section: Text, TextRun, Link, Title, PreserveText, TextBreak, PageBreak, Table, ListItem, Image, Object, Endnote, Footnote
|
||||
* Cell: Text, TextRun, Link, PreserveText, TextBreak, ListItem, Image, Object, Endnote, Footnote
|
||||
* TextRun: Text, Link, TextBreak, Image, Endnote, Footnote
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Element
|
||||
{
|
||||
/**
|
||||
* Parent writer
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\Writer\HTML
|
||||
*/
|
||||
protected $parentWriter;
|
||||
|
||||
/**
|
||||
* Element
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\Element\AbstractElement
|
||||
*/
|
||||
protected $element;
|
||||
|
||||
/**
|
||||
* Without paragraph
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $withoutP = false;
|
||||
|
||||
/**
|
||||
* Create new instance
|
||||
*
|
||||
* @param bool $withoutP
|
||||
*/
|
||||
public function __construct(AbstractElement $element, $withoutP = false)
|
||||
{
|
||||
$this->element = $element;
|
||||
$this->withoutP = $withoutP;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write element
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$html = '';
|
||||
$elmName = str_replace('PhpOffice\\PhpWord\\Element\\', '', get_class($this->element));
|
||||
$elmWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Element\\' . $elmName;
|
||||
if (class_exists($elmWriterClass) === true) {
|
||||
$elmWriter = new $elmWriterClass($this->element, $this->withoutP);
|
||||
$elmWriter->setParentWriter($this->parentWriter);
|
||||
$html = $elmWriter->write();
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set parent writer
|
||||
*
|
||||
* @param \PhpOffice\PhpWord\Writer\HTML $pWriter
|
||||
*/
|
||||
public function setParentWriter(HTML $writer)
|
||||
{
|
||||
$this->parentWriter = $writer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get parent writer
|
||||
*
|
||||
* @return \PhpOffice\PhpWord\Writer\HTML
|
||||
* @throws \PhpOffice\PhpWord\Exception\Exception
|
||||
*/
|
||||
public function getParentWriter()
|
||||
{
|
||||
if (!is_null($this->parentWriter)) {
|
||||
return $this->parentWriter;
|
||||
} else {
|
||||
throw new Exception("No parent HTML Writer assigned.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* Endnote element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Endnote extends Note
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* Footnote element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Footnote extends Note
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Element\Image as ImageElement;
|
||||
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
|
||||
|
||||
/**
|
||||
* Image element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Image extends Element
|
||||
{
|
||||
/**
|
||||
* Write image
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
if (!$this->getParentWriter()->isPdf()) {
|
||||
$imageData = $this->getBase64ImageData($this->element);
|
||||
if (!is_null($imageData)) {
|
||||
$styleWriter = new StyleWriter();
|
||||
$style = $styleWriter->assembleCss(array(
|
||||
'width' => $this->element->getStyle()->getWidth() . 'px',
|
||||
'height' => $this->element->getStyle()->getHeight() . 'px',
|
||||
));
|
||||
$html = "<img border=\"0\" style=\"{$style}\" src=\"{$imageData}\"/>";
|
||||
if (!$this->withoutP) {
|
||||
$html = "<p>{$html}</p>" . PHP_EOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Base64 image data
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
private function getBase64ImageData(ImageElement $element)
|
||||
{
|
||||
$source = $element->getSource();
|
||||
$imageType = $element->getImageType();
|
||||
$imageData = null;
|
||||
$imageBinary = null;
|
||||
$actualSource = null;
|
||||
|
||||
// Get actual source from archive image or other source
|
||||
// Return null if not found
|
||||
if ($element->getSourceType() == ImageElement::SOURCE_ARCHIVE) {
|
||||
$source = substr($source, 6);
|
||||
list($zipFilename, $imageFilename) = explode('#', $source);
|
||||
|
||||
$zipClass = \PhpOffice\PhpWord\Settings::getZipClass();
|
||||
$zip = new $zipClass();
|
||||
if ($zip->open($zipFilename) !== false) {
|
||||
if ($zip->locateName($imageFilename)) {
|
||||
$zip->extractTo($this->getParentWriter()->getTempDir(), $imageFilename);
|
||||
$actualSource = $this->getParentWriter()->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
|
||||
}
|
||||
}
|
||||
$zip->close();
|
||||
} else {
|
||||
$actualSource = $source;
|
||||
}
|
||||
if (is_null($actualSource)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Read image binary data and convert into Base64
|
||||
if ($element->getSourceType() == ImageElement::SOURCE_GD) {
|
||||
$imageResource = call_user_func($element->getImageCreateFunction(), $actualSource);
|
||||
ob_start();
|
||||
call_user_func($element->getImageFunction(), $imageResource);
|
||||
$imageBinary = ob_get_contents();
|
||||
ob_end_clean();
|
||||
} else {
|
||||
if ($fileHandle = fopen($actualSource, 'rb', false)) {
|
||||
$imageBinary = fread($fileHandle, filesize($actualSource));
|
||||
fclose($fileHandle);
|
||||
}
|
||||
}
|
||||
if (!is_null($imageBinary)) {
|
||||
$base64 = chunk_split(base64_encode($imageBinary));
|
||||
$imageData = 'data:' . $imageType . ';base64,' . $base64;
|
||||
}
|
||||
|
||||
return $imageData;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* Link element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Link extends Element
|
||||
{
|
||||
/**
|
||||
* Write link
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$url = $this->element->getLinkSrc();
|
||||
$text = $this->element->getLinkName();
|
||||
if ($text == '') {
|
||||
$text = $url;
|
||||
}
|
||||
$html = '';
|
||||
if (!$this->withoutP) {
|
||||
$html .= "<p>" . PHP_EOL;
|
||||
}
|
||||
$html .= "<a href=\"{$url}\">{$text}</a>" . PHP_EOL;
|
||||
if (!$this->withoutP) {
|
||||
$html .= "</p>" . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* ListItem element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class ListItem extends Element
|
||||
{
|
||||
/**
|
||||
* Write list item
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$text = htmlspecialchars($this->element->getTextObject()->getText());
|
||||
$html = '<p>' . $text . '</{$p}>' . PHP_EOL;
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Element\Endnote;
|
||||
use PhpOffice\PhpWord\Element\Footnote;
|
||||
|
||||
/**
|
||||
* Note element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Note extends Element
|
||||
{
|
||||
/**
|
||||
* Write footnote/endnote marks
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$noteId = count($this->getParentWriter()->getNotes()) + 1;
|
||||
$prefix = ($this->element instanceof Endnote) ? 'endnote' : 'footnote';
|
||||
$noteMark = $prefix . '-' . $this->element->getRelationId();
|
||||
$this->getParentWriter()->addNote($noteId, $noteMark);
|
||||
$html = "<a name=\"{$noteMark}\"><a href=\"#note-{$noteId}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* PageBreak element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class PageBreak extends TextBreak
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* Table element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Table extends Element
|
||||
{
|
||||
/**
|
||||
* Write table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$html = '';
|
||||
$rows = $this->element->getRows();
|
||||
$rowCount = count($rows);
|
||||
if ($rowCount > 0) {
|
||||
$html .= '<table>' . PHP_EOL;
|
||||
foreach ($rows as $row) {
|
||||
// $height = $row->getHeight();
|
||||
$rowStyle = $row->getStyle();
|
||||
$tblHeader = $rowStyle->getTblHeader();
|
||||
$html .= '<tr>' . PHP_EOL;
|
||||
foreach ($row->getCells() as $cell) {
|
||||
$cellTag = $tblHeader ? 'th' : 'td';
|
||||
$cellContents = $cell->getElements();
|
||||
$html .= "<{$cellTag}>" . PHP_EOL;
|
||||
if (count($cellContents) > 0) {
|
||||
foreach ($cellContents as $content) {
|
||||
$writer = new Element($content, false);
|
||||
$writer->setParentWriter($this->parentWriter);
|
||||
$html .= $writer->write();
|
||||
}
|
||||
} else {
|
||||
$writer = new Element(new \PhpOffice\PhpWord\Element\TextBreak(), false);
|
||||
$writer->setParentWriter($this->parentWriter);
|
||||
$html .= $writer->write();
|
||||
}
|
||||
$html .= '</td>' . PHP_EOL;
|
||||
}
|
||||
$html .= '</tr>' . PHP_EOL;
|
||||
}
|
||||
$html .= '</table>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Style\Font;
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
|
||||
|
||||
/**
|
||||
* Text element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Text extends Element
|
||||
{
|
||||
/**
|
||||
* Write text
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$html = '';
|
||||
// Paragraph style
|
||||
$pStyle = $this->element->getParagraphStyle();
|
||||
$pStyleIsObject = ($pStyle instanceof Paragraph);
|
||||
if ($pStyleIsObject) {
|
||||
$styleWriter = new StyleWriter($this->parentWriter, $pStyle);
|
||||
$pStyle = $styleWriter->write();
|
||||
}
|
||||
// Font style
|
||||
$fStyle = $this->element->getFontStyle();
|
||||
$fStyleIsObject = ($fStyle instanceof Font);
|
||||
if ($fStyleIsObject) {
|
||||
$styleWriter = new StyleWriter($this->parentWriter, $fStyle);
|
||||
$fStyle = $styleWriter->write();
|
||||
}
|
||||
|
||||
if ($pStyle && !$this->withoutP) {
|
||||
$attribute = $pStyleIsObject ? 'style' : 'class';
|
||||
$html .= "<p {$attribute}=\"{$pStyle}\">";
|
||||
}
|
||||
if ($fStyle) {
|
||||
$attribute = $fStyleIsObject ? 'style' : 'class';
|
||||
$html .= "<span {$attribute}=\"{$fStyle}\">";
|
||||
}
|
||||
$html .= htmlspecialchars($this->element->getText());
|
||||
if ($fStyle) {
|
||||
$html .= '</span>';
|
||||
}
|
||||
if ($pStyle && !$this->withoutP) {
|
||||
$html .= '</p>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* TextBreak element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class TextBreak extends Element
|
||||
{
|
||||
/**
|
||||
* Write text break
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
if ($this->withoutP) {
|
||||
$html = '<br />' . PHP_EOL;
|
||||
} else {
|
||||
$html = '<p> </p>' . PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
use PhpOffice\PhpWord\Style\Paragraph;
|
||||
use PhpOffice\PhpWord\Writer\HTML\Style\Style as StyleWriter;
|
||||
|
||||
/**
|
||||
* TextRun element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class TextRun extends Element
|
||||
{
|
||||
/**
|
||||
* Write text run
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$html = '';
|
||||
$elements = $this->element->getElements();
|
||||
if (count($elements) > 0) {
|
||||
// Paragraph style
|
||||
$pStyle = $this->element->getParagraphStyle();
|
||||
$pStyleIsObject = ($pStyle instanceof Paragraph);
|
||||
if ($pStyleIsObject) {
|
||||
$styleWriter = new StyleWriter($this->parentWriter, $pStyle);
|
||||
$pStyle = $styleWriter->write();
|
||||
}
|
||||
$tag = $this->withoutP ? 'span' : 'p';
|
||||
$attribute = $pStyleIsObject ? 'style' : 'class';
|
||||
$html .= "<{$tag} {$attribute}=\"{$pStyle}\">";
|
||||
foreach ($elements as $element) {
|
||||
$elementWriter = new Element($element, true);
|
||||
$elementWriter->setParentWriter($this->parentWriter);
|
||||
$html .= $elementWriter->write();
|
||||
}
|
||||
$html .= "</{$tag}>";
|
||||
$html .= PHP_EOL;
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||
|
||||
/**
|
||||
* TextRun element HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Title extends Element
|
||||
{
|
||||
/**
|
||||
* Write heading
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$tag = 'h' . $this->element->getDepth();
|
||||
$text = htmlspecialchars($this->element->getText());
|
||||
$html = "<{$tag}>{$text}</{$tag}>" . PHP_EOL;
|
||||
|
||||
return $html;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Style;
|
||||
|
||||
use PhpOffice\PhpWord\PhpWord;
|
||||
|
||||
/**
|
||||
* Font style HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Font extends Style
|
||||
{
|
||||
/**
|
||||
* Write style
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$css = array();
|
||||
if (PhpWord::DEFAULT_FONT_NAME != $this->style->getName()) {
|
||||
$css['font-family'] = "'" . $this->style->getName() . "'";
|
||||
}
|
||||
if (PhpWord::DEFAULT_FONT_SIZE != $this->style->getSize()) {
|
||||
$css['font-size'] = $this->style->getSize() . 'pt';
|
||||
}
|
||||
if (PhpWord::DEFAULT_FONT_COLOR != $this->style->getColor()) {
|
||||
$css['color'] = '#' . $this->style->getColor();
|
||||
}
|
||||
$css['background'] = $this->style->getFgColor();
|
||||
if ($this->style->getBold()) {
|
||||
$css['font-weight'] = 'bold';
|
||||
}
|
||||
if ($this->style->getItalic()) {
|
||||
$css['font-style'] = 'italic';
|
||||
}
|
||||
if ($this->style->getSuperScript()) {
|
||||
$css['vertical-align'] = 'super';
|
||||
} elseif ($this->style->getSubScript()) {
|
||||
$css['vertical-align'] = 'sub';
|
||||
}
|
||||
$css['text-decoration'] = '';
|
||||
if ($this->style->getUnderline() != \PhpOffice\PhpWord\Style\Font::UNDERLINE_NONE) {
|
||||
$css['text-decoration'] .= 'underline ';
|
||||
}
|
||||
if ($this->style->getStrikethrough()) {
|
||||
$css['text-decoration'] .= 'line-through ';
|
||||
}
|
||||
|
||||
return $this->assembleCss($css, $this->curlyBracket);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Style;
|
||||
|
||||
/**
|
||||
* Paragraph style HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Paragraph extends Style
|
||||
{
|
||||
/**
|
||||
* Write style
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$css = array();
|
||||
if ($this->style->getAlign()) {
|
||||
$css['text-align'] = $this->style->getAlign();
|
||||
}
|
||||
|
||||
return $this->assembleCss($css, $this->curlyBracket);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
/**
|
||||
* PHPWord
|
||||
*
|
||||
* @link https://github.com/PHPOffice/PHPWord
|
||||
* @copyright 2014 PHPWord
|
||||
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
|
||||
*/
|
||||
|
||||
namespace PhpOffice\PhpWord\Writer\HTML\Style;
|
||||
|
||||
use PhpOffice\PhpWord\Style\AbstractStyle;
|
||||
use PhpOffice\PhpWord\Writer\HTML;
|
||||
|
||||
/**
|
||||
* Style HTML writer
|
||||
*
|
||||
* @since 0.10.0
|
||||
*/
|
||||
class Style
|
||||
{
|
||||
/**
|
||||
* Parent writer
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\Writer\HTML
|
||||
*/
|
||||
protected $parentWriter;
|
||||
|
||||
/**
|
||||
* Style
|
||||
*
|
||||
* @var \PhpOffice\PhpWord\Style\AbstractStyle
|
||||
*/
|
||||
protected $style;
|
||||
|
||||
/**
|
||||
* Curly bracket
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $curlyBracket = false;
|
||||
|
||||
/**
|
||||
* Create new instance
|
||||
*
|
||||
* @param bool $curlyBracket
|
||||
*/
|
||||
public function __construct(HTML $parentWriter = null, AbstractStyle $style = null, $curlyBracket = false)
|
||||
{
|
||||
$this->parentWriter = $parentWriter;
|
||||
$this->style = $style;
|
||||
$this->curlyBracket = $curlyBracket;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write element
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
$css = '';
|
||||
$styleType = str_replace('PhpOffice\\PhpWord\\Style\\', '', get_class($this->style));
|
||||
$styleWriterClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Style\\' . $styleType;
|
||||
if (class_exists($styleWriterClass) === true) {
|
||||
$styleWriter = new $styleWriterClass($this->parentWriter, $this->style, $this->curlyBracket);
|
||||
$css = $styleWriter->write();
|
||||
}
|
||||
|
||||
return $css;
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes array where of CSS properties / values and converts to CSS string
|
||||
*
|
||||
* @param array $css
|
||||
* @param boolean $curlyBracket
|
||||
* @return string
|
||||
*/
|
||||
public function assembleCss($css, $curlyBracket = false)
|
||||
{
|
||||
$pairs = array();
|
||||
foreach ($css as $key => $value) {
|
||||
if ($value != '') {
|
||||
$pairs[] = $key . ': ' . $value;
|
||||
}
|
||||
}
|
||||
$string = implode('; ', $pairs);
|
||||
if ($curlyBracket) {
|
||||
$string = '{ ' . $string . ' }';
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue