服务热线 400-660-5555

苏州网站建设
首页 站内资讯

苏州网站建设

站内资讯
苏州网站建设 / 站内资讯 / 产品资讯 / 正文

PHP网站开发之将文档转换成pdf文件

来源: All文章
发布时间:2023-04-10 16:08:21

  本文介绍的是在Windows系统环境下进行的PHP将text、excl、word等等文档格式转换成pdf文件格式

  第一步:检查是当时的php是否开启对dcom扩展

  打开php.ini,搜索php_com_dotnet和php_com_dotnet:

  extension=php_com_dotnet.dll//把前面的分号去掉

  com.allow_dcom=true//改为true

  然后输出下phpinfo()

  看看有没有com_dotnet

  COMsupportenabled

  DCOMsupportenabled

  .Netsupportenabled

  表示COM组件开启成功

  所需要的工具openoffice安装openoffice软件

  在openoffice安装成功之后,需要在安装目录下porgram文件下打开cmd命令行输入

  cd/dd:\openoffice\program

  具体看你所安装的具体文件位置

  soffice-headless-accept=socket,host=12

  7.0.0.1,port=8100;urp;-nofirststartwizard

  配置openoffice服务启动和激活权限具体可以百度这里不做介绍

  下面就是代码介绍

  ?ph

  classOpenOffice{

  private$osm;

  //构造函数,启用OpenOffice的COM组件

  publicfunction__construct(){

  ini_set('magic_quotes_runtime',0);//设置运行时间

  $this-osm=newCOM(com.sun.star.ServiceManager)ordie(PleasebesurethatOpenOffice.orgisinstalled.n);

  }

  privatefunctionMakePropertyValue($name,$value){

  $oStruct=$this-osm-Bridge_GetStruct(com.sun.star.beans.PropertyValue);

  $oStruct-Name=$name;

  $oStruct-Value=$value;

  return$oStruct;

  }

  privatefunctiontransform($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);

  }

  /**

  *getPdfPages获取PDF文件页数的函数获取,文件应当对当前用户可读(linux下)

  *@paramstring$path文件路径

  *@returnint

  */

  privatefunctiongetPdfPages($path=''){

  if(!file_exists($path))return0;

  if(!is_readable($path))return0;

  $fp=@fopen($path,r);//打开文件

  if(!$fp){

  return0;

  }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;//返回页数

  }

  }

  /**

  *office文件转换pdf格式

  *@paramstring$input需要转换的文件

  *@paramstring$output转换后的pdf文件

  *@returnreturnstring页数

  */

  publicfunctionrun($input='',$output=''){

  if(empty($input)||empty($output)){

  return'参数错误';

  }

  $input=file:///.str_replace(\\,/,$input);

  $output=file:///.str_replace(\\,/,$output);

  //dump($input);

  //dump($output);exit;

  //测试文档$input='file:///C:/wamp/www/br/Uploads/Temp/files/20231023/5bcf3e022d9ff.txt';

  //测试文档$output='file:///C:/wamp/www/br/Uploads/Temp/files/20231023/5bcf3e022d9ff.pdf';

  return$this-transform($input,$output);

  }

  }

  调用

  functiontopdf(){

  import('org.Util.OpenOffice');

  $file_url=$res['file_url'];//原文件

  $file_dir='E:/wamp/www'.;//下载文件存放目录

  $file_info=pathinfo($file_url);

  $doc_file=$file_dir.$file_url;

  $pdf_file=$file_dir.$file_info['dirname'].'/'.$file_info['filename'].'.pdf';//转换后的pdf名

  //dump($pdf_file);

  //dump($file_info);exit;

  $open=new\OpenOffice();

  //$open-run($doc_file,$pdf_file);

  $res=$open-run($doc_file,$pdf_file);

  dump($res);

  }

  基本上完成,开发中遇到的问题主要有

  Openoffice服务没有开启;

  文件没有找到路径问题,要绝对路径

  COM组件没有开启。注意开启在window系统下Openoffice开启后会很占内存,服务器配置要高一点不然就卡死

* 文章来源于网络,如有侵权,请联系客服删除处理。
在线 咨询

添加动力小姐姐微信

微信 咨询

电话咨询

400-660-5555

我们联系您

电话 咨询
微信扫码关注动力小姐姐 X
qr