您当前的位置: 首页 >  ar

知其黑、受其白

暂无认证

  • 0浏览

    0关注

    1250博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

ZipArchive::getFromName(): Invalid or uninitialized Zip object

知其黑、受其白 发布时间:2021-07-19 11:55:29 ,浏览量:0

PHPword操作word报错处理
  • 解决办法:调用php自带的zip服务
    • 原因解释
composer.json 扩展包

"phpoffice/phpword": "^0.18.2",

开发框架:laravel5.8 报错提示:ZipArchive::getFromName(): Invalid or uninitialized Zip object 在这里插入图片描述 问题描述:在用 new \PhpOffice\PhpWord\TemplateProcessor(‘1.docx’)进行模板替换时出现错误

问题原因:可能有多种原因造成:比如没打开php_zip.dll、或者php_zip.dll缺失……。

在排除以上各种原因后,本人遇到这个错误,原因大概是因为:phpword需要调用zip软件打开word,但服务器上没有安装zip软件(后来安装了,也没用,不知道是不是要重启,因为有程序正在运行,就没重启)

解决办法:调用php自带的zip服务

具体方法为:在调用模板前加上

\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);

将自带是PCLZIP替换需要的zip服务。

\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
$tmp= new \PhpOffice\PhpWord\TemplateProcessor($file);//打开模板
原因解释

默认情况下,PHPWord 使用 Zip 扩展名来处理 ZIP 压缩档案和其中的文件。

如果您的服务器上无法安装 Zip 扩展,则可以使用 PHPWord 中包含的纯 PHP 库替代品 PclZip。

来自文档:https://phpword.readthedocs.io/en/latest/general.html#phpword-settings

关注
打赏
1665558895
查看更多评论
立即登录/注册

微信扫码登录

0.0579s