dongmei8071 2010-06-23 14:36
浏览 55

通过php解析RTF模板

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
";
    //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;
    }
}
  • 写回答

1条回答 默认 最新

  • douxi4114 2010-06-23 14:42
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计