如何使用PHPWord将Doc文件转换为docx或pdf格式?

时间:2017-02-17 04:12:29

标签: doc phpword

我正在尝试使用PHPWord将Doc(简历)文件转换为docx或pdf格式。我从github下载了PHPWord。

我尝试过以下代码

require_once '../PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Excel2003.doc');

// Save File

$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');

$objWriter->save('Excel2007.docx');

但是收到错误

Warning: include(/var/www/html/PHPWord-develop/src/PhpWord/PHPWord.php): failed to open stream: No such file or directory in /var/www/html/PHPWord-develop/readdoc.php on line 8

Warning: include(): Failed opening '/var/www/html/PHPWord-develop/src/PhpWord/PHPWord.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/PHPWord-develop/readdoc.php on line 8

Fatal error: Class 'PhpOffice\PHPWord' not found in /var/www/html/PHPWord-develop/readdoc.php on line 9

解决方案是什么?

提前致谢

1 个答案:

答案 0 :(得分:0)

在需要包之后,您确定当前文件可以读取PhpOffice / PhpWord包,然后尝试用以下代码替换您的代码:

$PHPWord = new \PHPWord();

$document = $PHPWord->loadTemplate('Excel2003.doc');

// Save File

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($PHPWord, 'Word2007');

$objWriter->save('Excel2007.docx');

然后还要注意,如果您尝试转换的文档是Word2007,那么您可以将第二个参数留空。