通过php解析RTF模板
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要手动创建文档,而是尝试使用 PHPRtfLite。
Instead of creating the document manually, try using PHPRtfLite.