PhpOffice——PHPWord导入导出水印模板替换

当然:PHPWord也支持导出pdf,不过依赖于MPDF或者tcpdf或者dompdf
PHPWord 需要以下内容:
PHP 5.3.3+
XML 解析器扩展
Laminas Escaper 组件 ($ composer require laminas/laminas-escaper)
Zip 扩展(可选,用于编写 OOXML 和 ODF)
GD 扩展(可选,用于添加图片)
XMLWriter 扩展(可选,用于编写 OOXML 和 ODF)
XSL 扩展(可选,用于将 XSL 样式表应用于模板)
dompdf 库(可选,用于编写 PDF)

用场景:
1、导入、可以把数据或者word文档转成html存储
2、导出、可以把数据或者html转成word
3、添加水印(创建新文件、添加水印、把源文件的内容转成html、把源文件的html添加到新文件中、保存并输出文件地址)
4、模板替换(云合同的签约)
5、其他
1、可以把数据或者word文档转成html存储

 /**
     * 将word文件转html代码
     * @param string $filePath docx 文件路径
     * @param boolean $saveHtml 是否保存html(依赖于isDownload,但是不会下载html页面) 否则输出html实体内容
     * @return string html|path|document
     */
    public function wordToHtml($filePath, $saveHtml = false)
    {
        if (!file_exists($filePath)) {
            throw new \Exception("文件不存在,请检查文件路径");
        }
        //第二个参数是默认值,可以不填写
        $phpWord = IOFactory::load($filePath, "Word2007");
        $writer = IOFactory::createWriter($phpWord, "HTML");
        if ((bool) $saveHtml) {
            //返回html实体内容
            return $writer->getContent();
        } else {
            if ($this->isDownload) {
                //直接显示 html
                $phpWord->save('php://output', 'HTML');
                exit;
            } else {
                //保存至服务器
                $phpWord->save($this->saveFilePath, 'HTML');
                //返回文件路径
                return $this->saveFilePath;
            }
        }
    }

2、html转word

   /**
     * 使用这个方法 downloadClass === 
     * 下载文件支持: docx  
     * 保存文件至服务器支持 .html,docx,...【如果需要更多格式,比如odt,记得在writers里面添加--必须是phpword里面支持的 pdf的话,我】
     */
    public function createServer()
    {
        //只用来下载docx 文件
        if ($this->downloadClass !== 'Word2007' && $this->isDownload) {
            throw new \Exception("当前只支持docx文件下载");
        }

        $this->header();
        //设置允许的请求时间
        @set_time_limit(5 * 60);

        $phpWord = new PhpWord();
        $section = $phpWord->addSection();
        $html = new Html();
        $html::addHtml($section, $this->wordData, false, false);

        if ($this->isDownload) {
            $phpWord->save('php://output');
            exit;
        } else {
            $phpWord->save($this->saveFilePath, $this->downloadClass);
            //返回文件路径
            return $this->saveFilePath;
        }
    }

3、添加水印(只有创建的时候可以、所以需要源文件添加的话,请自行转换)

$PHPWord = new PHPWord();

// New portrait section
$section = $PHPWord->createSection();

// Create header
$header = $section->createHeader();

// Add a watermark to the header
$header->addWatermark('_earth.jpg', array('marginTop'=>200, 'marginLeft'=>55));

 
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('Watermark.docx');

4、模板替换

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Template.docx');

$document->setValue('Value1', 'Sun');
$document->setValue('Value2', 'Mercury');
$document->setValue('Value3', 'Venus');
$document->setValue('Value4', 'Earth');
$document->setValue('Value5', 'Mars');
$document->setValue('Value6', 'Jupiter');
$document->setValue('Value7', 'Saturn');
$document->setValue('Value8', 'Uranus');
$document->setValue('Value9', 'Neptun');
$document->setValue('Value10', 'Pluto');

$document->setValue('weekday', date('l'));
$document->setValue('time', date('H:i'));

$document->save('Solarsystem.docx');

5、其他

header("Content-Type: text/html; charset=UTF-8");
include "phpword/PHPWord.php";

$PHPWord = new PHPWord();
$PHPWord->addFontStyle('rStyle', array('bold' => true, 'italic' => true, 'size' => 16));
$PHPWord->addParagraphStyle('pStyle', array('align' => 'center', 'spaceAfter' => 100));
$PHPWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));


$section = $PHPWord->createSection();//创建新页面



$section->addTitle('欢迎使用', 1);
$section->addText('中国');

$section->addTextBreak(2);


// Link 
$section->addLink('http://www.google.com', null, '中国');
$section->addTextBreak();


// Image
$section->addImage('1.jpg', array('width'=>180, 'height'=>180));

//表格--------------------------------------------------------------------------------------------

// Define table style arrays
$styleTable = array('borderSize'=>6, 'borderColor'=>'006699', 'cellMargin'=>80);
$styleFirstRow = array('borderBottomSize'=>18, 'borderBottomColor'=>'0000FF', 'bgColor'=>'66BBFF');

// Define cell style arrays
$styleCell = array('valign'=>'center');
$styleCellBTLR = array('valign'=>'center', 'textDirection'=>PHPWord_Style_Cell::TEXT_DIR_BTLR);

// Define font style for first row
$fontStyle = array('bold'=>true, 'align'=>'center');

// Add table style
$PHPWord->addTableStyle('myOwnTableStyle', $styleTable, $styleFirstRow);

// Add table
$table = $section->addTable('myOwnTableStyle');

// Add row
$table->addRow(900);

// Add cells
$table->addCell(2000, $styleCell)->addText('姓名 1', $fontStyle);
$table->addCell(2000, $styleCell)->addText('性别 2', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 3', $fontStyle);
$table->addCell(2000, $styleCell)->addText('Row 4', $fontStyle);
$table->addCell(500, $styleCellBTLR)->addText('Row 5', $fontStyle);

// Add more rows / cells
for($i = 1; $i <= 10; $i++) {
	$table->addRow();
	$table->addCell(2000)->addText("我 $i");
	$table->addCell(2000)->addText("Cell $i");
	$table->addCell(2000)->addText("Cell $i");
	$table->addCell(2000)->addText("Cell $i");
	
	$text = ($i % 2 == 0) ? 'X' : '';
	$table->addCell(500)->addText($text);
}
//-------------------------------------表格结束-------------------------------------------------------
// Add table
$table = $section->addTable();

for($r = 1; $r <= 10; $r++) { // Loop through rows
	// Add row
	$table->addRow();
	
	for($c = 1; $c <= 5; $c++) { // Loop through cells
		// Add Cell
		$table->addCell(1750)->addText("行 $r, 列 $c");
	}
}
//-----------------------------简单表格----------------------------
// Add 页头
$header = $section->createHeader();
$table = $header->addTable();
$table->addRow();
$table->addCell(4500)->addText('我是页头.');
$table->addCell(4500)->addImage('1.jpg', array('width'=>50, 'height'=>50, 'align'=>'right'));

// Add 页尾
$footer = $section->createFooter();
$footer->addPreserveText('第 {PAGE} 页共 {NUMPAGES} 页', array('align'=>'center'));

$section->addTextBreak(2);
// Add hyperlink elements
$section->addLink('http://www.google.com', '最好的搜索', array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE));
$section->addTextBreak(2);

$PHPWord->addLinkStyle('myOwnLinkStyle', array('bold'=>true, 'color'=>'808000'));
$section->addLink('http://www.bing.com', '描述', 'myOwnLinkStyle');
$section->addLink('http://www.yahoo.com', '超级链接', 'myOwnLinkStyle');


// Add listitem elements
$section->addListItem('列表 1', 0);
$section->addListItem('List Item 1.1', 1);
$section->addListItem('List Item 1.2', 1);
$section->addListItem('List Item 1.3 (styled)', 1, array('bold'=>true));
$section->addListItem('List列表Item 1.3.1', 2);
$section->addListItem('List Item 1.3.2', 2);
$section->addTextBreak(2);


$objWriter =  IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('demo.doc'); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值