这里我是使用composer导入相关使用包的,如果没有安装composer,先要安装composer。
需要加载的包有以下两个:
composer require phpoffice/phpword
composer require tecnickcom/tcpdf
- 模板操作
1.控制器中使用
// $PhpWord = new PhpWord();
// $document = $PhpWord->loadTemplate( 'helloWorld6.docx');
$document = new TemplateProcessor('test 1.docx');
$document->setValue('address', '杭州');
$document->setValue('day', '28');
$document->setValue('price', '0.2');
$document->saveAs('test_new.docx');
2.模板中使用
地点:${address}
日期:${day}
价格:${price}
- word转Html
1.控制器中代码使用
$phpWord = IOFactory::load('./test 1.docx');
$writer = IOFactory::createWriter($phpWord, 'HTML');
$writer->save('helloWorld.html');
- word转pdf(需先安装字体,建议字体droidsansfallback)
1.tcpdf安装字体
# 将下载ttf字体放在此目录下
vendor/tecnickcom/tcpdf/tools
# 安装字体,注意字体不要有空格(在vendor/tecnickcom/tcpdf/tools目录下执行)
# ./tcpdf_addfont.php -b -t 字体名 -f 32 -i 字体文件
./tcpdf_addfont.php -b -t Droid -f 32 -i DroidSansFallback.ttf
# 用法
$pdf->SetFont('droidsansfallback', ''); #新增支持中文的字体
2.转dompdf
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
Settings::setPdfRendererPath('.');
Settings::setDefaultFontName('droidsansfallback');
$phpWord = IOFactory::load('test.docx', 'Word2007');
$phpWord->save('test.pdf', 'PDF');
3.转tcpdf
Settings::setPdfRendererName(Settings::PDF_RENDERER_TCPDF);
Settings::setPdfRendererPath('vendor/tecnickcom/tcpdf');
$phpWord = IOFactory::load('./test.docx');
$phpWord->save('test.pdf', 'PDF');
乱码解决:找到vendor/tecnickcom/tcpdf/config/tcpdf_config.php这个文件,将配置文件中的字体改成受支持的中文字体,
define ('PDF_FONT_NAME_MAIN', 'droidsansfallback');
生成多页带表格的docx文档
$data = [
[
'realName' => '真实姓名1',
'phone' => '12345678911',
'way' => '银行卡',
'parentName' => '',
'bank' => '中国银行',
'subbranch' => 'xxx支行',
'account' => '14725836974511223',
'tag' => '',
'children' => [
[
'realName' => '真实姓名3',
'phone' => '88566611155',
'way' => '银行卡',
'parentName' => '真实姓名1',
'bank' => '中国银行',
'subbranch' => 'xxx支行',
'account' => '14725836974511223',
'tag' => '',
],
],
],
[
'realName' => '真实姓名2',
'phone' => '213545',
'way' => '银行卡',
'parentName' => '',
'bank' => '中国银行',
'subbranch' => 'xxx支行',
'account' => '14725836974511223',
'tag' => '',
'children' => [
[
'realName' => '真实姓名4',
'phone' => '88566611155',
'way' => '银行卡',
'parentName' => '真实姓名2',
'bank' => '中国银行',
'subbranch' => 'xxx支行',
'account' => '14725836974511223',
'tag' => '',
],
],
],
];
$phpWord = new PhpWord();
$phpWord->setDefaultFontName('微软雅黑');
$phpWord->setDefaultFontSize(15);
$phpWord->setDefaultParagraphStyle([
'align' => 'center',
'spaceBefore' => Converter::pointToTwip(0),
'spaceAfter' => Converter::pointToTwip(0),
'spacing' => 20,
]);
$i = 0;
foreach ($data as $val) {
$no = 1;
$section = $phpWord->addSection([
'orientation' => 'landscape',
'footerHeight' => Converter::cmToTwip(1.45),//设置页脚行高
'size' => 10,
]);
$section->addTitle('xxx' . $val['realName'] . '组xxx月份劳务工资发放明细汇总表');
$phpWord->addTableStyle('tableStyle', ['borderSize' => '6']);//设置表格边框为实线
$table = $section->addTable('tableStyle');//如果没有表格样式,直接$table = $section->addTable();即可
$styleCell = ['valign'=>'center'];
$fontStyle = ['align'=>'center', 'size' => 13];
$table->addRow();
$table->addCell(900, $styleCell)->addText('序号', $fontStyle);
$table->addCell(1100, $styleCell)->addText('姓名', $fontStyle);
$table->addCell(2000, $styleCell)->addText('电话', $fontStyle);
$table->addCell(1300, $styleCell)->addText('提现方式', $fontStyle);
$table->addCell(1100, $styleCell)->addText('委托人', $fontStyle);
$table->addCell(1500, $styleCell)->addText('开户行', $fontStyle);
$table->addCell(2000, $styleCell)->addText('支行', $fontStyle);
$table->addCell(3200, $styleCell)->addText('卡号', $fontStyle);
// $table->addCell(1100, $styleCell)->addText('实发款', $fontStyle);
$table->addCell(1100, $styleCell)->addText('签名', $fontStyle);
$table->addRow();
$table->addCell(900, $styleCell)->addText($no, $fontStyle);
$table->addCell(1100, $styleCell)->addText($val['realName'], $fontStyle);
$table->addCell(2000, $styleCell)->addText($val['phone'], $fontStyle);
$table->addCell(1300, $styleCell)->addText($val['way'], $fontStyle);
$table->addCell(1100, $styleCell)->addText($val['parentName'], $fontStyle);
$table->addCell(1500, $styleCell)->addText($val['bank'], $fontStyle);
$table->addCell(2000, $styleCell)->addText($val['subbranch'], $fontStyle);
$table->addCell(3200, $styleCell)->addText($val['account'], $fontStyle);
// $table->addCell(1100, $styleCell)->addText('实发款', $fontStyle);
$table->addCell(1100, $styleCell)->addText($val['tag'], $fontStyle);
foreach ($val['children'] as $v) {
$table->addRow();
$table->addCell(900, $styleCell)->addText($no + 1, $fontStyle);
$table->addCell(1100, $styleCell)->addText($v['realName'], $fontStyle);
$table->addCell(2000, $styleCell)->addText($v['phone'], $fontStyle);
$table->addCell(1300, $styleCell)->addText($v['way'], $fontStyle);
$table->addCell(1100, $styleCell)->addText($v['parentName'], $fontStyle);
$table->addCell(1500, $styleCell)->addText($v['bank'], $fontStyle);
$table->addCell(2000, $styleCell)->addText($v['subbranch'], $fontStyle);
$table->addCell(3200, $styleCell)->addText($v['account'], $fontStyle);
// $table->addCell(1100, $styleCell)->addText('实发款', $fontStyle);
$table->addCell(1100, $styleCell)->addText($v['tag'], $fontStyle);
$no++;
}
$remarkTextStyle = ['size' => 13, 'bold' => false];
$remarkPStyle = ['align' => 'left'];
$section->addText('');
$section->addText('备注:', $remarkTextStyle, $remarkPStyle);
$section->addText('1.' . $val['realName'] . ' ' . $val['bank'] . ' ' . $val['subbranch'] . ' ' . $val['account'], $remarkTextStyle, $remarkPStyle);
$section->addText('2.打款时请备注:' . $val['realName'] . '组' . count($val['children']) . '人劳务工资', $remarkTextStyle, $remarkPStyle);
$i++;
$footer = $section->createFooter();
$footer->addPreserveText('第 {PAGE} 页,共 {NUMPAGES} 页 ' . $lastMonth . '月xx批-' . date('m.d'), ['size' => 9], ['align' => 'right']);
}
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save(__DIR__ . '/劳务公司工资发放汇总表.docx');//保存在当前目录下
// header('Content-type: application/vnd.ms-word');
// header('Content-Disposition:attachment;filename = 劳务公司工资发放汇总表.docx');
// header('Cache-Control: max-age=0');
// $objWriter->save('php://output');
exit;
结果如下: