setPhpWord($phpWord);
}
/**
* Save PhpWord to file
*
* @param string $filename
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function save($filename = null)
{
$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();
}
/**
* Get phpWord data
*
* @return string
*/
public function writeDocument()
{
$content = '';
$content .= '' . PHP_EOL;
$content .= '' . PHP_EOL;
$content .= '' . PHP_EOL;
$content .= '
' . PHP_EOL;
$content .= $this->writeHead();
$content .= '' . PHP_EOL;
$content .= '' . PHP_EOL;
$content .= $this->writeBody();
$content .= $this->writeNotes();
$content .= '' . PHP_EOL;
$content .= '' . PHP_EOL;
return $content;
}
/**
* Generate HTML header
*
* @return string
*/
private function writeHead()
{
$phpWord = $this->getPhpWord();
$properties = $phpWord->getDocumentProperties();
$propertiesMapping = array(
'creator' => 'author',
'title' => '',
'description' => '',
'subject' => '',
'keywords' => '',
'category' => '',
'company' => '',
'manager' => ''
);
$title = $properties->getTitle();
$title = ($title != '') ? $title : 'PHPWord';
$content = '';
$content .= '' . PHP_EOL;
$content .= '' . htmlspecialchars($title) . '' . PHP_EOL;
foreach ($propertiesMapping as $key => $value) {
$value = ($value == '') ? $key : $value;
$method = "get" . $key;
if ($properties->$method() != '') {
$content .= '' . PHP_EOL;
}
}
$content .= $this->writeStyles();
return $content;
}
/**
* Get content
*
* @return string
*/
private function writeBody()
{
$phpWord = $this->getPhpWord();
$content = '';
$sections = $phpWord->getSections();
$countSections = count($sections);
if ($countSections > 0) {
foreach ($sections as $section) {
$writer = new Container($this, $section);
$content .= $writer->write();
}
}
return $content;
}
/**
* Get styles
*
* @return string
*/
private function writeStyles()
{
$css = '' . PHP_EOL;
return $css;
}
/**
* Write footnote/endnote contents as textruns
*/
private function writeNotes()
{
$phpWord = $this->getPhpWord();
$content = PHP_EOL;
if (!empty($this->notes)) {
$content .= "
" . PHP_EOL;
foreach ($this->notes as $noteId => $noteMark) {
list($noteType, $noteTypeId) = explode('-', $noteMark);
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
$collection = $phpWord->$method()->getItems();
if (array_key_exists($noteTypeId, $collection)) {
$element = $collection[$noteTypeId];
$noteAnchor = "";
$noteAnchor .= "{$noteId}";
$writer = new TextRunWriter($this, $element);
$writer->setOpeningText($noteAnchor);
$content .= $writer->write();
}
}
}
return $content;
}
/**
* 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;
}
}