[原]office,doc,xls,txt,pdf,ppt文档在线预览及转换

最近项目想用到"文档在线预览",参考了一下 使用OpenOffice.org将各类文档转为PDF

本想用OpenOffice的类, 但OpenOffice的类太复杂了..

后来想到了Aspose ,

Google docs(谷歌文档)也是用的这个商业解决方案..

当然还有PSVIEW 大家有兴趣研究下..是开源的

但是在偷窃的心理作用下..决定利用了下Google的优良服务.

演示地址: http://game.gtmm.cn/

以下为源代码(仅供参考..切莫用于商业用途..后果自负)

  1 <?php
2
3 /* phpGoogleViewer v1.1 (Update:2012年3月15日 21:42:14),修改了一下正则匹配八进制的问题
4 * 这个类的主要作用是从Google文档上下载回文件..没有什么正式的API..所以..`Google改了.这也要改
5 * 使用的时候请注意改一下HOSTS文件....因为国内服务器都没办法访问Google Docs的.
6 * 加入如下两条记录,在服务器HOSTS文件(所以..国内虚拟主机没办法了)
7 * 203.208.45.200 docs.google.com
8 * 74.125.31.132 doc-08-c8-docsviewer.googleusercontent.com
9 *
10 * by wc1217 Time: 2012-03-09 13:11:31 更新: 2012年3月21日 14:49:28 (八进制匹配的问题)
11 */
12
13 class google_docs{
14
15 private $viewerInfo = null;
16
17 //private $decorate = '_';
18
19 function __construct(){
20 require_once 'curl_multi_class.php';
21 }
22
23 /*
24 * 得到Google Viewer转换之后的信息
25 * $url
26 * $retArray 应返回的键名
27 */
28
29 private function getUrlViewerInfo($url, $retArray = array()){
30
31 $multi = new curl_multi();
32 $multi->setUrlList(array('https://docs.google.com/viewer?url=' . urlencode($url) . '&embedded=true&mobile=true'));
33 //$multi->setOpt(array('CURLOPT_HEADER'=>1));
34 $content = $multi->exec();
35 $out = array();
36 preg_match('/\{svUrl:\\\'\?url\\\\75(https?:\/\/.*?)\\\',biUrl:\\\'\?url\\\\75(https?:\/\/.*?)\\\',chanId:\\\'(.*?)\\\',gpUrl:\\\'(https?:\/\/.*?)\\\',docId:\\\'(.*?)\\\',numPages:(\d+),gtUrl:\\\'\?url\\\\75(https?:\/\/.*?)\\\',thWidth:(\d+),dlUrl:\\\'(.*?)\\\',thHeight:(.*?)\}/', $content[0], $out);
37 if(empty($out) || count($out) != 11){
38 trigger_error('没有应有的得到响应值!', E_USER_ERROR);
39 }else{
40 array_shift($out);
41 $allArray = array_combine(array('svUrl', 'biUrl', 'chanId', 'gpUrl', 'docId', 'numPages', 'gtUrl', 'thWidth', 'dlUrl', 'thHeight'), $out); //合并键值
42 //返回指定键值
43 return empty($retArray) || !is_array($retArray) ? $allArray : array_intersect_key($allArray, array_flip($retArray));
44 }
45 }
46
47 /*
48 * 转化八进制URL
49 */
50
51 private function transFormUrl($url){
52 return preg_replace('/\\\\([0-7]{2,3})/e', 'chr(ord("\\\$1"))', $url);
53 }
54
55 /*
56 * 转换成Png图片
57 * $url type biUrl
58 * $page number
59 * @retrun array pngByte
60 */
61
62 private function getUrlToPng($url, $page, $width = '1000'){
63 $urlList = array();
64 for($i = 1; $i <= $page; $i++){
65 $urlList[] = $this->transFormUrl("https://docs.google.com/viewer?url={$url}&pagenumber={$i}&w={$width}");
66 }
67 $multi = new curl_multi();
68 $multi->setUrlList($urlList);
69 return $multi->exec();
70 }
71
72 /*
73 * 先得到文件信息
74 */
75
76 function setUrlViewerInfo($url, $retArray = array('biUrl', 'numPages')){
77 if(empty($url))
78 trigger_error('$url can not be empty!', E_USER_ERROR);
79 else
80 $this->viewerInfo = $this->getUrlViewerInfo($url, $retArray);
81 }
82
83 /*
84 * 返回的Png的Byte保存至文件
85 * $filePrefix 文件前缀
86 * $numPages 要几页?
87 */
88
89 function byteToPngFile($filePrefix = '', $numPages = 0){
90 if(empty($this->viewerInfo))
91 trigger_error('Please call setUrlViewerInfo() before runing!', E_USER_ERROR);
92 else
93 $biUrl = $this->viewerInfo;
94 $pngByte = $this->getUrlToPng($biUrl['biUrl'], empty($numPages) ? $biUrl['numPages'] : $numPages);
95 $succeed = array();
96 foreach($pngByte as $key => $value){
97 $succeed[] = file_put_contents($filePrefix . (sprintf("%02d", $key + 1)) . '.png', $value);
98 }
99 return $succeed;
100 }
101
102 /*
103 * 转换成PDF输出
104 */
105
106 function viewerToPdfFile($filePrefix = ''){
107 if(empty($this->viewerInfo))
108 trigger_error('Please call setUrlViewerInfo() before runing!', E_USER_ERROR);
109 else
110 $gpUrl = $this->viewerInfo;
111 $url = $this->transFormUrl($gpUrl['gpUrl']);
112 $multi = new curl_multi();
113 $multi->setOpt(array(/* 'CURLOPT_FOLLOWLOCATION' => 0,'CURLOPT_MAXREDIRS'=>3, */'CURLOPT_HEADER' => 1));
114 $multi->setUrlList(array($url));
115 $urlHeader = $this->transFormHeader($multi->exec()); //第一次..
116 //得到cookie 还有location
117 $cookie = explode(';', $urlHeader['Set-Cookie']); //Set-Cookie:
118 $location = $urlHeader['Location']; //Location:
119 //exit($cookie[0]);
120 //$multi->setOpt(array('CURLOPT_COOKIE' => $cookie[0], 'CURLOPT_HEADER' => 1));
121 $multi->setUrlList(array($location));
122 $urlHeader = $this->transFormHeader($multi->exec()); //第二次
123 $location = $urlHeader['Location']; //Location:
124
125 $multi->setOpt(array('CURLOPT_COOKIE' => $cookie[0], 'CURLOPT_HEADER' => 0)); //第三次..加上cookie
126 $multi->setUrlList(array($location));
127 $bytePdf = $multi->exec();
128 if(!empty($bytePdf[0]))
129 return file_put_contents($filePrefix . 'pdf.pdf', $bytePdf);
130 }
131
132 /*
133 * 转化Header为数组格式
134 */
135
136 private function transFormHeader($str){
137 $headerArray = array();
138 if(is_array($str))
139 $str = $str[0];
140 if(!empty($str) && strpos($str, "\n") !== false)
141 foreach(explode("\n", $str) as $v){
142 if(strpos($v, ': ') !== false){
143 $t = explode(': ', $v);
144 if(count($t) == 2)
145 $headerArray[$t[0]] = $t[1];
146 }
147 }
148 return $headerArray;
149 }
150
151 /*
152 * 得到文件信息,并写入文件
153 * (不完全功能)有待XML解析
154 */
155
156 function viewerToTextFile($filePrefix = ''){
157 if(empty($this->viewerInfo))
158 trigger_error('Please call setUrlViewerInfo() before runing!', E_USER_ERROR);
159 else
160 $gtUrl = $this->viewerInfo;
161 $url = 'https://docs.google.com/viewer?url=' . $this->transFormUrl($gtUrl['gtUrl']);
162 $multi = new curl_multi();
163 $multi->setUrlList(array($url));
164 return file_put_contents($filePrefix . 'text.txt', $multi->exec());
165 }
166
167 }

curl_multi的类.请引用curl_multi_class.php文件

文件来自本人上一篇文章 http://blog.csdn.net/wc1217/article/details/7332852

以下是测试文件index.php

1 <?php
2
3 require_once 'google_docs_viewer.php';
4
5 $docs = new google_docs();
6 $docs->setUrlViewerInfo('http://infolab.stanford.edu/pub/papers/google.pdf', null);
7 echo $docs->viewerToPdfFile('10123_')."\n";
8 echo $docs->viewerToTextFile('10123_')."\n";
9 print_r($docs->byteToPngFile('10123_'));

作者:wc1217 发表于2012-3-9 13:30:00 原文链接

阅读:8 评论:0 查看评论