tags:

views:

21

answers:

1

I'm trying to use an RTF file as a template for generating a new RTF document containing a page per (mysql) database row.

My method almost works except that the generated file contains only one page, with the first row, and not one page for each row. (there should be 2 pages in my test).

Here is the main loop.

$today= date('d-m-Y');
$tp= new templateParser(LOCALADMIN.'_documents/fiche_individuelle.rtf');

foreach($inscriptions as $person) {
    $tags= array('FIRSTNAME'=>$person['firstname'],
            'LASTNAME'=>$person['lastname'],
            'BIRTHDATE'=>$person['birthdate'],
            'TELEPHONE1'=>$person['mobile_phone'],
            'MEDICALBACKGROUND'=>$person['medical_background'],
            'ALLERGIES'=>$person['allergies']
    );

    $tp->parseTemplate($tags);
    $content .= $tp->display();

    $content .= "\\section\n";
    //END foreach
}
// create RTF Document

    $today= date('d-m-Y-hms');
    $filename = $season_name.'_'.$today.'.rtf';

    header('Content-type: application/msword');
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: public");
    header("content-disposition: attachment; filename=\"$filename\"");
    print $content;
    exit;

For the sake of being thorough, here is my simple template class code:

class templateParser {
    // member definition
    var $output;
    var $log;
    var $replacedTags;

    function templateParser($templateFile = 'default_template.htm')
    {
        // constructor setting up class initialization
        $this->log .= "templateParser() called<br />";
        if (file_exists($templateFile)) {
            $this->log .= 'Found template file ' . $templateFile . '<br/>';
            $this->output = file_get_contents($templateFile);
        } else {
            $this->log .= 'Error:Template file ' . $templateFile . ' not found';
            return false;
        }
    }

    function parseTemplate($tags = array())
    {
        $this->log = 'parseTemplate()  called. <br/>';
        // code for parsing template files
        if (count($tags) > 0) {
            foreach($tags as $tag => $data) {
                $data = (file_exists($data))? $this->parseFile($data) : $data;
                $this->output = str_replace('%' . $tag . '%', $data, $this->output);
                $this->log .= 'parseTemplate() replaced <b>' . $tag . '</b> in template<br/>';
                $this->replacedTags ++;
            }
        } else {
            $this->log .= 'WARNING: No tags were provided for replacement<br/>';
        }
        return $this->replacedTags;
    }

    function parseFile($file)
    {
        $this->log .= 'parseFile()  called. <br/>';
        ob_start();
        include($file);
        $content = ob_get_contents();
        ob_end_clean();
        return $content;
    }

    function display()
    {
        // code for displaying the finished parsed page
        return $this->output;
    }
}
A: 

Instead of creating the document manually, try using PHPRtfLite.

PHPRtfLite is an API enabling developers to create rtf documents with php. PHPRtfLite is designed on OOP principles.

Simon Brown
phprtflite is to create RTF documents on the fly, not using templates, which is what the end-user wants. I don't expect him to be able to write php.
pixeline