PHP將上傳word文件,轉化為Html格式,(多種轉換方式)


方法一: 通過PHPOffice(推薦)

  1:    composer require phpoffice/phpword    
  ```
                                        /*  通過composer安裝 PHPOffice 
                                            需要
                                           或者用中國鏡像:https://www.phpcomposer.com/
                                        */   
  ```
  2:  安裝成功可看到 vendor文件夾


  3: 使用方法 :
  ```
  <?php 
      require 'vendor/autoload.php';
      $phpWord = \PhpOffice\PhpWord\IOFactory::load('./4.docx');
      $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
      $xmlWriter ->save('./ceshi.htm');
  ``` 


    
     4:官方  
     /*
       官方案例:https://phpword.readthedocs.io/en/latest/general.html
       github: https://github.com/PHPOffice/PhpSpreadsheet
      */

方法二:通過python

        ```
        from win32com import client as wc
        import sys

        def saveHtm():
      # print(wordPath)
      # print(htmPath)
      wordPath='E:/1.doc'
      htmPath='E:/1.htm'
      word = wc.Dispatch('Word.Application')
      print(word) 
      doc = word.Documents.Open('E:/1.doc')
      doc.SaveAs("E:/1.htm", 8)               //轉化為htm格式 
      doc.SvaeAs("E:/1.fpt",17)                

      doc.Close() 
      word.Quit()

        if __name__ == '__main__':
      saveHtm()
        ```

方法三:同時Offic API直接在網頁顯示word文檔

  ```
  <iframe
 src="http://view.officeapps.live.com/op/view.aspx?src=公網上能訪問的word文檔地址" ></iframe>

  例
  src="http://view.officeapps.live.com/op/view.aspxsrc=newteach.pbworks.com%2Ff%2Fele%2Bnewsletter.docx"

方法四:通過com組件

需要所在宿主機,有offic的環境,Linxu下不能使用
 
(不推薦)
<?php
/**
 * @desc   xmsb_wordToPdf         將word轉換為pdf
 * @param  string    $wordPath    word原文件路徑
 * @param  string    $outPath     pdf輸出路徑
 * @return string
 */
function xmsb_wordToPdf($wordPath, $outPath)
{
    // 原文件不存在則返回錯誤
    if(!file_exists($wordPath)) 
    {
        return 'word原文件不存在';
    }
    
    // 輸出目錄不存在則創建目錄
    if(!file_exists($tmpPath = rtrim($outPath, basename($outPath))))
    {
        mkdir($tmpPath, 0777, true);
    }
    
    $filenamedoc = dirname(__FILE__)."/".$wordPath;
    $filenamepdf = dirname(__FILE__)."/".$outPath;
    
    // 刪除已有同名文件
    if(file_exists($filenamepdf)) 
    {
        unlink($filenamepdf);
    }
    
    // 執行轉換操作
    $word = new COM("word.Application") or die("Could not initialise Object");
    $word->Documents->Open($filenamedoc);
    $word->ActiveDocument->ExportAsFixedFormat($filenamepdf, 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
    $word->Quit(false);
    unset($word);
    
    // 在頁面中顯示生成的pdf
    // header('Content-type: application/pdf');
    // header('filename='.$filenamepdf);
    // readfile($filenamepdf);
    
    return '轉換成功';
}

// 測試運行
// echo xmsb_wordToPdf('./word/word1.docx', './pdf/output1.pdf');

function word2html($wordname,$htmlname)  
{  
    $word = new COM("word.application") or die("Unable to instanciate Word");  
    $word->Visible = 1;  
    $word->Documents->Open($wordname);  
    $word->Documents[1]->SaveAs($htmlname,8);  
    $word->Quit();  
    $word = null;  
    unset($word);  
    return '轉換成功';
}  
 
// word2html('./word/cs.docx', './pdf/cs.html'); 

require 'vendor/autoload.php';

function changehtml($word,$htmlname,$url){
    $phpWord = \PhpOffice\PhpWord\IOFactory::load($word);

    //增加圖片
    $image = __DIR__.$url;
    
    $section = $phpWord->addSection();
    $section->addImage($image,array("width"=>100,"height"=>100,"position"=>"absolute","bottom"=>"0px"));

    $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
    $xmlWriter ->save($htmlname);
}

changehtml('./word/wo.docx', './pdf/wo.html','./word/gz.png'); 
?>

轉載:https://blog.csdn.net/weixin_42971363/article/details/101156195


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM