2012-12-24 468 views
2

我正在尋找一個PHP代碼或PHP庫來將Microsoft Doc/docx文件轉換爲PDF。我對Zend不熟悉。請幫助我成功將我的Word文件轉換爲PDF。使用PHP將Doc/Docx文件轉換爲PDF

+0

取消了一個在線convrtoe oror? –

+0

我不需要在線轉換器 –

+1

請輸入關閉投票的原因。這將幫助我避免錯誤。謝謝你.. –

回答

1

開放式辦公室和該代碼可以幫助我:)

<?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){ 
    //echo $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(MakePropertyValue("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(MakePropertyValue("FilterName","writer_pdf_Export",$osm)); 
    //print_r($export_args); 
    //Write out the PDF 
    $oWriterDoc->storeToURL($output_url,$export_args); 
    $oWriterDoc->close(true); 
    } 
    $output_dir = "C:/wamp/www/vineesh/"; 
    $doc_file = "C:/wamp/www/vineesh/vineesh.docx"; 
    $pdf_file = "vineesh.pdf"; 
    $output_file = $output_dir . $pdf_file; 
    $doc_file = "file:///" . $doc_file; 
    $output_file = "file:///" . $output_file; 
    word2pdf($doc_file,$output_file); 
    ?> 
+0

但我有錯誤此代碼「致命錯誤:未捕獲的異常'com_exception'與消息'無法創建COM對象com.sun.star.ServiceManager':語法無效'in C:\ xampp \ htdocs \ learn \ word.php:11堆棧跟蹤:#0 C:\ xampp \ htdocs \ learn \ word.php(11):com-> com('com.sun.star.Se .. ')#1 C:\ xampp \ htdocs \ learn \ word.php(30):word2pdf('file:/// C:/ xamp ...','file:/// C:/ xamp .. 。')#2 {main}拋出第11行的C:\ xampp \ htdocs \ learn \ word.php –

+0

請確定已安裝OpenOffice.org,在您的電腦或服務器上安裝open office –

+0

我該如何安裝OpenOffice在我的xampp服務器 –

0

你的意思是PHP庫嗎?然後phpdocx將爲你工作。閱讀安裝文檔並生成pdf。

0

phpword不轉換爲PDF,它不僅有利於創建Word文檔,並使用它的變量替換。

0

如果您在Windows上,COM package是最佳選擇。