2014-04-12 10:12:24 +07:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-05-05 13:06:53 +04:00
|
|
|
* This file is part of PHPWord - A pure PHP library for reading and writing
|
|
|
|
|
* word processing documents.
|
|
|
|
|
*
|
|
|
|
|
* PHPWord is free software distributed under the terms of the GNU Lesser
|
|
|
|
|
* General Public License version 3 as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* For the full copyright and license information, please read the LICENSE
|
|
|
|
|
* file that was distributed with this source code. For the full list of
|
|
|
|
|
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
|
2014-04-12 10:12:24 +07:00
|
|
|
*
|
|
|
|
|
* @link https://github.com/PHPOffice/PHPWord
|
2014-05-05 12:38:31 +04:00
|
|
|
* @copyright 2010-2014 PHPWord contributors
|
2014-05-04 21:03:28 +04:00
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
|
2014-04-12 10:12:24 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpWord\Writer;
|
|
|
|
|
|
2014-04-24 14:58:15 +07:00
|
|
|
use PhpOffice\PhpWord\Element\AbstractElement;
|
2014-04-12 10:12:24 +07:00
|
|
|
use PhpOffice\PhpWord\Exception\Exception;
|
2014-05-04 21:03:28 +04:00
|
|
|
use PhpOffice\PhpWord\PhpWord;
|
2014-04-12 10:12:24 +07:00
|
|
|
use PhpOffice\PhpWord\Style\Font;
|
|
|
|
|
use PhpOffice\PhpWord\Style\Paragraph;
|
2014-05-05 13:06:53 +04:00
|
|
|
use PhpOffice\PhpWord\Style;
|
2014-04-24 14:58:15 +07:00
|
|
|
use PhpOffice\PhpWord\Writer\HTML\Element\Element as ElementWriter;
|
|
|
|
|
use PhpOffice\PhpWord\Writer\HTML\Element\TextRun as TextRunWriter;
|
2014-04-26 11:14:27 +07:00
|
|
|
use PhpOffice\PhpWord\Writer\HTML\Style\Font as FontStyleWriter;
|
2014-05-04 21:03:28 +04:00
|
|
|
use PhpOffice\PhpWord\Writer\HTML\Style\Generic as GenericStyleWriter;
|
2014-04-26 11:14:27 +07:00
|
|
|
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph as ParagraphStyleWriter;
|
2014-04-12 10:12:24 +07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* HTML writer
|
|
|
|
|
*
|
2014-04-24 14:58:15 +07:00
|
|
|
* Not supported: PreserveText, PageBreak, Object
|
2014-04-12 10:12:24 +07:00
|
|
|
* @since 0.10.0
|
|
|
|
|
*/
|
|
|
|
|
class HTML extends AbstractWriter implements WriterInterface
|
|
|
|
|
{
|
2014-04-16 12:12:32 +07:00
|
|
|
/**
|
|
|
|
|
* Is the current writer creating PDF?
|
|
|
|
|
*
|
|
|
|
|
* @var boolean
|
|
|
|
|
*/
|
|
|
|
|
protected $isPdf = false;
|
|
|
|
|
|
2014-04-16 17:17:42 +07:00
|
|
|
/**
|
|
|
|
|
* Footnotes and endnotes collection
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $notes = array();
|
|
|
|
|
|
2014-04-12 10:12:24 +07:00
|
|
|
/**
|
|
|
|
|
* Create new instance
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(PhpWord $phpWord = null)
|
|
|
|
|
{
|
|
|
|
|
$this->setPhpWord($phpWord);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Save PhpWord to file
|
|
|
|
|
*
|
|
|
|
|
* @param string $filename
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function save($filename = null)
|
|
|
|
|
{
|
2014-05-07 09:47:02 +07:00
|
|
|
if (is_null($this->phpWord)) {
|
|
|
|
|
throw new Exception('PhpWord object unassigned.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->setTempDir(sys_get_temp_dir() . '/PHPWordWriter/');
|
|
|
|
|
$hFile = fopen($filename, 'w');
|
|
|
|
|
if ($hFile !== false) {
|
|
|
|
|
fwrite($hFile, $this->writeDocument());
|
|
|
|
|
fclose($hFile);
|
2014-04-12 10:12:24 +07:00
|
|
|
} else {
|
2014-05-07 09:47:02 +07:00
|
|
|
throw new Exception("Can't open file");
|
2014-04-12 10:12:24 +07:00
|
|
|
}
|
2014-05-07 09:47:02 +07:00
|
|
|
$this->clearTempDir();
|
2014-04-12 10:12:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get phpWord data
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-04-13 23:17:39 +07:00
|
|
|
public function writeDocument()
|
2014-04-12 10:12:24 +07:00
|
|
|
{
|
|
|
|
|
$html = '';
|
|
|
|
|
$html .= '<!DOCTYPE html>' . PHP_EOL;
|
|
|
|
|
$html .= '<!-- Generated by PHPWord -->' . PHP_EOL;
|
|
|
|
|
$html .= '<html>' . PHP_EOL;
|
|
|
|
|
$html .= '<head>' . PHP_EOL;
|
|
|
|
|
$html .= $this->writeHTMLHead();
|
|
|
|
|
$html .= '</head>' . PHP_EOL;
|
|
|
|
|
$html .= '<body>' . PHP_EOL;
|
|
|
|
|
$html .= $this->writeHTMLBody();
|
2014-04-16 17:17:42 +07:00
|
|
|
$html .= $this->writeNotes();
|
2014-04-12 10:12:24 +07:00
|
|
|
$html .= '</body>' . PHP_EOL;
|
|
|
|
|
$html .= '</html>' . PHP_EOL;
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate HTML header
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-04-13 23:17:39 +07:00
|
|
|
private function writeHTMLHead()
|
2014-04-12 10:12:24 +07:00
|
|
|
{
|
|
|
|
|
$properties = $this->getPhpWord()->getDocumentProperties();
|
|
|
|
|
$propertiesMapping = array(
|
|
|
|
|
'creator' => 'author',
|
|
|
|
|
'title' => '',
|
|
|
|
|
'description' => '',
|
|
|
|
|
'subject' => '',
|
|
|
|
|
'keywords' => '',
|
|
|
|
|
'category' => '',
|
|
|
|
|
'company' => '',
|
|
|
|
|
'manager' => ''
|
|
|
|
|
);
|
|
|
|
|
$title = $properties->getTitle();
|
|
|
|
|
$title = ($title != '') ? $title : 'PHPWord';
|
|
|
|
|
|
|
|
|
|
$html = '';
|
|
|
|
|
$html .= '<meta charset="UTF-8" />' . PHP_EOL;
|
|
|
|
|
$html .= '<title>' . htmlspecialchars($title) . '</title>' . PHP_EOL;
|
|
|
|
|
foreach ($propertiesMapping as $key => $value) {
|
|
|
|
|
$value = ($value == '') ? $key : $value;
|
|
|
|
|
$method = "get" . $key;
|
|
|
|
|
if ($properties->$method() != '') {
|
|
|
|
|
$html .= '<meta name="' . $value . '" content="' .
|
|
|
|
|
htmlspecialchars($properties->$method()) . '" />' . PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$html .= $this->writeStyles();
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get content
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-04-13 23:17:39 +07:00
|
|
|
private function writeHTMLBody()
|
2014-04-12 10:12:24 +07:00
|
|
|
{
|
|
|
|
|
$phpWord = $this->getPhpWord();
|
|
|
|
|
$html = '';
|
|
|
|
|
|
|
|
|
|
$sections = $phpWord->getSections();
|
|
|
|
|
$countSections = count($sections);
|
|
|
|
|
|
|
|
|
|
if ($countSections > 0) {
|
|
|
|
|
foreach ($sections as $section) {
|
2014-04-24 14:58:15 +07:00
|
|
|
$elements = $section->getElements();
|
|
|
|
|
foreach ($elements as $element) {
|
|
|
|
|
if ($element instanceof AbstractElement) {
|
2014-04-25 18:56:19 +07:00
|
|
|
$elementWriter = new ElementWriter($this, $element, false);
|
2014-04-24 14:58:15 +07:00
|
|
|
$html .= $elementWriter->write();
|
2014-04-12 10:12:24 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-16 17:17:42 +07:00
|
|
|
/**
|
2014-04-24 14:58:15 +07:00
|
|
|
* Write footnote/endnote contents as textruns
|
2014-04-16 17:17:42 +07:00
|
|
|
*/
|
|
|
|
|
private function writeNotes()
|
|
|
|
|
{
|
2014-05-04 00:57:44 +07:00
|
|
|
$phpWord = $this->getPhpWord();
|
2014-04-16 17:17:42 +07:00
|
|
|
$html = '';
|
2014-05-04 00:57:44 +07:00
|
|
|
|
2014-04-24 14:58:15 +07:00
|
|
|
if (!empty($this->notes)) {
|
2014-04-16 17:17:42 +07:00
|
|
|
$html .= "<hr />";
|
|
|
|
|
foreach ($this->notes as $noteId => $noteMark) {
|
|
|
|
|
$noteAnchor = "note-{$noteId}";
|
|
|
|
|
list($noteType, $noteTypeId) = explode('-', $noteMark);
|
2014-05-04 00:57:44 +07:00
|
|
|
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
|
|
|
|
|
$collection = $phpWord->$method()->getItems();
|
2014-04-16 17:17:42 +07:00
|
|
|
if (array_key_exists($noteTypeId, $collection)) {
|
|
|
|
|
$element = $collection[$noteTypeId];
|
2014-04-25 18:56:19 +07:00
|
|
|
$elmWriter = new TextRunWriter($this, $element, true);
|
2014-05-06 08:50:55 +07:00
|
|
|
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>";
|
|
|
|
|
$content .= $elmWriter->write();
|
2014-04-16 17:17:42 +07:00
|
|
|
$html .= "<p><a name=\"{$noteAnchor}\" />{$content}</p>" . PHP_EOL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-12 10:12:24 +07:00
|
|
|
/**
|
|
|
|
|
* Get styles
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function writeStyles()
|
|
|
|
|
{
|
|
|
|
|
$css = '<style>' . PHP_EOL;
|
2014-04-13 23:17:39 +07:00
|
|
|
|
2014-04-12 10:12:24 +07:00
|
|
|
// Default styles
|
2014-04-16 17:17:42 +07:00
|
|
|
$defaultStyles = array(
|
|
|
|
|
'*' => array(
|
|
|
|
|
'font-family' => $this->getPhpWord()->getDefaultFontName(),
|
|
|
|
|
'font-size' => $this->getPhpWord()->getDefaultFontSize() . 'pt',
|
|
|
|
|
),
|
|
|
|
|
'a.NoteRef' => array(
|
|
|
|
|
'text-decoration' => 'none',
|
|
|
|
|
),
|
|
|
|
|
'hr' => array(
|
|
|
|
|
'height' => '1px',
|
|
|
|
|
'padding' => '0',
|
|
|
|
|
'margin' => '1em 0',
|
|
|
|
|
'border' => '0',
|
|
|
|
|
'border-top' => '1px solid #CCC',
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
foreach ($defaultStyles as $selector => $style) {
|
2014-04-26 11:14:27 +07:00
|
|
|
$styleWriter = new GenericStyleWriter($style);
|
|
|
|
|
$css .= $selector . ' {' . $styleWriter->write() . '}' . PHP_EOL;
|
2014-04-16 17:17:42 +07:00
|
|
|
}
|
2014-04-13 23:17:39 +07:00
|
|
|
|
2014-04-12 10:12:24 +07:00
|
|
|
// Custom styles
|
2014-04-16 17:17:42 +07:00
|
|
|
$customStyles = Style::getStyles();
|
|
|
|
|
if (is_array($customStyles)) {
|
|
|
|
|
foreach ($customStyles as $name => $style) {
|
2014-04-12 10:12:24 +07:00
|
|
|
if ($style instanceof Font) {
|
2014-04-26 11:14:27 +07:00
|
|
|
$styleWriter = new FontStyleWriter($style);
|
2014-04-12 10:12:24 +07:00
|
|
|
if ($style->getStyleType() == 'title') {
|
|
|
|
|
$name = str_replace('Heading_', 'h', $name);
|
|
|
|
|
} else {
|
|
|
|
|
$name = '.' . $name;
|
|
|
|
|
}
|
2014-04-26 11:14:27 +07:00
|
|
|
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
|
2014-04-12 10:12:24 +07:00
|
|
|
} elseif ($style instanceof Paragraph) {
|
2014-04-26 11:14:27 +07:00
|
|
|
$styleWriter = new ParagraphStyleWriter($style);
|
2014-04-12 10:12:24 +07:00
|
|
|
$name = '.' . $name;
|
2014-04-26 11:14:27 +07:00
|
|
|
$css .= "{$name} {" . $styleWriter->write() . '}' . PHP_EOL;
|
2014-04-12 10:12:24 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$css .= '</style>' . PHP_EOL;
|
|
|
|
|
|
|
|
|
|
return $css;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-24 14:58:15 +07:00
|
|
|
* Get is PDF
|
2014-04-12 10:12:24 +07:00
|
|
|
*
|
2014-04-24 14:58:15 +07:00
|
|
|
* @return bool
|
2014-04-12 10:12:24 +07:00
|
|
|
*/
|
2014-04-24 14:58:15 +07:00
|
|
|
public function isPdf()
|
2014-04-12 10:12:24 +07:00
|
|
|
{
|
2014-04-24 14:58:15 +07:00
|
|
|
return $this->isPdf;
|
2014-04-12 10:12:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-24 14:58:15 +07:00
|
|
|
* Get notes
|
2014-04-12 10:12:24 +07:00
|
|
|
*
|
2014-04-24 14:58:15 +07:00
|
|
|
* @return array
|
2014-04-12 10:12:24 +07:00
|
|
|
*/
|
2014-04-24 14:58:15 +07:00
|
|
|
public function getNotes()
|
2014-04-12 10:12:24 +07:00
|
|
|
{
|
2014-04-24 14:58:15 +07:00
|
|
|
return $this->notes;
|
2014-04-12 10:12:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-24 14:58:15 +07:00
|
|
|
* Add note
|
2014-04-12 10:12:24 +07:00
|
|
|
*
|
2014-04-24 14:58:15 +07:00
|
|
|
* @param int $noteId
|
|
|
|
|
* @param string $noteMark
|
2014-04-16 12:12:32 +07:00
|
|
|
*/
|
2014-04-24 14:58:15 +07:00
|
|
|
public function addNote($noteId, $noteMark)
|
2014-04-16 12:12:32 +07:00
|
|
|
{
|
2014-04-24 14:58:15 +07:00
|
|
|
$this->notes[$noteId] = $noteMark;
|
2014-04-16 12:12:32 +07:00
|
|
|
}
|
2014-04-12 10:12:24 +07:00
|
|
|
}
|