Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PhpWord生成ms word文档_Php_Ms Word_Ms Office_Phpword - Fatal编程技术网

使用PhpWord生成ms word文档

使用PhpWord生成ms word文档,php,ms-word,ms-office,phpword,Php,Ms Word,Ms Office,Phpword,我将使用PhpWord生成一些文档,我有一些草稿文档,在这里我动态地设置了client\u name $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($draftUrl); $templateProcessor->setValue("client_name", $clientName); $filename = '30.docx'; $templateProcessor->saveAs($filename)

我将使用PhpWord生成一些文档,我有一些草稿文档,在这里我动态地设置了
client\u name

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($draftUrl);
$templateProcessor->setValue("client_name", $clientName);

$filename = '30.docx';
$templateProcessor->saveAs($filename);
然后我读了这个文件,用户可以下载它

header('Content-Type: application/octet-stream');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename='$filename'");
readfile($filename);
当我试图通过MsOffice打开它时,我收到以下消息:

当我单击“确定”时:

在接受这个之后,我终于看到我的文件准备好了。生成可读文档需要什么

我只是在阅读之前添加了代码,一切正常

此函数用于丢弃输出缓冲区的内容

此函数不会像ob_end_clean()那样破坏输出缓冲区 是的

因此,我确保不希望的输出不会进入生成的文档中

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($draftUrl);
$templateProcessor->setValue("client_name", $clientName);

$filename = '30.docx';
$templateProcessor->saveAs($filename);

header('Content-Type: application/octet-stream');
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-Disposition: attachment; filename='$filename'");
ob_clean();
readfile($filename);

您是否尝试在文本编辑器中打开word文件?可能是一些html输出进入了文件并损坏了它。我试图用写字板打开,但有无法读取的符号,但文档中没有任何html输出。接受此msword后,恢复文档将成为所需的两个建议:1)添加内容长度:
标题(“内容长度:”.filesize(basename($filename))和/或2)添加特定的mime类型
头('Content-type:application/vnd.openxmlformats-officedocument.wordprocessingml.document')。如果这些都没有任何区别,那么如果注释掉模板值设置,代码是否有效?也就是说,要查明问题是由模板和文件服务(更有可能)还是由设置中的无效内容(不太可能)引起的。@ejuhjav谢谢!我解决了这个问题,并把问题解决了answer@pat-och编辑问题或答案时,请不要添加“已编辑”评论。如果以及何时编辑了此类记录,则答案或问题下方始终可见。当然,但当我只想改进两行代码的格式时,我得到了“编辑必须至少为6个字符;此帖子中是否还有其他需要改进的内容”。。。那么如何进行这种改进呢?