php 利用openoffice把word轉成html和pdf的方法(導出html的圖片也可導出來)
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 )
{
//調用OpenOffice.org服務器
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
//設置應用程序繼續隱藏,避免閃爍的屏幕上的文檔
$args = array(OpenOfficeMakePropertyValue("Hidden",true,$osm));
//啟動桌面
$oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
//加載。doc文件,從上面傳遞“隱藏”屬性
$oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);
//設置參數為PDF輸出
$export_args = array(
OpenOfficeMakePropertyValue("FilterName","HTML (StarWriter)",$osm) ,
OpenOfficeMakePropertyValue("Overwrite","true",$osm)
);
//寫出的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);
?>