使用PHP将图像插入docx文件的最简单方法



我正在尝试在网站上设置一个导出按钮,该按钮将从数据库中导出一些信息并将其放入.doc文件中。我目前正在这样做:

$filename = "File.doc"; 
header('Content-Type: application/octet-stream');    
header("Content-Disposition: attachment; filename=$filename");
echo "<html><meta http-equiv="Content-Type" content="text/html; charset=Windows-1252"><body><img src='the_img.png' width='250px'><br><br><b style='font-size: 40px;'><u><center>Header</center></u></b><br><br><ol>";
$fileData = "SELECT `Songs` FROM `table`;";
$info = $con->query($fileData);
while ($the_row = $info->fetch_assoc()) {
$content = $the_row["column"];
}
$arr = explode("|", $content);
for($i = 0; $i < count($arr); $i++){
echo "<li>" . $arr[$i] . "</li>";
}
echo "</ol></body></html>";
mysqli_close($con);
exit;

当它导出.doc文件时,它会在 Word Microsoft中成功打开,但是在您进入编辑模式之前,图像似乎已损坏。进入编辑模式后,图像不会设置特定大小。有什么建议吗?

我认为问题出在这一行

echo "<html><meta http-equiv="Content-Type" content="text/html; charset=Windows- 
1252"><body><img src='the_img.png' width='250px'><br><br><b style='font-size: 40px;'> 
<u><center>Header</center></u></b><br><br><ol>";

您必须将图像的名称更改为没有空格,并在"宽度"之前添加"样式",如下所示

<img src="theimg.png" style="width:250px"/>

最新更新