1. 程式人生 > >office(doc,xls,txt,pdf,ppt)文件線上預覽及轉換(office2pdf)

office(doc,xls,txt,pdf,ppt)文件線上預覽及轉換(office2pdf)

本想用OpenOffice的類, 但OpenOffice的類太複雜了..

Google docs(谷歌文件)也是用的這個商業解決方案..

當然還有PSVIEW 大家有興趣研究下..是開源的

但是在偷竊的心理作用下..決定利用了下Google的優良服務.

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

以下為原始碼(僅供參考..切莫用於商業用途..後臺自負)

<?php

/*
 * 這個類的主要作用是從Google文件上下載迴文件..沒有什麼正式的API..所以..琰`Google改了.這也要改
 * 使用的時候請注意改一下HOSTS檔案....因為國內伺服器都沒辦法訪問Google Docs的.
 * 加入如下兩條記錄,在伺服器HOSTS檔案(所以..國內虛擬主機沒辦法了)
 * 203.208.45.200	docs.google.com
 * 74.125.31.132        doc-08-c8-docsviewer.googleusercontent.com
 * 
 * by wc1217 Time: 2012-03-09 13:11:31
 */

class google_docs{

    private $viewerInfo = null;

    //private $decorate = '_';

    function __construct(){
        require_once 'curl_multi_class.php';
    }

    /*
     * 得到Google Viewer轉換之後的資訊
     * $url
     * $retArray 應返回的鍵名
     */

    private function getUrlViewerInfo($url, $retArray = array()){

        $multi = new curl_multi();
        $multi->setUrlList(array('https://docs.google.com/viewer?url=' . urlencode($url) . '&embedded=true&mobile=true'));
        //$multi->setOpt(array('CURLOPT_HEADER'=>1));
        $content = $multi->exec();
        $out = array();
        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);
        if(empty($out) || count($out) != 11){
            trigger_error('沒有應有的得到響應值!', E_USER_ERROR);
        }else{
            array_shift($out);
            $allArray = array_combine(array('svUrl', 'biUrl', 'chanId', 'gpUrl', 'docId', 'numPages', 'gtUrl', 'thWidth', 'dlUrl', 'thHeight'), $out); //合併鍵值
            //返回指定鍵值
            return empty($retArray) || !is_array($retArray) ? $allArray : array_intersect_key($allArray, array_flip($retArray));
        }
    }

    /*
     * 轉化八進位制URL
     */

    private function transFormUrl($url){
        return preg_replace('/\\\\(\d{2,3})/e', 'chr(ord("\\\$1"))', $url);
    }

    /*
     * 轉換成Png圖片
     * $url type biUrl
     * $page number
     * @retrun array pngByte
     */

    private function getUrlToPng($url, $page, $width = '1000'){
        $urlList = array();
        for($i = 1; $i <= $page; $i++){
            $urlList[] = $this->transFormUrl("https://docs.google.com/viewer?url={$url}&pagenumber={$i}&w={$width}");
        }
        $multi = new curl_multi();
        $multi->setUrlList($urlList);
        return $multi->exec();
    }

    /*
     * 先得到檔案資訊
     */

    function setUrlViewerInfo($url, $retArray = array('biUrl', 'numPages')){
        if(empty($url))
            trigger_error('$url can not be empty!', E_USER_ERROR);
        else
            $this->viewerInfo = $this->getUrlViewerInfo($url, $retArray);
    }

    /*
     * 返回的Png的Byte儲存至檔案
     * $filePrefix 檔案字首
     * $numPages 要幾頁?
     */

    function byteToPngFile($filePrefix = '', $numPages = 0){
        if(empty($this->viewerInfo))
            trigger_error('Please call setUrlViewerInfo() before runing!', E_USER_ERROR);
        else
            $biUrl = $this->viewerInfo;
        $pngByte = $this->getUrlToPng($biUrl['biUrl'], empty($numPages) ? $biUrl['numPages'] : $numPages);
        $succeed = array();
        foreach($pngByte as $key => $value){
            $succeed[] = file_put_contents($filePrefix . (sprintf("%02d", $key + 1)) . '.png', $value);
        }
        return $succeed;
    }

    /*
     * 轉換成PDF輸出
     */

    function viewerToPdfFile($filePrefix = ''){
        if(empty($this->viewerInfo))
            trigger_error('Please call setUrlViewerInfo() before runing!', E_USER_ERROR);
        else
            $gpUrl = $this->viewerInfo;
        $url = $this->transFormUrl($gpUrl['gpUrl']);
        $multi = new curl_multi();
        $multi->setOpt(array(/* 'CURLOPT_FOLLOWLOCATION' => 0,'CURLOPT_MAXREDIRS'=>3, */'CURLOPT_HEADER' => 1));
        $multi->setUrlList(array($url));
        $urlHeader = $this->transFormHeader($multi->exec()); //第一次..
        //得到cookie 還有location
        $cookie = explode(';', $urlHeader['Set-Cookie']); //Set-Cookie: 
        $location = $urlHeader['Location']; //Location: 
        //exit($cookie[0]);
        //$multi->setOpt(array('CURLOPT_COOKIE' => $cookie[0], 'CURLOPT_HEADER' => 1));
        $multi->setUrlList(array($location));
        $urlHeader = $this->transFormHeader($multi->exec()); //第二次
        $location = $urlHeader['Location']; //Location: 

        $multi->setOpt(array('CURLOPT_COOKIE' => $cookie[0], 'CURLOPT_HEADER' => 0)); //第三次..加上cookie
        $multi->setUrlList(array($location));
        $bytePdf = $multi->exec();
        if(!empty($bytePdf[0]))
            return file_put_contents($filePrefix . 'pdf.pdf', $bytePdf);
    }

    /*
     * 轉化Header為陣列格式
     */

    private function transFormHeader($str){
        $headerArray = array();
        if(is_array($str))
            $str = $str[0];
        if(!empty($str) && strpos($str, "\n") !== false)
            foreach(explode("\n", $str) as $v){
                if(strpos($v, ': ') !== false){
                    $t = explode(': ', $v);
                    if(count($t) == 2)
                        $headerArray[$t[0]] = $t[1];
                }
            }
        return $headerArray;
    }

    /*
     * 得到檔案資訊,並寫入檔案
     * (不完全功能)有待XML解析
     */

    function viewerToTextFile($filePrefix = ''){
        if(empty($this->viewerInfo))
            trigger_error('Please call setUrlViewerInfo() before runing!', E_USER_ERROR);
        else
            $gtUrl = $this->viewerInfo;
        $url = 'https://docs.google.com/viewer?url=' . $this->transFormUrl($gtUrl['gtUrl']);
        $multi = new curl_multi();
        $multi->setUrlList(array($url));
        return file_put_contents($filePrefix . 'text.txt', $multi->exec());
    }

}

curl_multi的類.請引用curl_multi_class.php檔案

以下是測試檔案index.php

<?php

require_once 'google_docs_viewer.php';

$docs = new google_docs();
$docs->setUrlViewerInfo('http://infolab.stanford.edu/pub/papers/google.pdf', null);
echo $docs->viewerToPdfFile('10123_')."\n";
echo $docs->viewerToTextFile('10123_')."\n";
print_r($docs->byteToPngFile('10123_'));