PHPWord 打印word文档[可带图片,相对定位等]
来源:互联网 发布:燕郊淘宝店招聘 编辑:程序博客网 时间:2024/06/05 02:29
下载:
http://phpword.codeplex.com/releases/view/49543
解压缩,添加以下文件至项目中:
PHPWord // 文件夹PHPWord.class.php // 文件
然后在项目中引用PHPWord.class.php
;
$PHPWord = new PHPWord();$section = $PHPWord->createSection();
设置样式
$PHPWord->addFontStyle('content', array('bold'=>false, 'size'=>11));$PHPWord->addFontStyle('rstyle', array('bold'=>true, 'italic'=>false, 'size'=>18,'align'=>'center'));$PHPWord->addFontStyle('tstyle', array('bold'=>true, 'italic'=>false, 'size'=>11));$PHPWord->addParagraphStyle('pstyle', array('align'=>'center', 'spaceAfter'=>100));$PHPWord->addFontStyle('end_style', array('italic'=>false, 'size'=>11,'align'=>'right'));$PHPWord->addParagraphStyle('endstyle', array('align'=>'right', 'spaceAfter'=>100));
设置文字内容
$title = iconv('utf-8','GB2312//IGNORE','借款协议');$section->addText($title,'rstyle','pstyle');$section->addTextBreak(2); # 分段 2 设置段落距离$section->addText(iconv('utf-8','GB2312//IGNORE',$str),'tstyle'); #一段内容
设置表格
# 设置表格样式$cellStyle = array('valign'=>'center','align'=>'center'); $styleTable = array('borderSize'=>6, 'borderColor'=>'000000','cellMargin'=>80,'alignMent'=>'center');$PHPWord->addTableStyle('myTable', $styleTable);# 创建一个表格$table = $section->addTable('myTable'); $table->addRow(300); // 行$table->addCell(2300,$cellStyle)->addText(iconv('utf-8','GB2312//IGNORE','用户名')); //列$table->addCell(2300,$cellStyle)->addText(iconv('utf-8','GB2312//IGNORE','金额')); //列$table->addCell(2300,$cellStyle)->addText(iconv('utf-8','GB2312//IGNORE','期限')); //列foreach ($user as $v) { $table->addRow(300); if($v['user_id'] == is_login()){ $name = $v['name']; }else{ $name = msubstr($v['name'],0,2); } $table->addCell(2300,$cellStyle)->addText(iconv('utf-8','GB2312//IGNORE',$name)); $table->addCell(2300,$cellStyle)->addText($v['money'])); $btime = $deal['borrow_time']; if($deal['time_type'] == 1){ $btime = $btime.'个月'; }else{ $btime = $btime.'天'; } $table->addCell(2300,$cellStyle)->addText(iconv('utf-8','GB2312//IGNORE',$btime));}
设置图片
# 设置图片样式[定位]$src = trim($_SERVER['DOCUMENT_ROOT'].'/public/img/zhang.png');$tmp_textrun = $section->createTextRun(array('indentLeft' => 2600)); $imageStyle = array('width'=>110, 'height'=>110, 'position' => 'absolute', 'top' => -108, 'left' => 480, 'zIndex' => 4);$tmp_textrun->addImage($src, $imageStyle);
保存为文件
$path = './Upload/'.date('Ym').'/'.date('d').'/'; if (!is_dir($path)){ mkdir($path,0777,true); } $fileName = strtolower($code).'.doc'; $filePath = $path.$fileName; $objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); $objWriter->save($filePath); echo $filePath;
解决中文不乱码
# 修改PHPWord/Template.php/*** Set a Template value* * @param mixed $search* @param mixed $replace*/public function setValue($search, $replace) { if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { $search = '${'.$search.'}'; } if(!is_array($replace)) { //$replace = utf8_encode($replace); $replace =iconv('gbk', 'utf-8', $replace); // 注释掉上面行后添加这行 } $this->_documentXML = str_replace($search, $replace, $this->_documentXML);}# 调用方式$section->addText(iconv('utf-8','GB2312//IGNORE','你好'),'tstyle');
解决图片定位问题
// 路径 PHPWord\Style\Image.php// 添加private $_indentLeft = null; private $_indentRight = null;private $_position = null;private $_top = null;private $_left = null;private $_zIndex = null;public function getIndentLeft() { return $this->_indentLeft; } public function setIndentLeft($pValue = null) { $this->_indentLeft = $pValue; return $this; } public function getIndentRight() { return $this->_indentRight; } public function setIndentRight($pValue = null) { $this->_indentRight = $pValue; return $this; } public function getPosition() { return $this->_position; } public function setPosition($pValue = null) { $this->_position = $pValue; return $this; } public function getTop() { return $this->_top; } public function getLeft() { return $this->_left; } public function setTop($pValue = null) { $this->_top = $pValue; return $this; } public function setLeft($pValue = null) { $this->_left = $pValue; return $this; } public function getZIndex() { return $this->_zIndex; } public function setZIndex($pValue = null) { $this->_zIndex = $pValue; return $this; } // 修改 PHPWord\Writer\Word2007\Base.php 我将原方法注释了,放上了这个protected function _writeImage(PHPWord_Shared_XMLWriter $objWriter = null, $image, $is_textrun = false) { $rId = $image->getRelationId(); $style = $image->getStyle(); $width = $style->getWidth(); $height = $style->getHeight(); $align = $style->getAlign(); $indentLeft = $style->getIndentLeft(); $indentRight = $style->getIndentRight(); $position = $style->getPosition(); $top = $style->getTop(); $left = $style->getLeft(); $zIndex = $style->getZIndex(); if (!$is_textrun) { $objWriter->startElement('w:p'); } if(!is_null($align) || !is_null($indentLeft) || !is_null($indentRight)) { $objWriter->startElement('w:pPr'); if (!is_null($align)) { $objWriter->startElement('w:jc'); $objWriter->writeAttribute('w:val', $align); $objWriter->endElement(); // w:jc } if (!is_null($indentLeft) || !is_null($indentRight)) { $objWriter->startElement('w:ind'); if (!is_null($indentLeft)) { $objWriter->writeAttribute('w:left', $indentLeft); } if (!is_null($indentRight)){ $objWriter->writeAttribute('w:right', $indentRight); } $objWriter->endElement(); // w:ind } $objWriter->endElement(); // w:pPr } $objWriter->startElement('w:r'); $objWriter->startElement('w:pict'); $objWriter->startElement('v:shape'); $objWriter->writeAttribute('type', '#_x0000_t75'); if (!empty($position) && $top !== null && $left !== null && $zIndex !== null){ $objWriter->writeAttribute('style','width:'.$width.'px;height:'.$height.'px;position:'.$position.';top:'.$top.'px;left:'.$left.'px;z-index:'.$zIndex.';'); }else { $objWriter->writeAttribute('style', 'width:'.$width.'px;height:'.$height.'px'); } $objWriter->startElement('v:imagedata'); $objWriter->writeAttribute('r:id', 'rId'.$rId); $objWriter->writeAttribute('o:title', ''); $objWriter->endElement(); $objWriter->endElement(); // v:shape $objWriter->endElement();// w:pict $objWriter->endElement(); // w:r if (!$is_textrun) { $objWriter->endElement(); // w:p }}// 图片调用写在上方
效果如下:
参考资料:
http://phpword.codeplex.com/documentation
http://www.jb51.net/article/53975.htm
https://wenku.baidu.com/view/2c63938edd88d0d233d46aa6.html
阅读全文
1 0
- PHPWord 打印word文档[可带图片,相对定位等]
- phpword生成word文档
- 用phpword导出word文档
- PHPWord小记之图片悬浮定位
- 测试带图片的WORD文档。
- PHPWord利用模板替换字符串生成精确的word文档
- PHPWord利用模板替换字符串生成精确的word文档
- 使用PHPWord读取Word文件并插入图片
- PHP_PHP操作Word-PHPWord
- PHPWord 导出生成word
- FCK里粘贴带图片的word文档
- 打印WORD文档
- Javascript 打印本地word文档
- C#直接打印word文档
- 双面小册子--WORD文档打印
- C# Word文档打印方法
- jquery的相对父元素和相对文档定位总结
- C#操作word的一些基本方法(word打印,插入文件,插入图片,定位页眉页脚,去掉横线)
- AlertDialog获取Button,点击外围不消失,设置样式
- 折腾gcc/g++链接时.o文件及库的顺序问题
- Spring Security(05)——异常信息本地化(国际化)
- Redis在Windows下安装配置
- Tomcat8配置优化
- PHPWord 打印word文档[可带图片,相对定位等]
- git学习笔记 -- day03 远程仓库、Linux安装私服
- android 关闭硬件加速
- 软件工程(C语言实践篇)课程总结
- 使用Canvas绘制圆形表盘
- iOS10中UISearchController对searchBar的修改
- Delphi : kbmMemTable关于内存表的使用
- git学习笔记 -- day04 分支管理
- HDU4292 Food(基础最大流)