PHP(Zend) & Smarty - 生成 .doc 文件和 http 标头时出现问题

发布于 2024-11-26 18:07:33 字数 1003 浏览 2 评论 0原文

我在一个项目中使用 Zend Framework 和 Smarty 模板生成器,我对这项工作以及 ZF 和 Smarty 都是新手。不管怎样,在一个操作中,有一个用于生成 .doc 报告的可点击链接。单击后,它会使用 HtmlToDoc类通过这种方法:

$htmltodoc_instance -> createDoc($body ,
                  'filename.doc',
                   TRUE);  
/* the last parameter TRUE means: download or save the file. it set the HTTP headers 
'Content-type: application/octet-stream' and 'Content-Disposition: attachment; 
 filename="..."', forcing the browser to open a download dialog for filename.doc

之前已经通过

 $this -> view -> obj_to_render = $obj_to_render.

在视图中放置了obj_to_render来渲染当前的action,而$obj_to_render一般是Smarty格式化的html代码模板引擎。 我不知道这是否是原因,但似乎这导致了问题:.doc 下载对话框的标题在某种程度上被视图渲染的下一个标题“覆盖”,因此 .doc 文件的输出不成功。我尝试在 createDoc() 方法调用之后放置 exit() ,但它确实有很大帮助。输出有所不同,但它仍然没有显示我需要的信息。有人知道如何划分这两个操作然后获得正确的文件输出吗? 谢谢!

i'm using the Zend Framework and Smarty Template generator on a project, i'm new to this work and also to ZF and Smarty. Anyway, on an action there's a clickable link for a .doc report generation. Once clicked, it generates a .doc file using HtmlToDoc class by this method:

$htmltodoc_instance -> createDoc($body ,
                  'filename.doc',
                   TRUE);  
/* the last parameter TRUE means: download or save the file. it set the HTTP headers 
'Content-type: application/octet-stream' and 'Content-Disposition: attachment; 
 filename="..."', forcing the browser to open a download dialog for filename.doc

before that, it has been renderd the current action by

 $this -> view -> obj_to_render = $obj_to_render.

with obj_to_render placed in the view, while $obj_to_render is generally the formatted html code by Smarty template engine.
I don't know if it is the cause, but it seems that this cause the problem: the header of .doc download dialog is somehow 'overriden' by the next header of view's rendering, and so the .doc file's output is not succesful. i've tried to put an exit() after createDoc() method call, but it doens help much. output is diferent, but it still doesn't display the informations i need. Anybody know how to divide this two operations and then obtain the right file output?
thanks!

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

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

发布评论

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

评论(1

小兔几 2024-12-03 18:07:33
public function h2dAction()
{
    $content = '<strong>wow! it\'s alive!</strong>';

    $htmltodoc = new HTML_TO_DOC();
    $htmltodoc->docFile = 'wow.doc';
    @$htmltodoc->createDoc($content, $htmltodoc->docFile, true);
    die;
}

GDocs 可以很好地处理此文件。无法在 MS Word 上进行测试,我没有它:)

public function h2dAction()
{
    $content = '<strong>wow! it\'s alive!</strong>';

    $htmltodoc = new HTML_TO_DOC();
    $htmltodoc->docFile = 'wow.doc';
    @$htmltodoc->createDoc($content, $htmltodoc->docFile, true);
    die;
}

GDocs works fine with this file. cant test on MS Word, i dont have it :)

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