用PHPWord下载产生的文件 [英] Download produced file with PHPWord

查看:68
本文介绍了用PHPWord下载产生的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHPWord 插件将一些HTML转换为.docx

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

但是当我下载文件时,我只会弄乱像这样的字符:
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 V.#8 T>Z 5.x# C,

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,��

这是我的代码:

<?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.

推荐答案

只需按以下方式使用内置功能

just use the builtin functionality as follows

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

最后一个参数将强制下载生成的文件。

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

这篇关于用PHPWord下载产生的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆