HTML Writer: Enable footnotes and endnotes

This commit is contained in:
Ivan Lanin 2014-04-16 17:17:42 +07:00
parent 1ed13cc036
commit 52b3506bbb
3 changed files with 92 additions and 18 deletions

View File

@ -32,7 +32,7 @@ This release marked heavy refactorings on internal code structure with the creat
- ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198 - ListItem: Ability to create custom list and reset list number - @ivanlanin GH-10 GH-198
- ODT Writer: Basic table writing support - @ivanlanin - ODT Writer: Basic table writing support - @ivanlanin
- Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194 - Image: Keep image aspect ratio if only 1 dimension styled - @japonicus GH-194
- HTML Writer: Basic HTML writer: text, textrun, link, title, textbreak, table, image (as Base64) - @ivanlanin GH-203 GH-67 GH-147 - HTML Writer: Basic HTML writer: text, textrun, link, title, textbreak, table, image (as Base64), footnote, endnote - @ivanlanin GH-203 GH-67 GH-147
- PDF Writer: Basic PDF writer using DomPDF: All HTML element except image - @ivanlanin GH-68 - PDF Writer: Basic PDF writer using DomPDF: All HTML element except image - @ivanlanin GH-68
### Bugfixes ### Bugfixes

View File

@ -101,9 +101,9 @@ Writers
+ +-----------------------+--------+-------+-------+-------+-------+ + +-----------------------+--------+-------+-------+-------+-------+
| | Footer | ✓ | | | | | | | Footer | ✓ | | | | |
+ +-----------------------+--------+-------+-------+-------+-------+ + +-----------------------+--------+-------+-------+-------+-------+
| | Footnote | ✓ | | | | | | | Footnote | ✓ | | | | |
+ +-----------------------+--------+-------+-------+-------+-------+ + +-----------------------+--------+-------+-------+-------+-------+
| | Endnote | ✓ | | | | | | | Endnote | ✓ | | | | |
+-------------------------+-----------------------+--------+-------+-------+-------+-------+ +-------------------------+-----------------------+--------+-------+-------+-------+-------+
| **Graphs** | 2D basic graphs | | | | | | | **Graphs** | 2D basic graphs | | | | | |
+ +-----------------------+--------+-------+-------+-------+-------+ + +-----------------------+--------+-------+-------+-------+-------+

View File

@ -22,7 +22,9 @@ use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextBreak; use PhpOffice\PhpWord\Element\TextBreak;
use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Element\Title; use PhpOffice\PhpWord\Element\Title;
use PhpOffice\PhpWord\Endnotes;
use PhpOffice\PhpWord\Exception\Exception; use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Footnotes;
use PhpOffice\PhpWord\PhpWord; use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Style; use PhpOffice\PhpWord\Style;
use PhpOffice\PhpWord\Style\Font; use PhpOffice\PhpWord\Style\Font;
@ -42,6 +44,13 @@ class HTML extends AbstractWriter implements WriterInterface
*/ */
protected $isPdf = false; protected $isPdf = false;
/**
* Footnotes and endnotes collection
*
* @var array
*/
protected $notes = array();
/** /**
* Create new instance * Create new instance
*/ */
@ -85,6 +94,7 @@ class HTML extends AbstractWriter implements WriterInterface
$html .= '</head>' . PHP_EOL; $html .= '</head>' . PHP_EOL;
$html .= '<body>' . PHP_EOL; $html .= '<body>' . PHP_EOL;
$html .= $this->writeHTMLBody(); $html .= $this->writeHTMLBody();
$html .= $this->writeNotes();
$html .= '</body>' . PHP_EOL; $html .= '</body>' . PHP_EOL;
$html .= '</html>' . PHP_EOL; $html .= '</html>' . PHP_EOL;
@ -181,6 +191,32 @@ class HTML extends AbstractWriter implements WriterInterface
return $html; return $html;
} }
/**
* Write footnote/endnote contents
*/
private function writeNotes()
{
$footnote = Footnotes::getElements();
$endnote = Endnotes::getElements();
$html = '';
if (count($this->notes) > 0) {
$html .= "<hr />";
foreach ($this->notes as $noteId => $noteMark) {
$noteAnchor = "note-{$noteId}";
list($noteType, $noteTypeId) = explode('-', $noteMark);
$collection = $$noteType;
if (array_key_exists($noteTypeId, $collection)) {
$element = $collection[$noteTypeId];
$content = "<a href=\"#{$noteMark}\" class=\"NoteRef\"><sup>{$noteId}</sup></a>" . $this->writeTextRun($element, true);
$html .= "<p><a name=\"{$noteAnchor}\" />{$content}</p>" . PHP_EOL;
}
}
}
return $html;
}
/** /**
* Get text * Get text
* *
@ -226,19 +262,20 @@ class HTML extends AbstractWriter implements WriterInterface
} }
/** /**
* Get text run content * Write text run content
* *
* @param TextRun $textrun * @param TextRun|Footnote|Endnote $textrun
* @return string * @return string
*/ */
private function writeTextRun($textrun) private function writeTextRun($textrun, $withoutP = false)
{ {
$html = ''; $html = '';
$elements = $textrun->getElements(); $elements = $textrun->getElements();
if (count($elements) > 0) { if (count($elements) > 0) {
$paragraphStyle = $textrun->getParagraphStyle(); $paragraphStyle = $textrun->getParagraphStyle();
$spIsObject = ($paragraphStyle instanceof Paragraph); $spIsObject = ($paragraphStyle instanceof Paragraph);
$html .= '<p';
$html .= $withoutP ? '<span' : '<p';
if ($paragraphStyle) { if ($paragraphStyle) {
if (!$spIsObject) { if (!$spIsObject) {
$html .= ' class="' . $paragraphStyle . '"'; $html .= ' class="' . $paragraphStyle . '"';
@ -262,7 +299,8 @@ class HTML extends AbstractWriter implements WriterInterface
$html .= $this->writeFootnote($element); $html .= $this->writeFootnote($element);
} }
} }
$html .= '</p>' . PHP_EOL; $html .= $withoutP ? '</span>' : '</p>';
$html .= PHP_EOL;
} }
return $html; return $html;
@ -279,11 +317,14 @@ class HTML extends AbstractWriter implements WriterInterface
{ {
$url = $element->getLinkSrc(); $url = $element->getLinkSrc();
$text = $element->getLinkName(); $text = $element->getLinkName();
if ($text == '') {
$text = $url;
}
$html = ''; $html = '';
if (!$withoutP) { if (!$withoutP) {
$html .= "<p>" . PHP_EOL; $html .= "<p>" . PHP_EOL;
} }
$html .= "<a href=\"{$url}'\">{$text}</a>" . PHP_EOL; $html .= "<a href=\"{$url}\">{$text}</a>" . PHP_EOL;
if (!$withoutP) { if (!$withoutP) {
$html .= "</p>" . PHP_EOL; $html .= "</p>" . PHP_EOL;
} }
@ -467,7 +508,7 @@ class HTML extends AbstractWriter implements WriterInterface
*/ */
private function writeFootnote($element) private function writeFootnote($element)
{ {
return $this->writeUnsupportedElement($element, true); return $this->writeNote($element);
} }
/** /**
@ -478,7 +519,25 @@ class HTML extends AbstractWriter implements WriterInterface
*/ */
private function writeEndnote($element) private function writeEndnote($element)
{ {
return $this->writeUnsupportedElement($element, true); return $this->writeNote($element);
}
/**
* Write footnote/endnote marks
*
* @param Footnote|Endnote $element
* @return string
*/
private function writeNote($element)
{
$index = count($this->notes) + 1;
$prefix = ($element instanceof Endnote) ? 'endnote' : 'footnote';
$noteMark = $prefix . '-' . $element->getRelationId();
$noteAnchor = "note-{$index}";
$this->notes[$index] = $noteMark;
$html = "<a name=\"{$noteMark}\"><a href=\"#{$noteAnchor}\" class=\"NoteRef\"><sup>{$index}</sup></a>";
return $html;
} }
/** /**
@ -509,18 +568,33 @@ class HTML extends AbstractWriter implements WriterInterface
*/ */
private function writeStyles() private function writeStyles()
{ {
$bodyCss = array();
$css = '<style>' . PHP_EOL; $css = '<style>' . PHP_EOL;
// Default styles // Default styles
$bodyCss['font-family'] = "'" . $this->getPhpWord()->getDefaultFontName() . "'"; $defaultStyles = array(
$bodyCss['font-size'] = $this->getPhpWord()->getDefaultFontSize() . 'pt'; '*' => array(
$css .= '* ' . $this->assembleCss($bodyCss, true) . PHP_EOL; '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) {
$css .= $selector . ' ' . $this->assembleCss($style, true) . PHP_EOL;
}
// Custom styles // Custom styles
$styles = Style::getStyles(); $customStyles = Style::getStyles();
if (is_array($styles)) { if (is_array($customStyles)) {
foreach ($styles as $name => $style) { foreach ($customStyles as $name => $style) {
if ($style instanceof Font) { if ($style instanceof Font) {
if ($style->getStyleType() == 'title') { if ($style->getStyleType() == 'title') {
$name = str_replace('Heading_', 'h', $name); $name = str_replace('Heading_', 'h', $name);