odtphp: A nice library to output openoffice odt file

Recently, I want to add a function in our data center website(Which is only Chinese version now, an English version is preparing): “output the geographic metadata to openoffice odt format”. So, the odtphp project came into my sight.

After download the 1.0.1 version, and try some tests in its code. There is no problem in the text replacing, but if I insert into the odt file an image, the OpenOffice (What I use is LibreOffice) always tell me this file need repair, although the repair result nice. Dig into another search, find this problem and solution in the author’s blog.

Use the latest version (1.3) in the author’s blog, this bug is disappeared.

There is another requirement in my site yet, maybe it is not a problem: The image file which is dynamic produced has not a extension in the url, So the code in odtphp: setImage must change its behavior here.

So I patched the code, and released it here (it’s GPL license):

wlx@wlxpc:~$ diff odf.php odtphp-1.0.1_modified_files/odf.php
111a112,113
> $filename = strtok(strrchr($value, '/'), '/.');
> $file = substr(strrchr($value, '/'), 1);
116,118d117
< $filename=md5($value);; < $imgext=array('.gif','.jpg','.png','.swf','.psd','.bmp','.tif','.tif','.jpc','.jp2','.jpx','.jb2','.swc','.iff','.wbmp','.xbm','.ico'); < $file=$filename.$imgext[$size[2]]; 287,289c286 < //$this->file->addFile($imageKey, 'Pictures/' . $imageValue);
< $this->file->addFromString('Pictures/' . $imageValue,file_get_contents($imageKey));
< --- > $this->file->addFile($imageKey, 'Pictures/' . $imageValue);

In short, what I do: change the filenmae to a hash string (md5(source)), and save it with file_get_contents function (which could parse the url).

And, a big Thanks to you and your odtphp project: Vikas Mahajan.
Download patch file: odf.php.patch


已发布

分类

来自

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注