[原]office(doc,xls,txt,pdf,ppt)文檔在線預覽及轉換(office2pdf) - PHP版


最近項目想用到"文檔在線預覽",參考了一下 使用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 查看評論


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM