通过php解析RTF模板

发布于 2024-09-07 06:00:42 字数 2922 浏览 7 评论 0原文

我正在尝试使用 RTF 文件作为模板来生成新的 RTF 文档,其中每个(mysql)数据库行包含一个页面。

我的方法几乎有效,只是生成的文件仅包含一页(第一行),而不是每行一页。 (我的测试应该有2页)。

这是主循环。

$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;

为了彻底起见,这是我的简单模板类代码:

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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2024-09-14 06:00:50

不要手动创建文档,而是尝试使用 PHPRtfLite

PHPRtfLite 是一个 API,使开发人员能够使用 php 创建 rtf 文档。 PHPRtfLite 是根据 OOP 原则设计的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文