286 lines
8.2 KiB
PHP
Raw Normal View History

2014-04-12 10:12:24 +07:00
<?php
/**
* 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
* @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;
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;
use PhpOffice\PhpWord\Style;
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
*
* 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;
/**
* 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)
{
if (!is_null($this->getPhpWord())) {
2014-04-16 13:37:48 +07:00
$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");
}
2014-04-16 12:12:32 +07:00
$this->clearTempDir();
2014-04-12 10:12:24 +07:00
} else {
throw new Exception("No PHPWord assigned.");
}
}
/**
* 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();
$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) {
$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);
$html .= $elementWriter->write();
2014-04-12 10:12:24 +07:00
}
}
}
}
return $html;
}
/**
* Write footnote/endnote contents as textruns
*/
private function writeNotes()
{
$phpWord = $this->getPhpWord();
$html = '';
if (!empty($this->notes)) {
$html .= "<hr />";
foreach ($this->notes as $noteId => $noteMark) {
$noteAnchor = "note-{$noteId}";
list($noteType, $noteTypeId) = explode('-', $noteMark);
$method = 'get' . ($noteType == 'endnote' ? 'Endnotes' : 'Footnotes');
$collection = $phpWord->$method()->getItems();
if (array_key_exists($noteTypeId, $collection)) {
$element = $collection[$noteTypeId];
2014-04-25 18:56:19 +07:00
$elmWriter = new TextRunWriter($this, $element, true);
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>" . $elmWriter->write();
$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
$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-13 23:17:39 +07:00
2014-04-12 10:12:24 +07:00
// Custom styles
$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;
}
/**
* Get is PDF
2014-04-12 10:12:24 +07:00
*
* @return bool
2014-04-12 10:12:24 +07:00
*/
public function isPdf()
2014-04-12 10:12:24 +07:00
{
return $this->isPdf;
2014-04-12 10:12:24 +07:00
}
/**
* Get notes
2014-04-12 10:12:24 +07:00
*
* @return array
2014-04-12 10:12:24 +07:00
*/
public function getNotes()
2014-04-12 10:12:24 +07:00
{
return $this->notes;
2014-04-12 10:12:24 +07:00
}
/**
* Add note
2014-04-12 10:12:24 +07:00
*
* @param int $noteId
* @param string $noteMark
2014-04-16 12:12:32 +07:00
*/
public function addNote($noteId, $noteMark)
2014-04-16 12:12:32 +07:00
{
$this->notes[$noteId] = $noteMark;
2014-04-16 12:12:32 +07:00
}
2014-04-12 10:12:24 +07:00
}