PHP code example of alanalbert / office2pdf

1. Go to this page and download the library: Download alanalbert/office2pdf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alanalbert / office2pdf example snippets




$source_path_utf8 = "E:/测试.doc";                           // Word文件路径
$source_path = iconv('UTF-8', 'GBK', $source_path_utf8);	// 转为GBK,防止中文乱码而找不到文件
$output_path = $source_path . '.pdf';                       // PDF目标文件路径

$office = new Office\OfficeCOM();
$office->run('word');
var_dump($office->word2Pdf($source_path, $output_path));
$office->close('word');

/*
 * @method boolean      run($type = 'all')                      运行应用, 参数: all|word|excel|ppt
 * @method boolean      close($type = 'all')                    关闭应用, 参数: all|word|excel|ppt
 * @method int|false    getPageNumFromDoc($file)                获取Word文档页数
 * @method int|false    getPageNumFromPpt($file)                获取PPT文档页数
 * @method int|false    getPageNumFromExcel($file)              获取Excel文档页数
 * @method int|false    getPageNumFromPdf($file)                获取PDF文件页数
 * @method boolean      word2Pdf($source_file, $output_file)    Word转PDF
 * @method boolean      excel2Pdf($source_file, $output_file)   Excel转PDF
 * @method boolean      ppt2Pdf($source_file, $output_file)     PPT转PDF
 */