PHP Classes

PHP RTF Tools: Parse and generate RTF documents using templates

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 73%Total: 721 All time: 4,596 This week: 64Up
Version License PHP version Categories
rtftools 1.0.4GNU General Publi...5.6PHP 5, Files and Folders, Text proces..., P...
Description 

Author

This package can parse and generate RTF documents using templates.

Currently it provides different classes that can:

- Process RTF documents from files or strings (RtfDocument class)
- Rewrite RTF documents splitting their tags into multiple lines so it can be easy to compare and find differences between two files using diff like utilities (RtfBeautifier)
- Extract raw text from RTF documents with some basic formatting capabilities (RtfBeautifier)
- Process RTF document templates with a macro language that suppports:

* Inline variables expansion within the document (variables are supplied by the caller)
* Expressions evaluation and replacement using PHP syntax (for example : date ( 'Y-m-d H:i:s' ))
* IF/ELSEIF/ELSE/ENDIF constructs, to conditionally include text in the output document
* REPEAT/FOR loops that can repeat blocks of text
* FOREACH loops that operate on array values supplied by the caller
(RtfTemplater class)

- Merge several RTF documents together including using the template processing objects, external files or strings, preserving details like fonts, colors, styles and avoiding page number repetition (RtfMerger class)
- A generic Rtf parser class is also provided, that can be used if you need more advanced capabilities when interpreting Rtf document contents (RtfParser class).

All the classes in this package have been designed to be able to process Rtf documents that may be larger than the available memory. This is useful when you need to handle at once several Rtf documents whose total size may exceed your current PHP memory limit.

Innovation Award
PHP Programming Innovation award nominee
September 2016
Number 3


Prize: PHP Tools for Visual Studio Personal license
Comparing word processing documents programmatically to see what was changed is a hard problem because you need to parse the documents and determine how to compare them.

This package provides an alternative solution to simplify this task.

Many Word processing documents can export documents in RTF (Rich Text Format). This is a text based format based on marks that identify different values in the documents like fonts, text formatting attributes, colors, etc..

If you save your Word processor documents in the RTF format, this package can easily render the document data in a beautified way that make it easier to compare document using a diff-like text comparison program.

Manuel Lemos
Picture of Christian Vigh
  Performance   Level  
Name: Christian Vigh <contact>
Classes: 32 packages by
Country: France France
Age: 58
All time rank: 13410 in France France
Week rank: 24 Up2 in France France Up
Innovation award
Innovation award
Nominee: 20x

Winner: 3x

Example

<?php
   
/****************************************************************************************************
     *
     * This script demonstrates the Rtf text templating capabilities of the RtfTemplater classes
     * (RtfStringTemplater or RtfFileTemplater).
     *
     * It extract text contents from file "template.rtf", which contains a whole set of templating
     * instructions.
     *
     ****************************************************************************************************/

   
include ( '../../sources/RtfTemplater.phpclass' ) ;

   
$variables =
       [
       
'VNAME1' => 'the value of vname1',
       
'VNAME2' => 'the value of vname2',
       
'INDEX' => 17,
       
'ARRAY' => [ 'string a', 'string b', 'string c' ]
        ] ;

    
$templater = new RtfStringTemplater ( file_get_contents ( 'template.rtf' ), $variables ) ;
    
$templater -> SaveTo ( 'sample.rtf' ) ;


Details

INTRODUCTION

This package provides several classes to handle Rtf data. It has been designed to be able to handle files whose contents are too big to fit into memory. This is why you will see a dichotomy among classes :

  • Classes whose name starts with RtfString handle in-memory Rtf contents.
  • Classes whose name starts with RtfFile handle file-based Rtf contents.

It is up to you to decide whether you will have to handle big files. If you are sure that your Rtf contents will always fit into memory, use the RtfString classes. If you are sure that your files may be bigger than the available memory, use the RtfFile classes. This will add some overhead due to file IO (this can more than double the execution time if you have low performance IO subsystem, but I noticed some systems where the difference between the in-memory and file versions are only a few tenths of milliseconds).

This package currently implements the following classes :

  • RtfStringBeautifier and RtfFileBeautifier : pretty-prints Rtf file contents so that you will be able to compare two Rtf files using utilities such as diff or windiff. You will find some help here : help/README.beautifier.md.
  • RtfStringParser and RtfFileParser : a generic parser for Rtf contents. It provides a set of Rtf\Token* classes that map to underlying Rtf token types, along with a minimal intelligence that allows you to track certain control word values depending on the current nesting level, as well as handling picture or binary data contents (help/README.parser.md).
  • RtfStringTexter and RtfFileTexter : a class that extracts raw text from Rtf contents, with some basic formatting capabilities (help/README.texter.md).
  • RtfStringTemplater and RtfFileTemplater : a class that allows you to process Rtf templates containing macro language constructs to generate final output documents, such as you would do for mailings (help/README.templater.md).
  • RtfMerger : a class that allows you to merge multiple Rtf documents into a single one (help/README.merger.md). This is the only Rtf-processing class of this package that does not inherit from RtfDocument.

DEPENDENCIES

All the classes in this package rely on the SearchableFile class (http://www.phpclasses.org/package/9697-PHP-Process-text-files-too-big-to-fit-into-memory.html) that allows to search for contents in files too big to fit into memory. A copy of this class is present in this package for your convenience but note that it may not be the latest release.

All the main class files in this package (RtfBeautifier.phpclass, RtfParser.phpclass and RtfTexter.phpclass) also inherit from the RtfDocument class (RtfDocument.phpclass).

REFERENCES

This package depends on the following package, SearchableFile, which allows you to search text data from files too big to fit into memory :

http://www.phpclasses.org/package/9697-PHP-Process-text-files-too-big-to-fit-into-memory.html

For convenience reasons, the SearchableFile.phpclass file has been included here, but note that this may not be the latest version.

The latest Microsoft Rtf format specifications can be downloaded here :

https://www.microsoft.com/en-us/download/details.aspx?id=10725

But you can also consult the excellent book "Rtf pocket guide" from Sean M. Burke :

https://books.google.fr/books/about/RTF_Pocket_Guide.html?id=4N_lVcyyhqMC&redir_esc=y

or visit his page :

http://interglacial.com/~sburke/

SUPPORT

If you have problems using one of the RtfTools class, or get strange results, please feel free to contact me at the following address :

christian.vigh@wuthering-bytes.com

Don't hesitate to send me the Rtf documents that caused the failure as attachments in your email ; I will be happy to answer you !


  Files folder image Files (44)  
File Role Description
Files folder imageexamples (1 file, 4 directories)
Files folder imagehelp (7 files, 1 directory)
Files folder imagesources (8 files, 1 directory)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file NOTICE Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (44)  /  examples  
File Role Description
Files folder imagebeautifier (4 files)
Files folder imagemerger (5 files)
Files folder imagetemplater (3 files)
Files folder imagetexter (2 files)
  Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (44)  /  examples  /  beautifier  
File Role Description
  Accessible without login Plain text file beautifier.php Example Example script
  Accessible without login Plain text file beautifier_perf.php Example Example script
  Accessible without login Plain text file create_samples.php Aux. Auxiliary script
  Accessible without login Plain text file sample.rtf Data Auxiliary data

  Files folder image Files (44)  /  examples  /  merger  
File Role Description
  Accessible without login Plain text file merger.php Example Example script
  Accessible without login Plain text file sample1.rtf Data Auxiliary data
  Accessible without login Plain text file sample2.rtf Data Auxiliary data
  Accessible without login Plain text file sample3.rtf Data Auxiliary data
  Accessible without login Plain text file sample4.rtf Data Auxiliary data

  Files folder image Files (44)  /  examples  /  templater  
File Role Description
  Accessible without login Plain text file sample.rtf Data Auxiliary data
  Accessible without login Plain text file template.rtf Data Auxiliary data
  Accessible without login Plain text file templater.php Example Example script

  Files folder image Files (44)  /  examples  /  texter  
File Role Description
  Accessible without login Plain text file sample.rtf Data Auxiliary data
  Accessible without login Plain text file texter.php Example Example script

  Files folder image Files (44)  /  help  
File Role Description
Files folder imageimages (9 files)
  Accessible without login Plain text file help.css Data Auxiliary data
  Accessible without login HTML file help.html Doc. Documentation
  Accessible without login Plain text file README.beautifier.md Doc. Documentation
  Accessible without login Plain text file README.merger.md Doc. Documentation
  Accessible without login Plain text file README.parser.md Doc. Documentation
  Accessible without login Plain text file README.templater.md Doc. Documentation
  Accessible without login Plain text file README.texter.md Doc. Documentation

  Files folder image Files (44)  /  help  /  images  
File Role Description
  Accessible without login Image file Modeling-RtfBeautifier.png Data Auxiliary data
  Accessible without login Image file Modeling-RtfDocument.png Data Auxiliary data
  Accessible without login Image file Modeling-RtfMerger.png Icon Icon image
  Accessible without login Image file Modeling-RtfParser.png Data Auxiliary data
  Accessible without login Image file Modeling-RtfTemplater.png Data Auxiliary data
  Accessible without login Image file Modeling-RtfTexter.png Data Auxiliary data
  Accessible without login Image file Modeling-RtfToken.png Data Auxiliary data
  Accessible without login Image file tree-collapse.png Icon Icon image
  Accessible without login Image file tree-expand.png Icon Icon image

  Files folder image Files (44)  /  sources  
File Role Description
Files folder imageMerger (2 files)
  Plain text file RtfBeautifier.phpclass Class Class source
  Plain text file RtfDocument.phpclass Class Class source
  Plain text file RtfMerger.phpclass Class Class source
  Plain text file RtfParser.phpclass Class Class source
  Plain text file RtfTemplater.phpclass Class Class source
  Plain text file RtfTexter.phpclass Class Class source
  Plain text file RtfToken.phpclass Class Class source
  Plain text file SearchableFile.phpclass Class Class source

  Files folder image Files (44)  /  sources  /  Merger  
File Role Description
  Plain text file RtfMergerDocument.phpclass Class Class source
  Plain text file RtfMergerHeader.phpclass Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:721
This week:0
All time:4,596
This week:64Up
User Ratings User Comments (1)
 All time
Utility:91%StarStarStarStarStar
Consistency:95%StarStarStarStarStar
Documentation:87%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:73%StarStarStarStar
Rank:127