PHP 实现Word,excel等转换pdf
最近做一个项目,需要将用户上传的word,excel文档转成PDF文档保存并打印,在网上找了很多资料,并不全面,所以自己写了一份比较全面的教程来分享。
下面是操作步骤:
1、 安装免费的openOffice软件,请至openoffice.org下载最新版本。
2、 JDK支持,请自行搜索下载最新版本JDK。
3、 安装完openOffice后,在开始--运行中输入Dcomcnfg打开组件服务。在组件服务?计算机?我的电脑?DCOMP配置中,选择
在这两项上分别点击右键属性,打开属性面板如下图:
选择安全选项卡,分别在启动和激活权限和访问权限两项上点自定义,添加Everyone的权限。
选择标识选项卡,选择交互式用户。
4、 安装完openOffice后,请先打开一次确认可以正常运行软件,然后退出后用命令行运行以下命令。
先到安装目录下,例如:C:\Program Files\OpenOffice 4\program\
执行命令:
soffice -headless-accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
成功后即在后台运行了该软件。
5、 如果是php5.4.5以前版本,需要在php.ini里把com.allow_dcom = true打开,即去掉前面的分号。如果是以后版本,需要在php.ini 里增加一行扩展extension=php_com_dotnet.dll,然后检查php的ext目录中是否存在该dll文件,如果没有请自行下载对应版本的dll。然后重启apache或IIS服务器。
6、 代码实现
/** * office文档转换为PDF类 * @author jinzhonghao created 2015-04-23 */class office2pdf{ 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"); $oWriterDoc = $oDesktop->loadComponentFromURL($input_url,"_blank", 0, $args); $export_args = array($this->MakePropertyValue("FilterName","writer_pdf_Export")); $oWriterDoc->storeToURL($output_url,$export_args); $oWriterDoc->close(true); return $this->getPdfPages($output_url); } public function run($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 <br> <br> <br> <p></p>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
