为什么我的php生成的word文档不能打开? [英] Why can't my php generated word document be opened?

查看:504
本文介绍了为什么我的php生成的word文档不能打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的xampp htdocs文件夹中,我有两个文件:一个图像和一个php脚本。我试图用图像创建一个单词文档。这是我使用的代码:

  $ image ='img.png'; 
$ imageData = base64_encode(file_get_contents($ image));
$ src ='data:'。 mime_content_type($图像)。; BASE64, $的imageData。

header(Content-type:application / vnd.ms-word);
header(Content-Disposition:attachment; Filename = document_name.doc);

echo< html>;
echo< meta http-equiv = \Content-Type\content = \text / html;字符集= Windows的1252\ > 中;
echo< body>;
echo< h1> bla< / h1>;
echo< b>我的第一个文档< / b>;
echo'< img src =',$ src,'>';
echo< / body>;
echo< / html>;实际上我没有在我的电脑上安装Microsoft Word,但它也应该与Libre Office一起工作。我也尝试过 http://www.viewdocsonline.com ,但没有起作用。首先,我尝试了一个太大的图像,我认为这是导致问题,但它甚至不能与一个小的图像。文件只是加载所有的时间,但不能打开。文件大小似乎是正确的 - 它是52kb - 所以图像似乎在文档中。



但是可能会导致错误?如何找出和如何调试?

解决方案

Word无法读取Html,至少如果您指定 .doc 扩展名。



如果要使用最新版本的Word(2007年以来),您应该使用Docx生成器)或doc,如果你想创建一个从word 2003可读的文档。



http://www.phpdocx.com/ 非常适合( https:// phpword。 codeplex.com/ ,但不太受支持)


In my xampp htdocs folder I got two files: An image and a php script. I tried to create a word document with an image. This is the code I used:

$image = 'img.png';
$imageData = base64_encode(file_get_contents($image));
$src = 'data: '. mime_content_type($image).';base64,'.$imageData;

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<h1>bla</h1>";
echo "<b>My first document</b>";
echo '<img src="',$src,'">';
echo "</body>";
echo "</html>";

Well actually I don't have Microsoft Word installed on my PC but it should work with Libre Office too. I also tried http://www.viewdocsonline.com but it didn't work. First I tried it with a way too big image and I thought that was causing the problem but it doesn't even work with a small image. The File is just loading all the time but can't be opened. The file size seems to be right - it's 52kb - so the image seems to be in the document.

But what could cause the error? How to find out and how to debug?

解决方案

Word can't read Html, at least not if you specify the .doc extension.

You should use a Docx generator if you want to work with the latest version of Word (since 2007), or doc if you want to create a document readable from word 2003.

http://www.phpdocx.com/ works great for that (https://phpword.codeplex.com/ too, but isn't well supported)

这篇关于为什么我的php生成的word文档不能打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆