admin管理员组

文章数量:1605159

PHP 安装COM组件

注意:php版本>5.3.15,需要保证ext文件夹下有php_com_dotnet.dll 并在php.ini中加入

extension=php_com_dotnet.dll

php.ini中搜索并去除com.allow_dcom = true前面的‘;’

com.allow_dcom = true

注意:此组件需要服务器内安装Office组件

office2016官方下载免费完整版_office2016正式版-系统之家-系统下载 

/**
 * @package word文档转pdf
 * @param  string    $wordPath    word原文件路径
 * @param  string    $outPath     pdf输出路径
 * @return string
 */
function word_turn_pdf($wordPath, $outPath)
{
    // 原文件不存在则返回错误
    if(!file_exists($wordPath)) 
    {
      return 'word原文件不存在';
    }
    
    // 输出目录不存在则创建目录
    if(!file_exists($tmpPath = rtrim($outPath, basename($outPath))))
    {
        mkdir($tmpPath, 0777, true);
    }
    
    $filenamedoc = $wordPath;
    $filenamepdf = $outPath;
    
    // 删除已有同名文件
    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 $outPath;
}

本文标签: 文件PHPwordPDF