php xlsx ,xls,doc,docx 转换pdf 代码示例

1.xlsx ,xls,doc,docx 转换pdf 必须安装openoffice 安装(如果不安装这个软件下面代码不能使用)

// xlsx ,xls,doc,docx  转换pdf  必须安装openoffice 安装

function  wordpdf($sfile,$pdfname){
    set_time_limit(0);

    //////
    $wei_houzhui=substr(strrchr($_SERVER['SCRIPT_FILENAME'], '/'), 1);
    $wei_file=mb_strlen($wei_houzhui,'utf-8')+1;//获取后缀名的长度
    $zong_file=mb_strlen($_SERVER['SCRIPT_FILENAME'],'utf-8');//获取总的长度
    $filenams_file=mb_substr($_SERVER['SCRIPT_FILENAME'],0,$zong_file-$wei_file,'utf-8');
    /////
    $output_dir =  $filenams_file.'/Public/Uploads/pdf/';
    $doc_file =$filenams_file."/Public/Uploads/".$sfile;
    $pdf_file =$pdfname.".pdf";
    $output_file = $output_dir.$pdf_file;
    $doc_file =iconv('utf-8','gb2312',"file:///".$doc_file);
    $output_file = iconv('utf-8','gb2312',"file:///".$output_file);
    word2pdf($doc_file,$output_file);
}

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 ("请确认OpenOffice.org库是否已经安装.\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);
}

猜你喜欢

转载自blog.csdn.net/qq_38366657/article/details/81394496