Displaying TextEdit files on a website


Sometimes it's easier to type some text in TextEdit, save the file as html and use an <iframe> to display the file.

But what if you want to display images? You cannot save text and images as HTML.

TextEdit will save such file as .rtfd (Rich Text  Format with Attachments). But on a server it's a folder which contains the images and a TXT.rtf file.

And the TXT.rtf file will not always display in a browser.

So PHP to the rescue.

There are rtf > html PHP converters. But none worked or needed additional libraries.

So I wrote the code myself. Here's how it works :

Besides saving the file as .rtfd, you also save the file as .webarchive (Web Archive).

When you open the .webarchive file in a plaintext editor, you'll see the HTML code at the end of the page.

Everything before that code, are the images (if present) and other styling.

The PHP code finds the <style>…</style> and the <body>…</body> parts.

There are two option. Extract the <body>…</body> part with or without the <body> and </body> element. The default is without, since most of the time the content is included in an existing webpage. You currently have no choice. But if needed, the $useBodyTags variable in core.php must be set to true. You decide.

If it is not possible to include the file, you can display the file in an <iframe>. The default is the PHP function include().

The actual extraction is done in a PHP file named core.php.

The code to embed in a webpage is in file named content.php. But only because I use it on this site. You need the code in the file, not the file itself.

Click to see the content.php file

Click to see the included textedit.php file

The actual page that is included has the same name as the saved .rtfd and .webarchive files.

textedit-files

So, if the TextEdit files are named holiday.rtfd and holiday.webarchive, the accompanying file is named holiday.php.

The include function is include("holiday.php").

Click to see the holiday.php file

The TextEdit files en the PHP file should be in the textedit folder.

The textedit folder should be in the root of the webserver.

The code in core.php expects it there.

Other locations are possible, but it required a lot of checking of pathnames, so I opted to keep it simple.

The PHP code in the content.php file requires that you change the base name of the TextEdit files in variable $includedFile.

If you use an <iframe> you need to set variable $theUse to false. You can also set the $theWidth and $theHeight variables.

And if the code in the content.php files is too much, you can use :

include($_SERVER["DOCUMENT_ROOT"] . "/textedit/holiday.php");

or with the complete HTML code :

<iframe src="/textedit/holiday.php?usehtml=true"></iframe>

So, to display the text, the .webarchive is used. Any images are stored in the .rtfd file.

Changes can be made in either file. It is recommended to also Save as… the file in the other format vice-versa.

Download the textedit.zip file

Please feel free to add your own code or make changes.




Update 17 December 2015

If for any other reason, you can include TextEdit files with a JavaScript.

The concept is the same. File ➠ core.phpupdated version was rewritten.

The TextEdit files are in the same location as the file it is included.

Have a look ➠ Hema logo

Feb 17 2021 05:47:33