[英]Download produced file with PHPWord
I am trying to use the PHPWord plugin to convert some HTML into .docx 我正在尝试使用PHPWord插件将一些HTML转换为.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,
但是当我下载文件时,我只会弄乱像这样的
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,
: 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. 我已经搜索了Web,但是不知道如何进行。
just use the builtin functionality as follows 只需使用以下内置功能
$phpWord->save('teste.docx', 'Word2007', true);
The last parameter will force a download of the produced file. 最后一个参数将强制下载生成的文件。
add a space between attachement;
在
attachement;
之间添加空间attachement;
and filename="teste.docx"
so the header will be 和
filename="teste.docx"
因此标题为
header('Content-Disposition: attachment; filename="teste.docx"');
and add the following header: 并添加以下标头:
header('Content-Description: File Transfer');
Update 更新
You can try saving the file to disk and sending from it, I also head problems like this and it solved for me: 您可以尝试将文件保存到磁盘并从中发送,我也遇到了类似的问题,它为我解决了:
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($filePath);
header("Expires: Mon, 1 Apr 1974 05:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document;');
header("Content-Disposition: attachment; filename=file.docx");
readfile($filePath);
unlink($filePath);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.