PHPword怎么在载入的模板中插入一张图片? 5
2024-07-20 广告
2018-03-19 · 知道合伙人互联网行家
我就写了个demo看一下,事实证明是可以的。
[php] view plain copy
<?php
date_default_timezone_set("Asia/Shanghai");
require_once "PHPWord-develop/bootstrap.php";
//读取文件
$file = __DIR__ . "/demo.docx";
$word = \PhpOffice\PhpWord\IOFactory::load($file);
//增加图片
$image = __DIR__ . "/demo.jpg";
$section = $word->addSection();
$section->addImage($image);
//生成文件
$name = "HelloWorld.docx";
$wordWriter = \PhpOffice\PhpWord\IOFactory::createWriter($word, "Word2007");
$wordWriter->save($name);
//下载文件
$url = str_replace("demo.php", $name, $_SERVER["REQUEST_URI"]);
header("Location: $url");