简体   繁体   中英

how to use php to include an image in a word file?

Somebody has asked me to make an app in php that will generate a .doc file with an image and a few tables in it. My first approach was:

<?php
function data_uri($file, $mime) 
{  
  $contents = file_get_contents($file);
  $base64   = base64_encode($contents); 
  return ('data:' . $mime . ';base64,' . $base64);
}
$file = 'new.doc';
$fh = fopen($file,'w');
$uri = data_uri('pic.png','image/png');
fwrite($fh,'<table border="1"><tr><td><b>something</b></td><td>something else</td></tr><tr><td></td><td></td></tr></table>
<br/><img src="'.$uri.'" alt="some text" />
<br/>
<table border="1"><tr><td><b>ceva</b></td><td>altceva</td></tr><tr><td></td><td></td></tr></table>');
fclose($fh);
?>

This uses the data uri technique of embedding an image.

This will generate an html file that will be rendered ok in web browsers but the image is missing in Microsoft Office Word, at least in the standard setup. Then, while editing the file with Word, i've replace the image with an image from file and Microsoft Word changed the contents of the file into Open XML and added a folder, new_files where he put the imported image (which was a .png), a .gif version of the image and a xml file:

<xml xmlns:o="urn:schemas-microsoft-com:office:office">
  <o:MainFile HRef="../new.doc" /> 
  <o:File HRef="image001.jpg" /> 
  <o:File HRef="filelist.xml" /> 
</xml>

Now this isn't good enough either since i want this to be all kept in a single .doc file. Is there a way to embed an image in an OpenXML-formatted .doc file?

look here http://www.tkachenko.com/blog/archives/000106.html

<w:pict>
    <v:shapetype id="_x0000_t75" ...>
    ... VML shape template definition ...
    </v:shapetype>
    <w:binData w:name="wordml://02000001.jpg">
    ... Base64 encoded image goes here ...
    </w:binData>
    <v:shape id="_x0000_i1025" type="#_x0000_t75" 
      style="width:212.4pt;height:159pt">
         <v:imagedata src="wordml://02000001.jpg" 
           o:title="Image title"/>
    </v:shape> 
</w:pict>

There is PHPWord project to manipulate MS Word from within PHP.

PHPWord is a library written in PHP that create word documents. No Windows operating system is needed for usage because the result are docx files (Office Open XML) that can be opened by all major office software.

PHPWord可以将它们写成http://phpword.codeplex.com/ (注意:它仍处于测试阶段。我已经使用了很多同一个人的PHpExcel ...从未尝试过Word版本)。

Have a look at the phpdocx library for generating real .docx files rather than html files with a .doc extension

PS the extension should strictly be .docx rather than .doc for Open XML Word 2007 files

OpenTBS can create DOCX (and other OpenXML files) dynamic documents in PHP using the technique of templates.

No temporary files needed, no command lines, all in PHP.

It can add or delete pictures. The created document can be produced as a HTML download, a file saved on the server, or as binary contents in PHP.

It can also merge OpenDocument files (ODT, ODS, ODF, ...)

http://www.tinybutstrong.com/opentbs.php

如果富文本可以,请尝试PhpRtf

I would use PHPExcel. It can work with OpenXML too.

Here's the link: http://phpexcel.codeplex.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM