Download the PHP package jstewmc/rtf without Composer

On this page you can find all versions of the php package jstewmc/rtf. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package rtf

CircleCI codecov

rtf

This library makes it easy to work with (RTF) documents in PHP.

The example above would produce the following output (note, the text "foo" has been replaced by the text "bar"):

Installing

This library requires PHP 7.4+.

It is multi-platform, and we strive to make it run equally well on Windows, Linux, and OSX.

It should be installed via Composer. To do so, add the following line to the require section of your composer.json file, and run composer update:

Using this library

This library is large, and it's challenging to briefly summarize it. To make things a bit easier to understand, we've broken it into styling RTF.

Reading RTF

You can construct an RTF document from a string or file:

You can construct an RTF snippet from a string:

Either way, the library will automatically lex, parse, and render the given RTF.

Writing RTF

You can use the write() method to output a document or snippet as an RTF, HTML, or plain-text string:

When a document or snippet is treated as a string, it will return an RTF string:

Saving RTF

You can use a document's save() method to save a document to a file (snippets don't support files):

By default, the save() method will assume the file's format from the destination's file extension (i.e., htm[l], rtf, and txt). If you use a non-standard file extension, you can specify the file's format as the method's second argument:

Traversing RTF

Documents and snippets are composed of RTF elements, much like an HTML document is composed of tags or an XML document is composed of nodes. There are four types of RTF elements: groups, control words, control symbols, and text. To traverse a document or snippet, you'll move between and among these elements.

Each element is represented by a PHP object within the Jstewmc\Rtf\Element namespace.

Groups are a bit special, because they are the only element in RTF that can have children.

Every document has a root group, the top-most element of a document. All other elements in an RTF document are descendants of this group. Traversing a document always starts from the root group:

A snippet, on the other hand, may not have a root group. For a variety of reasons, this library treats a snippet as its own root group:

A group - root or not - provides a variety of methods to traverse its child elements, which may or may not include other groups.

You can use getFirstChild(), getLastChild(), or getChild() to access the first, last, or any child:

You can use getControlWords() or getControlSymbols() to query for specific control words and symbols (the second argument, parameter, may be: null, any parameter (default); false, no parameter; or, an int/string):

You can use the hasChild() method to query whether or not an element exists:

While the examples above use the root group, any group element supports these methods. But, there's more to traversing elements than just groups.

You can use getNextSibling() and getPreviousSibling() to move through any element:

You can use getNextText() and getPreviousText() to move through text elements:

Editing RTF

This library includes a number of methods for editing RTF.

We've done our best to support editing elements from either side of the parent-child, Group-Element relationship. So, feel free to use whichever side makes the most sense given the element you currently have and the change you're attempting to make.

You can use Group::appendChild() or Element::appendTo() to append an element as the last child to a group:

You can use Group::prependChild() or Element::prependTo() to prepend an element as the first child to a group:

You can use Group::insertChild()to insert an element at a given index within a group:

You can use Element::insertBefore() or Element::insertAfter() to insert the current element before or after another element, respectively:

You can use Element::putPreviousSibling() or Element::putNextSibling() to insert an element before or after the current element, respectively:

You can use Group::replaceChild() or Element::replaceWith() to replace an element:

Finally, you can use Group::removeChild() to remove an element:

Styling RTF

The RTF specification lumps an element's style into one huge "group state". Such a monolithic state is difficult to work with, however. So, I gave each element a style property, like an HTML element.

An element's style is sub-divided into document-, section-, paragraph-, and character-states.

For any element, the following properties are available:

Admittedly, it's a little clunky, but you can access an element's style like so:

A document will be rendered and its elements styled on instantiation. However, any changes to the document, like inserting or removing elements, will cause a re-render of the element's parent group.

Rich Text Format (RTF)

This library adheres to the Rich Text Format Version 1.5 Specification (1997), and it adds supports for the ignored control word \*.

If you aren't familiar with the Rich Text Format (RTF) language, it's relatively simple. There are four main language components: groups, control words, control symbols, and text.

Groups

Groups are the fundamental building blocks of an RTF document. A group consists of text, control words, control symbols, or other groups enclosed in braces ("{" and "}").

Like an XML document, an RTF document must have a root group. Within the root group there is a header, a group of document-formatting control words that, if they occur, must do so before any text, and the body, the content of the document.

A document header must precede the first plain-text character in the document, and may include groups for fonts, styles, screen-color, pictures, footnotes, comments (aka, annotations), headers, footers, summary information, fields, and bookmarks as well as document-, section-, paragraph-, and character-formatting properties.

Groups can be nested. Generally, formatting within a group affects only the text in that group, and text within a group inherits the formatting of the parent group. However, the footnote, comment (aka, annotation), header, and footer groups do not inherit the formatting of their parent group. (To ensure that these groups are always formatted correctly, you should set the formatting of these groups to the default with the \sectd, \pard, and \plain control words, and then add any desired formatting.)

Control words

A control word is a specially-formatted command used to perform actions in an RTF document such as: insert special characters, set paragraph-formatting, set character-formatting, etc.

A control word takes the following form: \<word>[<delimiter>], where word is a string of fewer than 32 lowercase, alphabetic characters, and delimiter is one of the following:

The parameters of certain control words (for example, bold, \b) have only two states. When such a control word has no parameter (or has a non-zero parameter), it is assumed that the control word turns on the property. When such a control word has a parameter of 0, it is assumed to turn off the property.

Control symbols

A control symbol consists of a backslash followed by a single, non-alphabetic character (aka, a symbol). A control symbol usually inserts a special character. For example, the control symbol \~ represents a non-breaking space.

Generally, control symbols take no parameters. However, the apostrophe control symbol takes a two-digit hexadecimal parameter (e.g., \'hh).

Text

Text is any character that isn't a group-open, group-close, control word, or control symbol.

Special characters like the backlash ("\"), open-bracket ("{"), and close-bracket ("}") must be escaped with the backslash character ("\").

Line endings

The RTF specification instructs writers to insert line-feeds and/or carriage-returns every 255 characters or so, and it instructs readers to ignore them. Instead, line breaks should be controlled with the \line control word (among others), and paragraphs should be controlled with the \par control word.

This library will ignore an un-escaped line-feed or carriage return. However, it will treat an escaped line-feed or carriage-return as an implicit \par control word.

Destinations

A destination is a group of related text that could appear in a different position (aka, "destination") within the document. Destinations may also be text that is used but should not appear in a document.

For the purposes of this library, destinations must be preceded by the ignored control symbol, \*.

Because this library doesn't support any destination control words, all destinations are ignored when formatting the document as text or html.

Supported control words and symbols

This library only supports a small subset of the several hundred possible RTF control words and control symbols (see RTF Specification 1.5 or Latex2Rtf Documentation for details):

This library doesn't support the following control words:

If this library encounters a control word or control symbol it doesn't support, it'll create a generic control word or control symbol element, Element\Control\Word or Element\Control\Symbol, respectively. When formatting the document as html or plain text, unsupported control words are ignored, and their text appears in the document, unless the group is a destination.

License

This library is released under the MIT license.

Acknowledgements

In February 2015, I started a project that required reading and writing RTF files. Actually, it required reading and writing files in the Court Reporter extension of the RTF language, RTF-CRE.

I couldn't find a library that was easily extensible with new control words and control symbols. So, I wrote my own (for better or worse haha).

Many thanks to the authors of the following articles for helping me get started:


All versions of rtf with dependencies

PHP Build Version
Package Version
Requires jstewmc/stream Version ^0.4.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jstewmc/rtf contains the following files

Loading the files please wait ....