解析通过PHP
问题描述:
RTF模板,我试图使用RTF文件作为生成包含每(MySQL的)数据库行的页面的新RTF文档的模板。解析通过PHP
我的方法几乎适用不同的是,生成的文件只包含一个页面,从第一行,而不是一个页面的每一行。 (我的测试中应该有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;
}
}
答
除了手动创建文档,请尝试使用PHPRtfLite。
PHPRtfLite是一个API,使开发人员能够使用php创建rtf文档。 PHPRtfLite是按照OOP原则设计的。
+1
phprtflite是即时创建RTF文档,不使用模板,这是最终用户想要的。我不指望他能写php。 – pixeline 2010-06-23 15:14:21
我在寻找同样的事情。你有没有找到你的问题的答案? – 2011-02-16 10:43:27