Skip to content

Basic RTF Reader #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers

## 0.11.0 - Not yet released

This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Three new elements were added: TextBox, ListItemRun, and Field. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted.
This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Three new elements were added: TextBox, ListItemRun, and Field. Relative and absolute positioning for images and textboxes were added. Writer classes were refactored into parts, elements, and styles. ODT and RTF features were enhanced. Ability to add elements to PHPWord object via HTML were implemeted. RTF reader were initiated.

### Features

Expand All @@ -30,6 +30,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3. Three
- RTF Writer: Ability to write document properties - @ivanlanin
- RTF Writer: Ability to write image - @ivanlanin
- Element: New `Field` element - @basjan GH-251
- RTF Reader: Basic RTF reader - @ivanlanin GH-72

### Bugfixes

Expand Down
2 changes: 1 addition & 1 deletion docs/elements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Inline style examples:
$textrun = $section->addTextRun();
$textrun->addText('I am bold', array('bold' => true));
$textrun->addText('I am italic', array('italic' => true));
$textrun->addText('I am colored, array('color' => 'AACC00'));
$textrun->addText('I am colored', array('color' => 'AACC00'));

Defined style examples:

Expand Down
2 changes: 1 addition & 1 deletion docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Readers
+---------------------------+----------------------+--------+-------+-------+
| | Custom | ✓ | | |
+---------------------------+----------------------+--------+-------+-------+
| **Element Type** | Text | ✓ | ✓ | |
| **Element Type** | Text | ✓ | ✓ | |
+---------------------------+----------------------+--------+-------+-------+
| | Text Run | ✓ | | |
+---------------------------+----------------------+--------+-------+-------+
Expand Down
4 changes: 2 additions & 2 deletions docs/src/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Below are the supported features for each file formats.
|-------------------------|--------------------|------|-----|-----|
| **Document Properties** | Standard | ✓ | | |
| | Custom | ✓ | | |
| **Element Type** | Text | ✓ | ✓ | |
| **Element Type** | Text | ✓ | ✓ | |
| | Text Run | ✓ | | |
| | Title | ✓ | ✓ | |
| | Link | ✓ | | |
Expand Down Expand Up @@ -495,7 +495,7 @@ $section->addText('I am simple paragraph', $fontStyle, $paragraphStyle);
$textrun = $section->addTextRun();
$textrun->addText('I am bold', array('bold' => true));
$textrun->addText('I am italic', array('italic' => true));
$textrun->addText('I am colored, array('color' => 'AACC00'));
$textrun->addText('I am colored', array('color' => 'AACC00'));
```

Defined style examples:
Expand Down
3 changes: 2 additions & 1 deletion samples/Sample_11_ReadWord2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

// Read contents
$name = basename(__FILE__, '.php');
$source = "resources/{$name}.docx";
$source = __DIR__ . "/resources/{$name}.docx";

echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);

Expand Down
3 changes: 2 additions & 1 deletion samples/Sample_24_ReadODText.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

// Read contents
$name = basename(__FILE__, '.php');
$source = "resources/{$name}.odt";
$source = __DIR__ . "/resources/{$name}.odt";

echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'ODText');

Expand Down
15 changes: 15 additions & 0 deletions samples/Sample_28_ReadRTF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
include_once 'Sample_Header.php';

// Read contents
$name = basename(__FILE__, '.php');
$source = __DIR__ . "/resources/{$name}.rtf";

echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'RTF');

// Save file
echo write($phpWord, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
4 changes: 2 additions & 2 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ function write($phpWord, $filename, $writers)
$result .= date('H:i:s') . " Write to {$writer} format";
if (!is_null($extension)) {
$xmlWriter = IOFactory::createWriter($phpWord, $writer);
$xmlWriter->save("{$filename}.{$extension}");
rename("{$filename}.{$extension}", "results/{$filename}.{$extension}");
$xmlWriter->save(__DIR__ . "/{$filename}.{$extension}");
rename(__DIR__ . "/{$filename}.{$extension}", __DIR__ . "/results/{$filename}.{$extension}");
} else {
$result .= ' ... NOT DONE!';
}
Expand Down
21 changes: 21 additions & 0 deletions samples/resources/Sample_28_ReadRTF.rtf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{\rtf1
\ansi\ansicpg1252
\deff0
{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}
{\colortbl;\red255\green0\blue0;\red14\green0\blue0}
{\*\generator PhpWord;}

{\info{\title }{\subject }{\category }{\keywords }{\comment }{\author }{\operator }{\creatim \yr2014\mo05\dy27\hr23\min36\sec45}{\revtim \yr2014\mo05\dy27\hr23\min36\sec45}{\company }{\manager }}
\deftab720\viewkind1\uc1\pard\nowidctlpar\lang1036\kerning1\fs20
{Welcome to PhpWord}\par
\pard\nowidctlpar{\cf0\f0 Hello World!}\par
\par
\par
\pard\nowidctlpar{\cf0\f0\fs32\b\i I am styled by a font style definition.}\par
\pard\nowidctlpar{\cf0\f0 I am styled by a paragraph style definition.}\par
\pard\nowidctlpar\qc\sa100{\cf0\f0\fs32\b\i I am styled by both font and paragraph style.}\par
\pard\nowidctlpar{\cf1\f1\fs40\b\i\ul\strike\super I am inline styled.}\par
\par
{\field {\*\fldinst {HYPERLINK "http://www.google.com"}}{\fldrslt {Google}}}\par
\par
}
2 changes: 1 addition & 1 deletion src/PhpWord/IOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
*/
public static function createReader($name = 'Word2007')
{
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText'))) {
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText', 'RTF'))) {
throw new Exception("\"{$name}\" is not a valid reader.");
}

Expand Down
2 changes: 1 addition & 1 deletion src/PhpWord/Reader/AbstractReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class AbstractReader implements ReaderInterface
*
* @var bool|resource
*/
protected $fileHandle = true;
protected $fileHandle;

/**
* Read data only?
Expand Down
51 changes: 51 additions & 0 deletions src/PhpWord/Reader/RTF.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Reader;

use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Reader\RTF\Document;

/**
* RTF Reader class
*
* @since 0.11.0
*/
class RTF extends AbstractReader implements ReaderInterface
{
/**
* Loads PhpWord from file
*
* @param string $docFile
* @throws \Exception
* @return \PhpOffice\PhpWord\PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();

if ($this->canRead($docFile)) {
$doc = new Document();
$doc->rtf = file_get_contents($docFile);
$doc->read($phpWord);
} else {
throw new \Exception("Cannot read {$docFile}.");
}

return $phpWord;
}
}
Loading