简体   繁体   English

使用docx在phpoffice中捕获异常

[英]catch exceptions in phpoffice with docx

How to catch this exception (or any exception in phpoffice) 如何捕获此异常(或phpoffice中的任何异常)

https://github.com/PHPOffice/PHPWord/tree/master/src/PhpWord https://github.com/PHPOffice/PHPWord/tree/master/src/PhpWord

require_once \Ini::get('path/class').'/third-party/phpword/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();

\PhpOffice\PhpWord\Settings::setPdfRendererPath(\Ini::get('path/class').'/third-party/tcpdf');
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');

try{
    $word = \PhpOffice\PhpWord\IOFactory::load($file_dst);
    $writer = \PhpOffice\PhpWord\IOFactory::createWriter($word, 'PDF');
}
catch(\PhpOffice\PhpWord\BadMethodCallException $e){

}

error 错误

    PHP Fatal error:  Uncaught exception 'BadMethodCallException' with message 'Cannot add PreserveText in Cell.' in /var/www/dyntest.dk/class/third-party/phpword/Element/AbstractContainer.php:276

Thats a base PHP Exception (part of the PHP core, not PHPOffice), you should catch it like this: 那就是基本的PHP异常 (PHP核心的一部分,而不是PHPOffice的一部分),您应该这样捕获它:

try{
    $word = \PhpOffice\PhpWord\IOFactory::load($file_dst);
    $writer = \PhpOffice\PhpWord\IOFactory::createWriter($word, 'PDF');
}
catch(\BadMethodCallException $e){

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM