PHP windoews调用OpenOffice实现word/ppt转PDF

1、安装免费的openOffice软件

2、需要JDK支持

3、安装完openOffice后,在开始--运行中输入Dcomcnfg打开组件服务。在组件服务—计算机—我的电脑—DCOMP配置中

PHP windoews调用OpenOffice实现word/ppt转PDF

PHP windoews调用OpenOffice实现word/ppt转PDF

 

PHP windoews调用OpenOffice实现word/ppt转PDF

 

 

 PHP windoews调用OpenOffice实现word/ppt转PDF

 

PHP windoews调用OpenOffice实现word/ppt转PDF

 

 

 

4、 先到安装目录下,例如:C:\Program Files\OpenOffice 4\program\

cmd -> soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

 

5、需要在php.ini里把com.allow_dcom = true打开,即去掉前面的分号。如果是以后版本,需要在php.ini 里增加一行扩展extension=php_com_dotnet.dll,然后检查php的ext目录中是否存在该dll文件,如果没有请自行下载对应版本的dll。然后重启apache或IIS服务器。

 

class Office4Pdf
{
    private $osm;

    public function __construct()
    {
        $this->osm = new \COM("com.sun.star.ServiceManager")or die ("Please be sure that OpenOffice.org is installed.n");
    }

    public function MakePropertyValue($name,$value)
    {
        $oStruct = $this->osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
        $oStruct->Name = $name;
        $oStruct->Value = $value;
        return $oStruct;
    }

    public function transform($input_url, $output_url)
    {
        $args = array($this->MakePropertyValue("Hidden",true));

        $oDesktop = $this->osm->createInstance("com.sun.star.frame.Desktop");
//var_dump($input_url);die();
        $oWriterDoc = $oDesktop->loadComponentFromURL($input_url,"_blank", 0, $args);

        $export_args = array($this->MakePropertyValue("FilterName","writer_pdf_Export"));
//var_dump($output_url,$export_args);die();
        $oWriterDoc->storeToURL($output_url,$export_args);
        $oWriterDoc->close(true);
        return $this->getPdfPages($output_url);
    }

    public function runs($input,$output)
    {
        $input = "file:///" . str_replace("\\","/",$input);
        $output = "file:///" . str_replace("\\","/",$output);
        return $this->transform($input, $output);
    }

    /**
     * 获取PDF文件页数的函数获取
     * 文件应当对当前用户可读(linux下)
     * @param  [string] $path [文件路径]
     * @return int
     */
    public function getPdfPages($path)
    {
        if(!file_exists($path)) return 0;
        if(!is_readable($path)) return 0;
        // 打开文件
        $fp=@fopen($path,"r");
        if (!$fp)
        {
            return 0;
        }
        else
        {
            $max=0;
            while(!feof($fp))
            {
                $line = fgets($fp,255);
                if (preg_match('/\/Count [0-9]+/', $line, $matches))
                {
                    preg_match('/[0-9]+/',$matches[0], $matches2);
                    if ($max<$matches2[0]) $max=$matches2[0];
                }
            }
            fclose($fp);
            // 返回页数

            return $max;
        }
    }
}

 

调用

 static $file = 'C:/phpStudy/';
    public function actionPreview()
    {

        $html = '暂不支持预览';
        //名字
        $new_name = 'test';
        //拼装名字
        $source =  self::$file.'BOOTEX.txt';
        $pdf_url =self::$file.$new_name.".pdf";
        $last_url = self::$file.$new_name.".pdf";
        $source = iconv("UTF-8", "gbk",$source);
        $pdf_url = iconv("UTF-8", "gbk",$pdf_url);
        require dirname(dirname(__DIR__)).'/vendor/SDK/OpenOffice/Office4Pdf.php';
        $office2pdf = new \Office4Pdf();
        $result = $office2pdf->runs($source,$pdf_url);
        if($result > 0){
            $res = [
                'type' =>$new_name,
                'url' =>$last_url,
            ];
            var_dump($res);die;
        }else{
            print_r($html);die;
        }
        die;
    }

 

上一篇:OpenOffice的简单安装


下一篇:Linux 安装openoffice及中文字库