132 lines
3.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\Exception\Exception;
2014-05-04 21:03:28 +04:00
use PhpOffice\PhpWord\PhpWord;
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);
$this->parts = array('Head', 'Body');
foreach ($this->parts as $partName) {
$partClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Part\\' . $partName;
if (class_exists($partClass)) {
/** @var \PhpOffice\PhpWord\Writer\HTML\Part\AbstractPart $part Type hint */
$part = new $partClass();
$part->setParentWriter($this);
$this->writerParts[strtolower($partName)] = $part;
}
}
2014-04-12 10:12:24 +07:00
}
/**
* Save PhpWord to file
*
* @param string $filename
2014-05-15 14:41:08 +07:00
* @throws \PhpOffice\PhpWord\Exception\Exception
2014-04-12 10:12:24 +07:00
*/
public function save($filename = null)
{
2014-05-07 09:47:02 +07:00
$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
{
$content = '';
$content .= '<!DOCTYPE html>' . PHP_EOL;
$content .= '<!-- Generated by PHPWord -->' . PHP_EOL;
$content .= '<html>' . PHP_EOL;
$content .= $this->getWriterPart('Head')->write();
$content .= $this->getWriterPart('Body')->write();
$content .= '</html>' . PHP_EOL;
2014-04-12 10:12:24 +07:00
return $content;
2014-04-12 10:12:24 +07:00
}
/**
* 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
}