Skip to content

Commit 87f071d

Browse files
committed
#72: Basic RTF Reader
1 parent d7f3d25 commit 87f071d

File tree

8 files changed

+459
-8
lines changed

8 files changed

+459
-8
lines changed

samples/Sample_27_ReadRTF.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
include_once 'Sample_Header.php';
3+
4+
// Read contents
5+
$name = basename(__FILE__, '.php');
6+
$source = "results/Sample_01_SimpleText.rtf";
7+
$source = "resources/rtf.rtf";
8+
$source = "results/Sample_11_ReadWord2007.rtf";
9+
10+
echo date('H:i:s'), " Reading contents from `{$source}`", EOL;
11+
$phpWord = \PhpOffice\PhpWord\IOFactory::load($source, 'RTF');
12+
13+
// Save file
14+
echo write($phpWord, basename(__FILE__, '.php'), $writers);
15+
if (!CLI) {
16+
include_once 'Sample_Footer.php';
17+
}

samples/resources/rtf.rtf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{\rtf1
2+
\ansi\ansicpg1252
3+
\deff0
4+
{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 Times New Roman;}}
5+
{\colortbl;\red255\green0\blue0;\red14\green0\blue0}
6+
{\*\generator PhpWord;}
7+
8+
{\info{\title }{\subject }{\category }{\keywords }{\comment }{\author }{\operator }{\creatim \yr2014\mo05\dy27\hr23\min36\sec45}{\revtim \yr2014\mo05\dy27\hr23\min36\sec45}{\company }{\manager }}
9+
\deftab720\viewkind1\uc1\pard\nowidctlpar\lang1036\kerning1\fs20
10+
{Welcome to PhpWord}\par
11+
\pard\nowidctlpar{\cf0\f0 Hello World!}\par
12+
\par
13+
\par
14+
\pard\nowidctlpar{\cf0\f0\fs32\b\i I am styled by a font style definition.}\par
15+
\pard\nowidctlpar{\cf0\f0 I am styled by a paragraph style definition.}\par
16+
\pard\nowidctlpar\qc\sa100{\cf0\f0\fs32\b\i I am styled by both font and paragraph style.}\par
17+
\pard\nowidctlpar{\cf1\f1\fs40\b\i\ul\strike\super I am inline styled.}\par
18+
\par
19+
{\field {\*\fldinst {HYPERLINK "http://www.google.com"}}{\fldrslt {Google}}}\par
20+
\par
21+
}

src/PhpWord/IOFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
5151
*/
5252
public static function createReader($name = 'Word2007')
5353
{
54-
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText'))) {
54+
if (!in_array($name, array('ReaderInterface', 'Word2007', 'ODText', 'RTF'))) {
5555
throw new Exception("\"{$name}\" is not a valid reader.");
5656
}
5757

src/PhpWord/Reader/AbstractReader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class AbstractReader implements ReaderInterface
3939
*
4040
* @var bool|resource
4141
*/
42-
protected $fileHandle = true;
42+
protected $fileHandle;
4343

4444
/**
4545
* Read data only?

src/PhpWord/Reader/RTF.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Reader;
19+
20+
use PhpOffice\PhpWord\PhpWord;
21+
use PhpOffice\PhpWord\Reader\RTF\Document;
22+
23+
/**
24+
* RTF Reader class
25+
*
26+
* @since 0.11.0
27+
*/
28+
class RTF extends AbstractReader implements ReaderInterface
29+
{
30+
/**
31+
* Loads PhpWord from file
32+
*
33+
* @param string $docFile
34+
* @return \PhpOffice\PhpWord\PhpWord
35+
*/
36+
public function load($docFile)
37+
{
38+
$phpWord = new PhpWord();
39+
40+
if ($this->canRead($docFile)) {
41+
$doc = new Document();
42+
$doc->rtf = file_get_contents($docFile);
43+
$doc->read($phpWord);
44+
} else {
45+
throw new \Exception("Cannot read {$docFile}.");
46+
}
47+
48+
return $phpWord;
49+
}
50+
}

0 commit comments

Comments
 (0)