Created: April 2001. Updated December 2001, February 2004, December 2024.
Make an RTF Document with PHP
NOTE: I didn't do any magic here, I just modified the ideas at irt.org on producing RTF files with Cold Fusion
Update 1/2002: Matthew Walden had some issues getting this to work under IIS and Apache. His comments may be helpful. Thanks Matthew!
I had problems getting apache and MS IIS working with RTF documents. I fixed apache by adding the following line into
httpd.conf
:AddType application/x-httpd-php .rtf
IIS has a slightly more long-winded solution:
In the management console, under the properties for the virtual (or default) web in the home directory, go to home directory | application settings | configuration. Click on "Add" and enter the path to the php.exe as it is set for .php files. (For me this was
C:\PHP\php.exe "%s" "%s"
). Type .rtf as the extension and press OK.The web / web service may need to be restarted for the changes to be reflected.
And on changing textarea
type documents so that the newlines propagate to the RTF file, I got this feedback:
<?print print str_replace('<br />', '\line',nl2br(StripSlashes($_POST['address'])))?>
That works because in the text based format of RTF,
\line
is the code that forces a newline.
Thanks to Mariusz for this feedback!
HTML Form
<form action="./rtf.rtf" method="post" class="example">
<h2>Create RTF Document:</h2>
<label><span>Enter your name: </span><input type="text" name="name" required></label>
<label><span>Enter your last name: </span><input type="text" name="lastname" required></label>
<label><span>Enter your address: </span><input type="text" name="address" required></label>
<label><span>Enter your city: </span><input type="text" name="city" required></label>
<label><span>Enter your state: </span><input type="text" name="state" required></label>
<label><span>Enter your zip: </span><input type="text" name="zip" required></label>
<input type="submit" value="Submit">
</form>
rtf.rtf
<?php
header('Content-Type: application/rtf');
header('Content-Disposition: attachment; filename="data.rtf"');
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
?>{\rtf1\mac\ansicpg10000\uc1 \deff0\deflang1033\deflangfe1033{\upr{\fonttbl{\f0\fnil\fcharset256\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f4\fnil\fcharset256\fprq2{\*\panose 02000500000000000000}Times;}
}{\*\ud{\fonttbl{\f0\fnil\fcharset256\fprq2{\*\panose 02020603050405020304}Times New Roman;}{\f4\fnil\fcharset256\fprq2{\*\panose 02000500000000000000}Times;}}}}{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;
\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;
\red128\green128\blue128;\red192\green192\blue192;}{\stylesheet{\widctlpar\adjustright \f4\cgrid \snext0 Normal;}{\*\cs10 \additive Default Paragraph Font;}{\*\cs15 \additive \ul\cf2 \sbasedon10 Hyperlink;}}{\info{\title <}{\author Joe Crawford}
{\operator Joe Crawford}{\creatim\yr2001\mo2\dy15\hr22\min12}{\revtim\yr2001\mo2\dy15\hr22\min38}{\version2}{\edmins6}{\nofpages1}{\nofwords46}{\nofchars267}{\*\company joecrawford.com}{\nofcharsws327}{\vern115}}
\widowctrl\ftnbj\aenddoc\hyphcaps0\formshade\viewkind4\viewscale100\pgbrdrhead\pgbrdrfoot \fet0\sectd \linex0\endnhere\sectdefaultcl {\*\pnseclvl1\pnucrm\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl2\pnucltr\pnstart1\pnindent720\pnhang{\pntxta .}}
{\*\pnseclvl3\pndec\pnstart1\pnindent720\pnhang{\pntxta .}}{\*\pnseclvl4\pnlcltr\pnstart1\pnindent720\pnhang{\pntxta )}}{\*\pnseclvl5\pndec\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl6\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb (}
{\pntxta )}}{\*\pnseclvl7\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl8\pnlcltr\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}{\*\pnseclvl9\pnlcrm\pnstart1\pnindent720\pnhang{\pntxtb (}{\pntxta )}}\pard\plain
\widctlpar\adjustright \f4\cgrid {\fs144 <?php echo $name; ?>'s Data.
\par }\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrh\brdrs\brdrw10 \trbrdrv\brdrs\brdrw10 \clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr
\brdrs\brdrw10 \cltxlrtb \cellx4320\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \cltxlrtb \cellx8748\pard \widctlpar\intbl\adjustright {\b\fs32 Name\cell }{\fs32 <?php echo $name; echo " "; echo $lastname?>
\cell }\pard \widctlpar\intbl\adjustright {\fs32 \row }\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrh\brdrs\brdrw10 \trbrdrv\brdrs\brdrw10 \clvertalt\clbrdrt\brdrs\brdrw10
\clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \cltxlrtb \cellx4320\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \cltxlrtb \cellx8748\pard \widctlpar\intbl\adjustright {\b\fs32
Address\cell }{\fs32 <?php echo $address; ?>\cell }\pard \widctlpar\intbl\adjustright {\fs32 \row }\pard \widctlpar\intbl\adjustright {\b\fs32 City\cell }{\fs32 <?php Echo $city ?>\cell }\pard \widctlpar\intbl\adjustright {\fs32 \row }\pard
\widctlpar\intbl\adjustright {\b\fs32 State\cell }{\fs32 <?php echo $state ?>\cell }\pard \widctlpar\intbl\adjustright {\fs32 \row }\trowd \trgaph108\trleft-108\trbrdrt\brdrs\brdrw10 \trbrdrl\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10
\trbrdrh\brdrs\brdrw10 \trbrdrv\brdrs\brdrw10 \clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr\brdrs\brdrw10 \cltxlrtb \cellx4320\clvertalt\clbrdrt\brdrs\brdrw10 \clbrdrl\brdrs\brdrw10 \clbrdrb\brdrs\brdrw10 \clbrdrr
\brdrs\brdrw10 \cltxlrtb \cellx8748\pard \widctlpar\intbl\adjustright {\b\fs32 Zip Code\cell }{\fs32 <?php echo $zip; ?>\cell }\pard \widctlpar\intbl\adjustright {\fs32 \row }\pard \widctlpar\adjustright {\fs32
\par
\par }{This demo brought to you by Joe Crawford <https://artlung.com>.}{\ul
\par }{\fs32
\par
\par
\par
\par }}
.htaccess
# Process .rtf as PHP files
AddHandler application/x-httpd-php .rtf