duanmo6937 2017-11-21 13:56
浏览 648
已采纳

使用PHPWord下载生成的文件

I am trying to use the PHPWord plugin to convert some HTML into .docx

But when i download the file, I get only messed up charecters like: PK########�^uK�j�#c###!#######[Content_Types].xml���N�0#E�|E�-Jܲ@#5��#*Q>5'��_�8}�=��D#AC�v#)��s�G�G��5� "j�J6,#,#'��nQ���s~�2L�)a���m#�d|5�m#F�#K�L)�s�r V�#8�T>Z��5.x#�C,��

Here is my code:

<?php
error_reporting (E_ALL | E_STRICT);
@ini_set ('display_errors', 'on');
require_once '../../../../vendor/autoload.php';

$my_content = $_POST['html_content'];

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();

\PhpOffice\PhpWord\Shared\Html::addHtml($section, $my_content);

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachement;filename="teste.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('php://output');
?>

I already searched into the Web but got no clue how to proceed.

  • 写回答

2条回答 默认 最新

  • dongyun234854 2017-12-26 20:57
    关注

    just use the builtin functionality as follows

    $phpWord->save('teste.docx', 'Word2007', true);
    

    The last parameter will force a download of the produced file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog