php 上传word文件 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文件,代码是文件的地址我服务器的全是绝对地址,否则文件通不过!



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值