使用 PHP 将 Doc/Docx 文件转换为 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14018846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Doc/Docx Files To PDF using PHP
提问by Vineesh Kalarickal
I am looking for a PHP code or PHP Library to convert microsoft doc/docx files to PDF. I am not familiar with Zend. Please help me to convert my word files to PDF successfully.
我正在寻找将 microsoft doc/docx 文件转换为 PDF 的 PHP 代码或 PHP 库。我对 Zend 不熟悉。请帮助我成功地将我的 word 文件转换为 PDF。
采纳答案by Vineesh Kalarickal
Open Office and This Code Helps me :)
打开 Office,此代码对我有帮助 :)
<?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);
?>
回答by christian
phpword does not converts to PDF, it only helps on creating word document and using variables replacement on it.
phpword 不会转换为 PDF,它只会帮助创建 Word 文档并在其上使用变量替换。
回答by shasi kanth
If you are on Windows, COM packageis the best option.
如果你在 Windows 上,COM 包是最好的选择。

