php 利用openoffice把word转成html和pdf的方法(导出html的图片也可导出来)

2017-02-06 11:10
1。电脑上首先要安装openoffice
2。打开com组件的支持,方法为该篇日志类别下的《PHP 开启COM组件》
3。转为pdf的代码为:
<?php set_time_limit(0); function MakePropertyValue($name,$value,$osm){ $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue"); $oStruct->Name = $name; $oStruct->Value = $value; return $oStruct; } function word2pdf($doc_url, $output_url){ $osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.n"); $args = array(MakePropertyValue("Hidden",true,$osm)); $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop"); $oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args); $export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm)); $oWriterDoc->storeToURL($output_url,$export_args); $oWriterDoc->close(true); } $output_dir = "文件导出的目录,如F:/";
$doc_file = "doc文件的位置,如F:/test.doc";
$pdf_file = "导出pdf文件的名字,如test.pdf"; $output_file = $output_dir . $pdf_file; $doc_file = "file:///" . $doc_file; $output_file = "file:///" . $output_file; word2pdf($doc_file,$output_file); ?>
4.转为html的代码为:
<?php    
function OpenOfficeMakePropertyValue( $name, $value, $osm) 
    $oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue"); 
    $oStruct->Name = $name; 
    $oStruct->Value = $value; 
    return $oStruct; 

function OpenOfficeWordToHtml( $doc_url, $output_url ) 
    //Invoke the OpenOffice.org service manager 
    $osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n"); 
    //Set the application to remain hidden to avoid flashing the document onscreen 
    $args = array(OpenOfficeMakePropertyValue("Hidden",true,$osm)); 
    //Launch the desktop 
    $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop"); 

    //Load the .doc file, and pass in the "Hidden" property from above 
    $oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args); 
    //Set up the arguments for the PDF output 
    $export_args = array( 
                  OpenOfficeMakePropertyValue("FilterName","HTML (StarWriter)",$osm) , 
                  OpenOfficeMakePropertyValue("Overwrite","true",$osm) 
         ); 
    //Write out the HTML 
    $oWriterDoc->storeToURL($output_url,$export_args); 
    $oWriterDoc->close(true); 
}

$output_dir = "文件导出的目录,如F:/";  
$doc_file = "doc文件的位置,如F:/test.doc";  
$pdf_file = "导出html文件的名字,如test.html";  
$output_file = $output_dir . $pdf_file;  

$doc_file = "file:///" . $doc_file;  
$output_file = "file:///" . $output_file; 
OpenOfficeWordToHtml($doc_file,$output_file); 
?>  
^