惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
T
Tailwind CSS Blog
V
Visual Studio Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
月光博客
月光博客
H
Hacker News: Front Page
D
DataBreaches.Net
GbyAI
GbyAI
Recorded Future
Recorded Future
IT之家
IT之家
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Schneier on Security
Schneier on Security
P
Privacy International News Feed
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
博客园 - 三生石上(FineUI控件)
M
MIT News - Artificial intelligence
Google Online Security Blog
Google Online Security Blog
L
LINUX DO - 最新话题
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
L
LangChain Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
The Last Watchdog
The Last Watchdog
I
Intezer
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hacker News - Newest:
Hacker News - Newest: "LLM"
Stack Overflow Blog
Stack Overflow Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
U
Unit 42
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Schneier on Security
T
Tenable Blog
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
B
Blog
S
Security @ Cisco Blogs
A
About on SuperTechFans
V
V2EX
T
The Exploit Database - CXSecurity.com

博客园 - 浊浊然

kkfileview5 安装 css毛玻璃效果 createjs 实现场景拖动、滚轮放大缩小 createjs 实现图片拖动 node-sass 安装不上的问题 nodejs 搭建本地服务器 微信小程序,真机上接口调不通 mysql 去除字段中 空格 制表符等 用jeesite做后台开发,新写了个前端业务系统的权限问题 uniapp 打包app 用到地图相关 升级node最新版本18.x .Error: error:0308010C js 读取url中参数值 linux php 安装oci8 electron nativefier打包网址 electron-winstaller制作安装包 h5+ 上传图片(选择图片、拍照) Vscode 右键 open with code 没有的情况,使用以下注册表脚本 PhpSpreadsheet导出excel apache https小程序android ssl error js获取url参数,以及中文乱码问题 微信js上传图片并 展示,iphone下预览
phpword 导出word,文件已损坏问题
浊浊然 · 2019-11-21 · via 博客园 - 浊浊然

win10下始终都是正常的,linux下下载的word文档始终提示文件损坏之类的

1.使用phpword创建一个word文档

$phpWord = new \PhpOffice\PhpWord\PhpWord();
//各种操作
if(ob_get_length()) ob_end_clean();//linux加上这一句可能就不会再有文件损坏的问题了,我没测试,因为发现使用word模版会更方便,如2
$filename = '文件名.docx';
$phpWord->save($file,'Word2007',true);//下载文档第三个参数为true

2.创建一个word文件,phpword进行加载,替换word中的模版变量就可以,觉得这个很实用,在word中添加变量名,类似:${name}

        $docxname = './static/test.docx';
$document = new TemplateProcessor($docxname);
$document->setValues(array('name'=>'姓名'));//word文件中的 ${name}会被替换为姓名
// $document->setImageValue('CompanyLogo', 'path/to/company/logo.png');
$filename = '文件名.docx';
$testPath = './runtime/test_'.uniqid().'.docx';
$document->saveAs($testPath);//php://output
// 下载Word文件
if(ob_get_length()) ob_end_clean();//这一句必须,没有这一句,linux生成的word会提示文件损坏
header('Cache-control: max-age=360');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: '.filesize($testPath));
header('Content-Transfer-Encoding: binary');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Expires: '.gmdate("D, d M Y H:i:s", time() + 360) . ' GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()) . ' GMT');
header('Pragma: public');
ob_start(); //打开缓冲区
echo file_get_contents($testPath);
@unlink($testPath);
ob_end_flush();//输出全部内容到浏览器
exit();

其他操作直接查看phpword官方文档

php://output