本质上的道理就是 输出如下的两行头,然后 后面 跟着是 html 的代码吧
drupal_add_http_header('Content-Type', 'application/vnd.ms-word; utf-8');
drupal_add_http_header('Content-Disposition', 'attachment; filename="' . check_plain($node->title) . '.doc"');
1) https://www.drupal.org/project/node_to_word (drupal6 的模块)
2) https://www.drupal.org/files/node_to_word-7.x-1.x.tar_.gz (drupal7 的模块) ( 见 (https://www.drupal.org/node/1462686 这个页面有说明 )
在 drupal7 中, 启用后 在节点页面,看到 下载 word 的链接

但是 下载后发现 word里 图像不显示,原因是 节点里面的 图像 是不带 http://域名的,所以
我们要 hook 一下代码
node_to_word.module 里面约 206 行
function theme_node_to_word_doc_output($variables) {
$node = $variables['node'];
$build = node_view($node, 'full');
$build['links']['#access'] = FALSE;
$output = drupal_render($build);
return $output;
}
在 模板的 template.php 文件 里面 hook 它,,也就是 加上红包的两行,_chuli_img_absolute 作用是 给里面的
'<img src="/sites.....' 变成 '<img src="'.'http://域名'./sites.....'
function bartik_clone_node_to_word_doc_output($variables) {
$node = $variables['node'];
$node->body['und'][0]['value'] = _chuli_img_absolute($node->body['und'][0]['value']);
$node->body['und'][0]['safe_value'] = _chuli_img_absolute($node->body['und'][0]['safe_value']);
$build = node_view($node, 'full');
$build['links']['#access'] = FALSE;
$output = drupal_render($build);
return $output;
}
_chuli_img_absolute 函数一般写在 模块里,函数内容如下
function _get_img_absolute($matches){
if(_start_with($matches[2],'http')){//是以 http 开头的绝对路径,就原样返回
return $matches[0];
}else{ //一般是以 / 开头的 路径,比如 /sites/default/files/ueditor/1/upload/catcher/20211230/1640851521400479.png
return str_replace('src="','src="'.$GLOBALS['base_url'],$matches[0]);
}
}
function _chuli_img_absolute($body_str)
{
return preg_replace_callback('/<img(.*)src=["\']([^>]+)["\'](.*)\/>/misU','_get_img_absolute',$body_str);
}
进行了如下的代码修改,下载 转化后的 word 后, 里面的图像能正常显示