php框架symfony5.4 phpoffice/phpword 包版本 有一个项目需要做文章批量导出为word功能,调研后决定采用phpword,使用过程中碰到以下问题: 1、如果是下面这种全局样式,p标签下的内容的就不会生效 !DOCTYPE htmlhtmlheadmeta http-equiv="Content-Type" content="text/ht
php框架symfony5.4
phpoffice/phpword 包版本
有一个项目需要做文章批量导出为word功能,调研后决定采用phpword,使用过程中碰到以下问题:
1、如果是下面这种全局样式,p标签下的内容的就不会生效
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-" />
</head>
<body>
<style>
p {font-size:18px;text-indent:36px;line-height:2.2;margin-bottom:30px;}
</style>
<p>test</p>   
<img src="/image/20210226-1614324791820472.jpg" alt="1.jpg" style="width: 550; text-indent: 0; margin-bottom: 0px; margin-left: -1em;">
</body></html>
想到的解决方法是使用 ‘tijsverkoyen/css-to-inline-styles‘ 这个包来使全局样式转换为行内样式,然后再进行导出。
2、图片width解析不了
在导出word后发现图片宽度普遍比较宽,需要设置图片的宽度为500px,然而用以下方式修改并不会生效
<img src="/image/20210226-1614324791820472.jpg" alt="1.jpg" style="width: 550; text-indent: 0; margin-bottom: 0px; margin-left: -1em;">
查看phpword的包发现\vendor\phpoffice\phpword\src\PhpWord\Shared\Html.php该文件下的 788行  parseImage 方法style样式下只解析了float,没解析width,所有2个解决方案:用第一种解决方案,2829行新增以下代码:
case 'style':
                    $styleattr = explode(';', $attribute->value);
                    foreach ($styleattr as $attr) {
                        if (strpos($attr, ':')) {
                            list($k, $v) = explode(':', $attr);
                            switch ($k) {
                                case 'float':
                                    if (trim($v) == 'right') {
                                        $style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_RIGHT;
                                        $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
                                        $style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
                                        $style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
                                        $style['overlap'] = true;
                                    }
                                    if (trim($v) == 'left') {
                                        $style['hPos'] = \PhpOffice\PhpWord\Style\Image::POS_LEFT;
                                        $style['hPosRelTo'] = \PhpOffice\PhpWord\Style\Image::POS_RELTO_MARGIN; // inner section area
                                        $style['pos'] = \PhpOffice\PhpWord\Style\Image::POS_RELATIVE;
                                        $style['wrap'] = \PhpOffice\PhpWord\Style\Image::WRAP_TIGHT;
                                        $style['overlap'] = true;
                                    }
                                    break;
                                case 'width':
                                    $style['width'] = $v;
                                    $style['unit'] = \PhpOffice\PhpWord\Style\Image::UNIT_PX;
                                    break;
                            }
                        }
                    }
                    break;
 
 
				 沃梦达教程
				
			本文标题为:phpword使用html保存word,图片width和全局样式无法导出问题
				
        
 
            
        
             猜你喜欢
        
	     - PHP中PDO事务处理操作示例 2022-10-15
 - Laravel balde模板文件中判断数据为空方法 2023-08-30
 - windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境) 2023-09-02
 - PHP实现微信支付(jsapi支付)流程步骤详解 2022-10-09
 - php微信公众号开发之秒杀 2022-11-23
 - 用nohup命令实现PHP的多进程 2023-09-02
 - laravel通用化的CURD的实现 2023-03-17
 - PHP简单实现二维数组的矩阵转置操作示例 2022-10-02
 - laravel实现按月或天或小时统计mysql数据的方法 2023-02-22
 - PHP仿tp实现mvc框架基本设计思路与实现方法分析 2022-10-18
 
						
						
						
						
						
				
				
				
				