问题描述
有人要求我做的PHP应用程序,将生成的图像,并在它几桌的.doc文件。我的第一个方法是:
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);
?>
此使用嵌入图像的数据URI的技术。
This uses the data uri technique of embedding an image.
这将产生将在Web浏览器中呈现好的html文件,但图像丢失在Microsoft Office Word,至少在标准设置。然后,在编辑使用Word文件,我已经从文件和Microsoft Word的图像替换图像更改的文件的内容到开放的XML和添加的文件夹,new_files他把导入的图像(这是一个巴纽),图像的一个.gif版本和xml文件:
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>
现在,这是不够好或者因为我想这是所有保存在一个单独的.doc文件。
有没有办法在处理OpenXML格式.doc文件中嵌入图像?
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?
推荐答案
看这里
<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>
这篇关于如何使用PHP,包括在一个word文件的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!