Skip to content

Commit

Permalink
PHPOffice#196: Ability to add links and page breaks in RTF
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanlanin committed May 12, 2014
1 parent f8f98cc commit 4b1a160
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
- Table: Ability to add table inside a cell (nested table) - @ivanlanin GH-149
- RTF: UTF8 support for RTF: Internal UTF8 text is converted to Unicode before writing - @ivanlanin GH-158
- Table: Ability to define table width (in percent and twip) and position - @ivanlanin GH-237
- RTF: Ability to add links and page breaks in RTF - @ivanlanin GH-196

### Bugfixes

Expand Down
4 changes: 2 additions & 2 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ Writers
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Title || | |||
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Link ||| |||
| | Link ||| |||
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Preserve Text || | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Text Break ||||||
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | Page Break || | | | |
| | Page Break || | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
| | List || | | | |
+---------------------------+----------------------+--------+-------+-------+--------+-------+
Expand Down
4 changes: 2 additions & 2 deletions docs/src/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ Below are the supported features for each file formats.
| **Element Type** | Text ||||||
| | Text Run ||||||
| | Title || | |||
| | Link ||| |||
| | Link ||| |||
| | Preserve Text || | | | |
| | Text Break ||||||
| | Page Break || | | | |
| | Page Break || | | | |
| | List || | | | |
| | Table ||| |||
| | Image ||| || |
Expand Down
6 changes: 4 additions & 2 deletions samples/Sample_01_SimpleText.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
$section->addText('I am styled by a font style definition.', 'rStyle');
$section->addText('I am styled by a paragraph style definition.', null, 'pStyle');
$section->addText('I am styled by both font and paragraph style.', 'rStyle', 'pStyle');
$section->addTextBreak();

$section->addPageBreak();

// Inline font style
$fontStyle['name'] = 'Times New Roman';
Expand All @@ -36,10 +37,11 @@
$fontStyle['fgColor'] = 'yellow';
$fontStyle['smallCaps'] = true;
$section->addText('I am inline styled.', $fontStyle);

$section->addTextBreak();

// Link
$section->addLink('http://www.google.com', null, 'NLink');
$section->addLink('http://www.google.com', 'Google');
$section->addTextBreak();

// Image
Expand Down
54 changes: 54 additions & 0 deletions src/PhpWord/Writer/RTF/Element/Link.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\RTF\Element;

use PhpOffice\PhpWord\Shared\String;

/**
* Link element RTF writer
*
* @since 0.11.0
*/
class Link extends AbstractElement
{
/**
* Write element
*
* @return string
*/
public function write()
{
$element = $this->element;
if (!$element instanceof \PhpOffice\PhpWord\Element\Link) {
return;
}

$content = '';
if (!$this->withoutP) {
$content .= '{';
}
$content .= '{\field {\*\fldinst {HYPERLINK "' . $element->getTarget() . '"}}{\\fldrslt {';
$content .= String::toUnicode($element->getText());
$content .= '}}}';
if (!$this->withoutP) {
$content .= '}';
}

return $content;
}
}
36 changes: 36 additions & 0 deletions src/PhpWord/Writer/RTF/Element/PageBreak.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2014 PHPWord contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpWord\Writer\RTF\Element;

/**
* PageBreak element RTF writer
*
* @since 0.11.0
*/
class PageBreak extends AbstractElement
{
/**
* Write element
*
* @return string
*/
public function write()
{
return '\page' . PHP_EOL;
}
}
2 changes: 1 addition & 1 deletion src/PhpWord/Writer/RTF/Element/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function write()

$content = '';

$content .= '\pard\nowidctlpar';
$content .= '\pard\nowidctlpar ';
$content .= String::toUnicode($this->element->getText());
$content .= '\par' . PHP_EOL;

Expand Down
2 changes: 1 addition & 1 deletion tests/PhpWord/Tests/Writer/RTF/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ElementTest extends \PHPUnit_Framework_TestCase
*/
public function testUnmatchedElements()
{
$styles = array('Container', 'Text', 'Title');
$styles = array('Container', 'Text', 'Title', 'Link');
foreach ($styles as $style) {
$objectClass = 'PhpOffice\\PhpWord\\Writer\\RTF\\Element\\' . $style;
$parentWriter = new RTF();
Expand Down

0 comments on commit 4b1a160

Please sign in to comment.