在 Joomla 中生成 MS Word 文档

发布于 2024-11-30 14:52:57 字数 242 浏览 0 评论 0原文

我试图完成的任务:

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

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

What I try to accomplish:

  1. Admin creates MS Word document with placeholders that will be filled with data from Joomla database
  2. Admin uploades MS Word file to Joomla and connects it with SQL statement
  3. User execute "Generate MS Word" function and gets MS Word document filled with data from database.

Is there any components for Joomla that does this?
I have done this in my application using Interop libraries.

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

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

发布评论

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

评论(1

土豪 2024-12-07 14:52:57

最近我使用 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;

recently I've done this for a joomla component using phpdocx and pclzip libraries, where
a *.docx file is generated from a template file.

Config:

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

Initialize variables:

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

bind form fields with user params:

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 application form fields:

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

output the file:

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

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