zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Oracle 的字符集与乱码
    linux 时间同步的2种方法
    2 创建型模式-----工厂方法模式
    条款4:确定对象在使用前已被初始化
    条款3:尽可能地使用const
    条款2:尽量以const、enum、inline替换#define
    条款13:以对象管理资源
    条款12:牢记复制对象的所有成员
    条款11:在operator=中处理“自我赋值”
    条款10:令operator=返回一个*this的引用
  • 原文地址:https://www.cnblogs.com/lalalagq/p/9964075.html
Copyright © 2011-2022 走看看