setPhpWord($phpWord);
}
/**
* Save PhpWord to file
*
* @param string $filename
* @throws Exception
*/
public function save($filename = null)
{
if (!is_null($this->getPhpWord())) {
$this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/');
$hFile = fopen($filename, 'w');
if ($hFile !== false) {
fwrite($hFile, $this->writeDocument());
fclose($hFile);
} else {
throw new Exception("Can't open file");
}
$this->clearTempDir();
} else {
throw new Exception("No PHPWord assigned.");
}
}
/**
* Get phpWord data
*
* @return string
*/
public function writeDocument()
{
$html = '';
$html .= '' . PHP_EOL;
$html .= '' . PHP_EOL;
$html .= '' . PHP_EOL;
$html .= '
' . PHP_EOL;
$html .= $this->writeHTMLHead();
$html .= '' . PHP_EOL;
$html .= '' . PHP_EOL;
$html .= $this->writeHTMLBody();
$html .= $this->writeNotes();
$html .= '' . PHP_EOL;
$html .= '' . PHP_EOL;
return $html;
}
/**
* Generate HTML header
*
* @return string
*/
private function writeHTMLHead()
{
$properties = $this->getPhpWord()->getDocumentProperties();
$propertiesMapping = array(
'creator' => 'author',
'title' => '',
'description' => '',
'subject' => '',
'keywords' => '',
'category' => '',
'company' => '',
'manager' => ''
);
$title = $properties->getTitle();
$title = ($title != '') ? $title : 'PHPWord';
$html = '';
$html .= '' . PHP_EOL;
$html .= '' . htmlspecialchars($title) . '' . PHP_EOL;
foreach ($propertiesMapping as $key => $value) {
$value = ($value == '') ? $key : $value;
$method = "get" . $key;
if ($properties->$method() != '') {
$html .= '' . PHP_EOL;
}
}
$html .= $this->writeStyles();
return $html;
}
/**
* Get content
*
* @return string
*/
private function writeHTMLBody()
{
$phpWord = $this->getPhpWord();
$html = '';
$sections = $phpWord->getSections();
$countSections = count($sections);
if ($countSections > 0) {
foreach ($sections as $section) {
$elements = $section->getElements();
foreach ($elements as $element) {
if ($element instanceof AbstractElement) {
$elementWriter = new ElementWriter($this, $element, false);
$html .= $elementWriter->write();
}
}
}
}
return $html;
}
/**
* Write footnote/endnote contents as textruns
*/
private function writeNotes()
{
$html = '';
if (!empty($this->notes)) {
$html .= "
";
foreach ($this->notes as $noteId => $noteMark) {
$noteAnchor = "note-{$noteId}";
list($noteType, $noteTypeId) = explode('-', $noteMark);
$collectionObject = 'PhpOffice\\PhpWord\\' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
$collection = $collectionObject::getElements();
if (array_key_exists($noteTypeId, $collection)) {
$element = $collection[$noteTypeId];
$elmWriter = new TextRunWriter($this, $element, true);
$content = "{$noteId}" . $elmWriter->write();
$html .= "{$content}
" . PHP_EOL;
}
}
}
return $html;
}
/**
* Get styles
*
* @return string
*/
private function writeStyles()
{
$css = '' . PHP_EOL;
return $css;
}
/**
* Get is PDF
*
* @return bool
*/
public function isPdf()
{
return $this->isPdf;
}
/**
* Get notes
*
* @return array
*/
public function getNotes()
{
return $this->notes;
}
/**
* Add note
*
* @param int $noteId
* @param string $noteMark
*/
public function addNote($noteId, $noteMark)
{
$this->notes[$noteId] = $noteMark;
}
}