php wordファイルWordファイルをアップロードPDFに転送

2315 ワード

以前officeコンポーネントを使ってアップロードファイルwordを同時にhtmlファイルに変換したことがありますが、今回wordファイルをpdf形式に変換するには、ネット上の方法が多く、面倒で、サーバーにサードパーティのソフトウェアをインストールしたくないので、何日もかかりました.やっとあるサイトで、COMコンポーネントを使ってhtmlファイルに変換すると同時に、pdf形式に変換することもでき、自分のサーバにoffice 2010がインストールされているので、以前の数行のコードを書き換えるだけでいいです.コードは以下の通りです.
  $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ファイルを交換するので、コードはファイルのアドレスです私のサーバーのはすべて絶対的なアドレスで、さもなくばファイルは通じません!