php把当前网页生成pdf(php转换上传word文件为PDF的方法基于COM组件)
类别:编程学习 浏览量:1798
时间:2022-01-26 01:55:33 php把当前网页生成pdf
php转换上传word文件为PDF的方法基于COM组件本文实例讲述了php转换上传word文件为PDF的方法。分享给大家供大家参考,具体如下:
以前用过office组件转换上传文件word同时转换为html文件,这次要将word文件转换为pdf格式,网上的方法很多,也很麻烦,也不想在服务器上再安装第三方软件,花了好几天的时间,终于在一个网站上,发现在了原来用COM组件,在转换为html文件的同时,也能转换为pdf格式,而自己服务器上已经安装了office2010,这样只需要改写一下以前的几行代码就可以,代码如下:
|
$word = new COM( "Word.Application" ) or die ( "Could not initialise Object." ); // set it to 1 to see the MS Word window (the actual opening of the document) $word ->Visible = 0; // recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc" $word ->DisplayAlerts = 0; // open the word 2007-2013 document $word ->Documents->Open( 'yourdocument.docx' ); //这个是绝对文件地址,如c:\www\1.txt这样的地址才通过 // save it as word 2003 $word ->ActiveDocument->SaveAs( 'newdocument.doc' ); //转换成doc格式 // convert word 2007-2013 to PDF $word ->ActiveDocument->ExportAsFixedFormat( 'yourdocument.pdf' , 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false); //转换为pdf模式 // quit the Word process $word ->Quit(false); // clean up unset( $word ); |
以上代码的原始地址:http://stackoverflow.com/questions/5538584/convert-word-doc-docx-and-excel-xls-xlsx-to-pdf-with-php
我把以上的代码做成了一个函数,代码如下:
|
function word2pdf( $lastfnamedoc , $lastfnamepdf ) { $word = new COM( "Word.Application" ) or die ( "Could not initialise Object." ); // set it to 1 to see the MS Word window (the actual opening of the document) $word ->Visible = 0; // recommend to set to 0, disables alerts like "Do you want MS Word to be the default .. etc" $word ->DisplayAlerts = 0; // open the word 2007-2013 document // $word->Documents->Open('3.docx'); // $wordname='D:/www/fa/3.doc'; $word ->Documents->Open( $lastfnamedoc ); // save it as word 2003 // $word->ActiveDocument->SaveAs('4.doc'); // convert word 2007-2013 to PDF // $pdfname='D:/www/fa/3.pdf'; $word ->ActiveDocument->ExportAsFixedFormat( $lastfnamepdf , 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false); // quit the Word process $word ->Quit(false); // clean up unset( $word ); } |
我的是doc文件直接换pdf文件,代码是文件的地址我服务器的全是绝对地址,否则文件通不过!
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://blog.csdn.net/jaray/article/details/52690620
您可能感兴趣
- 用php实现弹出消息提示框(php弹出提示框的是实例写法)
- php技术优点和缺点(php的优点总结 php有哪些优点)
- php 上传图片如何压缩(PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例)
- phpsession登录功能(php实现session共享的实例方法)
- php新建文件夹代码(php新建文件的方法实例)
- php逐行读写文件(PHP实现读取文件夹及批量重命名文件操作示例)
- php 结果集转json(PHP的JSON封装、转变及输出操作示例)
- php实例说明(PHP中Static静态关键字功能与用法实例分析)
- php生成安全随机数(PHP随机数函数rand与mt_rand的讲解)
- php关注公众号发送消息(php实现QQ小程序发送模板消息功能)
- php查询数据库给变量赋值(详解PHP变量传值赋值和引用赋值变量销毁)
- 自己在做项目过程中的php知识(PHP+Oracle本地开发环境搭建方法详解)
- php mq使用方法(PHP使用ActiveMQ实现消息队列的方法详解)
- web项目部署到阿里云服务器上(阿里云的WindowsServer2016上部署php+apache)
- php 策略模式工厂模式区别(PHP设计模式之策略模式原理与用法实例分析)
- dedecms注册怎么给权限(织梦DedeCms系统未审核文档禁止动态浏览修改方法view.php)
- 菲律宾潜水(菲律宾潜水价格)
- 泰国人妖(变性手术生殖器要割掉吗)
- 泰国美女(泰国人妖和女性如何区分)
- 泰国旅游业怎么样(泰国的旅游产业)
- 越南新娘(越南新娘婚介网站)
- 越南新娘(越南新娘骗婚套路流程)
热门推荐
- 基于python的加密算法(python实现维吉尼亚加密法)
- django响应返回的常用方法(Django异步任务之Celery的基本使用)
- tomcat在linux系统安装步骤(Tomcat的卸载和重装的实现图文)
- Web开发中的几个建议
- python微信消息模拟请求(python实现微信机器人: 登录微信、消息接收、自动回复功能)
- sql server事务回滚(SQL Server 添加Delete操作回滚日志方式)
- ftp服务器怎么搭建linux(Linux下使用vsftp搭建FTP服务器附参数说明)
- 单元格内容过多超出怎么办(单元格内文本显示超过单元格宽度的快速解决办法)
- php中钩子的理解与实例教程(php中钩子hook的原理与简单应用demo示例)
- sw动态注解视图干嘛的(Swoole 5将移除自动添加Event::wait特性详解)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9