【问题标题】:Generate MS word document in Joomla在 Joomla 中生成 MS Word 文档
【发布时间】:2011-08-20 22:22:53
【问题描述】:

我试图完成的事情:

  1. 管理员创建带有占位符的 MS Word 文档,这些占位符将填充 Joomla 数据库中的数据
  2. 管理员上传 MS Word 文件到 Joomla 并用 SQL 语句连接
  3. 用户执行“生成 MS Word”功能并从数据库中获取填充了数据的 MS Word 文档。

是否有任何 Joomla 组件可以做到这一点? 我已经在我的应用程序中使用互操作库完成了这项工作。

【问题讨论】:

    标签: joomla ms-word


    【解决方案1】:

    最近我使用 phpdocx 和 pclzip 库为 joomla 组件完成了此操作,其中 *.docx 文件是从模板文件生成的。

    配置:

    $params         = Object with form data;    // data from requeest
    $template       = 'xml_file_name';          // jfrom xml file name and *.docx template file name
    $pl             = '#';                      // place holder prefix & suffix: (example: #PLACEHOLDER#)
    $date_placehold = '#DATE#';                 // will be replaced with current date
    $date_formt     = 'F j, Y';                 // php date format
    $template_path  = JPATH_COMPONENT_SITE .DS.'templates'.DS.$template.'.docx';
    $temp_dir       = JPATH_ROOT.DS.'tmp'.DS.'phpdocx-temp-dir';    // + write access
    $output_mime    = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
    $filename       = $params->first_name .' - '. $params->second_name.'.docx';
    
    // get names of all form fields of type 'list' from xml file
    $lists      = (array) ComponentHelper::getJFormLists($template);
    // get all field names from the xml file
    $fields = (array) ComponentHelper::getJFormFieldsNames($template);
    

    初始化变量:

    $doc =& JFactory::getDocument();
    $doc->setMimeEncoding($output_mime); 
    
    // require phpdocx class
    require_once(JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers'.DS.'pclzip.lib.php');
    require_once(JPATH_COMPONENT_ADMINISTRATOR . DS . 'helpers'.DS.'phpdocx.php');
    
    $phpdocx = new phpdocx($template_path, $temp_dir);
    

    将表单字段与用户参数绑定:

    foreach($params as $field => $value)
    {
        if(array_key_exists($field,$lists) && is_array($lists[$field]) && array_key_exists($value, $lists[$field]) )
        {
            // if the field is JFormInput with type "list" its value is not important but its label
            $var = $lists[$field][$value];
        } else {
            $var = $value;
        }
    
        // use openxml carriage return on new lines
        if(strpos($var, "\n")) {
            $var = str_replace("\n", '<w:br/>', $var);
        }
    
        $fields[$field] = $var;
    }
    

    foreach 申请表字段:

    foreach($fields as $field => $value)
    {
            // replace placeholder with form value
            $phpdocx->assign($pl.strtoupper($field).$pl, $value);
    }
    
    // assign date for filled-in applications
    if(!empty($date_placehold)
    {
        $phpdocx->assign($date_placehold, date($date_formt));
    }
    

    输出文件:

    $phpdocx->stream($filename, $output_mime);
    
    return true;
    

    【讨论】:

    • 随意修改,欢迎提出任何建议。我将代码放在组件的模型中
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2012-09-20
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 2010-11-01
    • 2017-01-26
    相关资源
    最近更新 更多