function word_turn_pdf($wordPath, $pdfPath)
{
$filenamedoc = ROOT_PATH . 'public' . str_replace('/', DS, $wordPath);
$filenamepdf = ROOT_PATH . 'public' . str_replace('/', DS, $pdfPath);
// 原文件不存在则返回错误
if (!file_exists($filenamedoc)) {
return 'word原文件不存在';
}
// 输出目录不存在则创建目录
if (!file_exists($tmpPath = rtrim($filenamepdf, basename($filenamepdf)))) {
mkdir($tmpPath, 0777, true);
}
// 删除已有同名文件
if (file_exists($filenamepdf)) {
unlink($filenamepdf);
}
// 执行转换操作
$word = new COM("word.Application") or die("Could not initialise Object");
$word->Documents->Open($filenamedoc);
$word->ActiveDocument->ExportAsFixedFormat($filenamepdf, 17, false, 0, 0, 0, 0, 7, true, true, 2, true, true, false);
$word->Quit(false);
unset($word);
// 在页面中显示生成的pdf
// header('Content-type: application/pdf');
// header('filename='.$filenamepdf);
// readfile($filenamepdf);
return $pdfPath;
}
实现该功能需要开启com_dotnet扩展,我用的是小皮,直接在网站-》管理-》php扩展中选中com_dotnet扩展即可