2014-01-23 63 views
0

在我的xampp htdocs文件夹中有两个文件:一个图像和一个php脚本。我试图用图像创建一个word文档。这是我使用的代码:为什么我的php生成的word文档无法打开?

$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>"; 

嗯,事实上我没有做我的电脑上安装了Microsoft Word,但它应该与自由报办公室工作过。我也试过http://www.viewdocsonline.com,但它没有奏效。首先,我尝试了一种太大的图像,我认为这是造成这个问题的原因,但它甚至不适用于小图像。该文件只是加载所有的时间,但无法打开。文件大小似乎是正确的 - 它是52kb - 所以图像似乎在文档中。

但是什么会导致错误?如何找出和如何调试?

回答

1

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

,如果你想使用Word的最新版本(2007年起),或文档工作,如果你想创建字可读的文件应该使用一个DOCX发电机2003

http://www.phpdocx.com/对于伟大工程( https://phpword.codeplex.com/也是如此,但没有得到很好的支持)

+0

THX!但有可能只用上面的方法创建一个带有文本的工作文档文档。只是添加图片导致了问题。我试过phpdocx,它有可能创建一个带有文本和图像的文档文档。不幸的是,你不能格式化社区版本的phpDocx中的文本,其他版本非常昂贵:(http://www.phpdocx.com/documentation/features – user2718671

+1

是的,这是真的,PHPDocx是非常昂贵的。一个使用JS的web应用程序,你可以看看我创建的库,https://github.com/edi9999/docxtemplater。它是一个模板(也就是说你不能从头开始创建一个词,但需要创建一个模板),但你可以看看这里的演示:http://javascript-ninja.fr/docxgenjs/examples/demo.html – edi9999

+0

谢谢!你创建的工具似乎是强大和有用的。出来如何安装和如何使用我会试试看:) – user2718671

0

好吧 - 在edi9999和他的超酷图书馆的帮助下,我能够用我的文本变量和图像创建一个docx文档。

这里是我使用的代码: 的Javascript:

/*** importing all necessary scripts ***/ 
<script type="text/javascript" src="docxtemplater-master/libs/base64.js"></script> 
<script type="text/javascript" src="docxtemplater-master/libs/jszip/jszip.js"></script> 
<script type="text/javascript" src="docxtemplater-master/libs/jszip/jszip-load.js"></script> 
<script type="text/javascript" src="docxtemplater-master/libs/jszip/jszip-inflate.js"></script> 
<script type="text/javascript" src="docxtemplater-master/js/docxgen.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
/*** my JSON object with the variables for setTemplateVars() ***/ 
<script type="text/javascript"> 
      var JSON = { "articles": [ 
       { "title": "test-title", "first_name": "Paul", "last_name": "Alan", "phone": "555-nose", "fileName": "abc" }, 
       { "title": "test-title2", "first_name": "John", "last_name": "Doe", "phone": "555-abcd", "fileName": "bcd" } 
      ] 
      }; 
</script> 
<script type="text/javascript"> 
var xhrDoc = new XMLHttpRequest(); 
xhrDoc.open('GET', 'Example.docx', true); 
if (xhrDoc.overrideMimeType) { 
    xhrDoc.overrideMimeType('text/plain; charset=x-user-defined'); 
} 

xhrDoc.onreadystatechange = function (e) { 
    if (this.readyState == 4 && this.status == 200) { 
     window.docData = this.response; 
    } 
}; 
xhrDoc.send(); 


var xhrImage = new XMLHttpRequest(); 
xhrImage.open('GET', 'dog.jpg', true); 
      if (xhrImage.overrideMimeType) { 
       xhrImage.overrideMimeType('text/plain; charset=x-user-defined'); 
      } 

      xhrImage.onreadystatechange = function (e) { 
       if (this.readyState == 4 && this.status == 200) { 
        window.imgData = this.response; 
       } 
      }; 
      xhrImage.send(); 

generateDoc = function (docData) { 
      var doc = new DocxGen(docData) 

      doc.setTemplateVars(
       { "first_name": JSON.articles[0]["first_name"], 
        "last_name": JSON.articles[0]["last_name"], 
        "phone": JSON.articles[0]["phone"], 
        "fileName": JSON.articles[0]["fileName"] 
       } 
      ) 
      doc.applyTemplateVars() 
      imageList = doc.getImageList() 
      console.log(imageList); 
      doc.setImage(imageList[0].path, imgData); 
      doc.output() 
     } 

</script> 

HTML:

<button class="download_doc" onClick="generateDoc(window.docData)">download word docx with image</button> 

Word模板:

{phone} 
Product name: {first_name} 
Product reference : {last_name} 
*in the middle is an image* 
Blabla: {fileName} 
*here is another image* 

好内容是没有当然的道理,但它作品。但仍然有一些问题留下(特别是对edi9999,我希望你能回答我请:)):

1.图像必须在同一台服务器上,你必须使用相对路径, 对? Link的似乎没有工作。我试过xhrImage.open('GET', 'http://link-to-an-image.com/image.jpg', true);但没有成功。是否有可能使用外部链接来图像?

2. GET请求后面的控制台中有一个304错误(“未修改”)。这是正常的吗?

3.用来代替word文档中的图像的图像是否必须具有相同的尺寸(或至少是相同的纵横比)还是有任何选项变量可以使替换更灵活?例如:如果我想让图像在Word文档中显示完整宽度并获得具有不同宽高比的替换图像,是否可以保持该宽高比,并且只是增加Word文档中图像的高度?

4.如何使用多个图像进行更换?用xhrImage.open('GET', 'dog.jpg', true);只打开一个图像。如何添加更多的图像到“imageList”以及如何确定顺序?

5。该库基于原型,通常会导致在一个文档中同时使用(jQuery + Prototype)框架的错误。但我试图使用jQuery函数,它的工作。你有没有在一个文档中使用你的库和jQuery的问题?

0

发现了另一种解决方案: 与此库:https://github.com/PHPOffice/PHPWord

它很容易创建具有格式的文本和图像的DOCX文件。

这里是为我工作的代码:

require_once('PHPWord-master/Classes/PHPWord.php'); 
$PHPWord = new PHPWord(); 
$section = $PHPWord->createSection(); 
$my_text='Hello world! I am formatted.'; 
$section->addText($my_text, array('name'=>'Tahoma', 'size'=>16, 'bold'=>true)); 
$section->addText(''); // adding some white space because the marginTop attribute of the image doesn't work 
$filename="Jellyfish.jpg"; 
$size = getimagesize($filename); 
$width="560"; //full width in a word document if image is 96dpi 
$height=560/intval($size[0])*intval($size[1]); 
$section->addImage(
$filename, 
array(
    'width' => $width, 
    'height' => $height, 
    'align' => 'center' 
) 
); 
$section->addText('blabla'); 
$filename="dog.jpg"; 
$size = getimagesize($filename); 
$height=560/intval($size[0])*intval($size[1]); 
$section->addImage(
$filename, 
array(
    'width' => $width, 
    'height' => $height, 
    'align' => 'center' 
) 
); 
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); 
$objWriter->save('helloWorld.docx');