• word/excel/ppt 2 PDF


    PHP 实现 word/excel/ppt 转换为 PDF

    一般最常见的就是利用OpenOffice来转换,来看看实现的核心代码:

    
    class PDFConverter
    {
        private $com;
     
        /**
         * need to install openoffice and run in the background
         * soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
         */
        public function __construct()
        {
            try {
                $this->com = new COM('com.sun.star.ServiceManager');
            } catch (Exception $e) {
                die('Please be sure that OpenOffice.org is installed.');
            }
        }
     
        /**
         * Execute PDF file(absolute path) conversion
         * @param $source [source file]
         * @param $export [export file]
         */
        public function execute($source, $export)
        {
            $source = 'file:///' . str_replace('\', '/', $source);
            $export = 'file:///' . str_replace('\', '/', $export);
            $this->convertProcess($source, $export);
        }
     
        /**
         * Get the PDF pages
         * @param $pdf_path [absolute path]
         * @return int
         */
        public function getPages($pdf_path)
        {
            if (!file_exists($pdf_path)) return 0;
            if (!is_readable($pdf_path)) return 0;
            if ($fp = fopen($pdf_path, 'r')) {
                $page = 0;
                while (!feof($fp)) {
                    $line = fgets($fp, 255);
                    if (preg_match('//Count [0-9]+/', $line, $matches)) {
                        preg_match('/[0-9]+/', $matches[0], $matches2);
                        $page = ($page < $matches2[0]) ? $matches2[0] : $page;
                    }
                }
                fclose($fp);
                return $page;
            }
            return 0;
        }
     
        private function setProperty($name, $value)
        {
            $struct = $this->com->Bridge_GetStruct('com.sun.star.beans.PropertyValue');
            $struct->Name = $name;
            $struct->Value = $value;
            return $struct;
        }
     
        private function convertProcess($source, $export)
        {
            $desktop_args = array($this->setProperty('Hidden', true));
            $desktop = $this->com->createInstance('com.sun.star.frame.Desktop');
            $export_args = array($this->setProperty('FilterName', 'writer_pdf_Export'));
            $program = $desktop->loadComponentFromURL($source, '_blank', 0, $desktop_args);
            $program->storeToURL($export, $export_args);
            $program->close(true);
        }
    }
    

    更多详细细节可以关注公众号,并回复 word 获取word相关的资料。

    原文地址:https://segmentfault.com/a/1190000016861774

  • 相关阅读:
    第二阶段冲刺第五天(6月4号)
    第二阶段冲刺第四天(6月3号)
    第二次阶段冲刺第三天(6月2号)
    第二次阶段冲刺第二天(6月1号)
    第二次阶段冲刺第一天(5月31号)
    第十周学习进度
    第十一周学习进度
    第十二周学习进度
    javascript 将递归转化为循环
    创建数据库,并设置外部访问权限
  • 原文地址:https://www.cnblogs.com/lalalagq/p/9964075.html
Copyright © 2020-2023  润新知