2014-01-23 114 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');