PHPWord 打印word文档[可带图片,相对定位等]

下载:
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 
    }
}

// 图片调用写在上方

效果如下:
PHPWord 打印word文档[可带图片,相对定位等]_第1张图片
PHPWord 打印word文档[可带图片,相对定位等]_第2张图片

参考资料:
http://phpword.codeplex.com/documentation
http://www.jb51.net/article/53975.htm
https://wenku.baidu.com/view/2c63938edd88d0d233d46aa6.html

你可能感兴趣的:(php,PHPWord)