代码之家  ›  专栏  ›  技术社区  ›  godot

使用PhpWord生成ms word文档

  •  2
  • godot  · 技术社区  · 7 年前

    我将使用PhpWord生成一些文档,我在其中设置了一些草稿文档 client_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 我有这个消息: enter image description here

    当我单击时 好啊 : enter image description here

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   feeela    6 年前

    我刚刚加上 ob_clean() 我的代码在阅读之前,一切正常。

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

    此函数不会像ob\u end\u 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);