先说一下我的环境:
客户端:
操作系统:Windows 10 专业版20H2(64 位)内部版本:19042.870
浏览器:Microsoft Edge版本 89.0.774.75 (官方内部版本) (64 位)
服务器:
操作系统:CentOS 8.3.2011
Nginx版本:1.14.1
PHP版本:7.2.24
Yii2版本:2.0.14
phpoffice/phpword版本:0.18.1
今天,在Yii2下面用PhpWord生成了Word文件。
打开文件后提示:很抱歉,无法开test.docx,因为内容有问题。
点击详细信息后显示:The file is corrupt and cannot be opened
点击确定后显示:Word在"test.docx"中发现无法读取的内容。是否恢复此文档的内容?如果您信任此文档的来源,请点击"是"。
点击是以后显示:使用Microsoft Word无法打开此文件。是否搜索Office.com来查找可打开文件的转换器?
选择打开:即可正常打开文件。
本来这样也能正常使用,但是不解决这个问题,总感觉心里不舒服。
上次用phpexcel生成的excel文件能正常打开使用,看看那段代码咋弄的,直接拿过来试试。
这里直接给出答案:
ob_end_clean();
ob_start();
header('Content-Type : application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition:attachment;filename="test.docx"');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$xmlWriter->save("php://output");
完整的利用phpoffice/phpword生成word文档的代码示例(这是在生产环境下用的代码,确保可以正常使用,但是要注意phpword版本一定要一致):
$PHPWord = new \PhpOffice\PhpWord\PhpWord();
$PHPWord->setDefaultFontName('SimSun');
$PHPWord->setDefaultFontSize(11);
$properties = $PHPWord->getDocInfo();
$properties->setCreator('张三');
$properties->setCompany('公司');
$properties->setTitle('文档');
$properties->setDescription('描述');
$properties->setLastModifiedBy('张三');
$properties->setCreated( time() );
$properties->setModified( time() );
$PHPWord->addFontStyle('titleStyle', ['size' => 16, 'bold' => true]);
$PHPWord->addFontStyle('fStyle15', ['size' => 15]);
$PHPWord->addFontStyle('fStyle14', ['size' => 14]);
$PHPWord->addFontStyle('fStyle12', ['size' => 12]);
$PHPWord->addFontStyle('fStyle11', ['size' => 11]);
$PHPWord->addParagraphStyle('Center', ['align' => 'center']);
$PHPWord->addParagraphStyle(
'Normal', [
'align' => 'both',
'spaceBefore' => 0,
'spaceAfter' => 0,
'spacing' => 56,
'lineHeight' => 1.19,
'indentation' => [
'firstLine' => 0
]
]
);
$PHPWord->addParagraphStyle(
'firstLine', [
'indentation' => [
'firstLine' => 500
]
]
);
$sectionStyle = [
'orientation' => null,
'marginLeft' => 1799,
'marginRight' => 1799,
'marginTop' => 1442,
'marginBottom' => 1442,
'pageNumberingStart' => 1,
'headerHeight' => 851,
'footerHeight' => 993,
];
$section = $PHPWord->addSection($sectionStyle);
$section->addText('服务合作协议', 'titleStyle', 'Center');
$section->addTextBreak(1, 'fStyle15');
$section->addText('甲方:*****有限公司', 'fStyle15');
$section->addText('乙方:张三', 'fStyle15');
$section->addTextBreak(1);
$section->addText('上述各方经平等自愿协商,签订本合同以共同遵守。');
$section->addTextBreak(1);
$section->addText('合作范围', 'fStyle14');
$section->addText('甲方委托乙方进行****服务');
$section->addText('合作期限', 'fStyle14');
$section->addText('委托期2021年4月14日至2022年4月13日。本协议所拟合作期届为壹年,经双方协商同意,双方可续签协议。', 'fStyle11', 'firstLine');
$section->addText('双方责任与权力', 'fStyle14');
$section->addText('甲方应积极配合乙方,提供合法的、真实的、准确的、完整的资料,如因甲方提供的资料而引起的法律纠纷,由甲方负责。', 'fStyle11', 'firstLine');
$section->addText('甲方应按照本协议的规定及时付款。', 'fStyle11', 'firstLine');
$section->addText('乙方应积极配合甲方的工作,按时、保质、保量完成甲方委托的各项工作,并为甲方资料保密。', 'fStyle11', 'firstLine');
$section->addText('收款条例', 'fStyle14');
$section->addText('结算方式与时间:自双方合同结束之7日期内甲方将服务费用100.00元汇至乙方指定的账户。乙方收款后将开具普通发票给甲方,费用自付。', 'fStyle11', 'firstLine');
$section->addText('如在协议执行过程中产生的争议,双方协商解决,若协商不成,任何一方可向签约地的法院提起申诉。', 'fStyle11', 'firstLine');
$section->addText('本协议一式两份,甲、乙双方各一份,自签约日期起生效。', 'fStyle11', 'firstLine');
$section->addTextBreak(5);
$section->addText('甲方:********有限公司', 'fStyle12');
$section->addText('乙方:', 'fStyle12');
$section->addText('日期:2021年4月14日 日期: 2021年4月14日', 'fStyle12');
$filename = 'test.docx"';
ob_end_clean();
ob_start();
header('Content-Type : application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition:attachment;filename=' . $filename);
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');
$xmlWriter->save("php://output");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)