php生成word文件

《php生成word文件》

主要使用phpword 这个组件

官网: https://phpword.readthedocs.io/en/latest/installing.html

#安装
# composer require phpoffice\phpword
#laravel中使用
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$content = ['size' => 12, 'color' => '#222222', 'lineHeight'=>3,];
$Title = ['name' =>'黑体', 'size' => 18, 'color' => '#000000', 'bold' => true];
$Title1 = ['size' => 18, 'color' => '#000000', 'bold' => true, 'align'=>'center'];
$Title2 = ['size' => 42, 'color' => '#000000', 'bold' => true, 'valign' => 'center'];
$Date = ['size' => 18, 'color' => '#000000', 'bold' => false, 'align'=>'center'];
$center=['valign'=>'center', 'align'=>'center'];
$right=['valign'=>'right', 'align'=>'right'];
$left=['valign'=>'left', 'align'=>'left'];

$phpWord->setDefaultFontName('仿宋');//字体
$phpWord->setDefaultFontSize(12);//字号
$phpWord->getSettings()->setUpdateFields(true);
$phpWord->addTitleStyle(1, $Title);
#封面
#封面开始
$section = $phpWord->addSection();
#dd(Storage::path('word/logo2.jpg'));
$section->addImage(Storage::path('word/logo2.jpg'), ['width' => 220,'height'=>85]);
#底图
$textrun = $section->addTextRun();
$section->addImage(Storage::path('word/bg02.jpg'), ['width' => 380,'height'=>480,'wrappingStyle' => 'behind','positioning' => 'absolute','alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT,'align'=>'left']);
$section->addTextBreak(6);
$section->addText($res->Title,$Title2,array_merge($center,['spaceBefore'=>200]));
$section->addTextBreak(8);
#表格
$table = $section->addTable();
$table->addRow();
$cell = $table->addCell(4000)->addText('适用地区:',$Title1,$right);
$cell = $table->addCell(3000)->addText($res->Area->AreaName,$Title1,$left);
$table->addRow();
$cell = $table->addCell(4000)->addText('关 键 字:',$Title1,$right);
$cell = $table->addCell(3000)->addText($res->KeyWords,$Title1,$left);


$section->addTextBreak(3);

$getYear=function($str){
	return str_replace([1,2,3,4,5,6,7,8,9,0],['一','二','三','四','五','六','七','八','九','〇'],$str);
};
$getDay=function($str0)use($getYear){
	$str='';
	if(mb_strlen($str0==2)){
		if(mb_substr($str0,0,1)==1){
			$str='一十';
		}elseif(mb_substr($str0,0,1)==2){
			$str='二十';
		}elseif(mb_substr($str0,0,1)==3){
			$str='三十';
		}
		return $str.$getYear(mb_substr($str0,1,1));
	}
	return $getYear($str0);
};



$date0=explode(',',$res->ReleaseTime);
$date1=$date0[0]?($getYear(date('Y',strtotime($date0[0]))).'年'.$getDay(date('n',strtotime($date0[0]))).'月'.$getDay(date('j',strtotime($date0[0]))).'日'):null;
$date2=$date0[1]?($getYear(date('Y',strtotime($date0[1]))).'年'.$getDay(date('n',strtotime($date0[1]))).'月'.$getDay(date('j',strtotime($date0[1]))).'日'):null;
#dd($date1);
$section->addText($date1.' 至 '.$date2,$Date,$center);
#正文开始
$section = $phpWord->addSection();
#页眉
$header = $section->addHeader();
$header->addWatermark(Storage::path('word/logo1.jpg'), array('width' => 30,'height'=>30,'marginTop' => -20, 'marginLeft' => 0)); 
$header->addText(      '     '.(($res->PolicyType && $res->PolicyType->Title)?$res->PolicyType->Title:'').'报告                                            '.$date1.' 至 '.$date2,['size'=>10,'color' => '#444444','alignment'=>'left','align'=>'left']);
#页脚
$footer = $section->addFooter();
$footer->addPreserveText('第 {PAGE} / {NUMPAGES} 页', null, ['alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER]);

#目录开始
$section->addText('目 录' ,['name' =>'黑体','size' => 24,'color' => '#333333','bold' => true],$center);
#底图
$section->addImage(Storage::path('word/bg02.jpg'), ['width' => 380,'height'=>480,'wrappingStyle' => 'behind','positioning' => 'absolute','alignment' => \PhpOffice\PhpWord\SimpleType\Jc::LEFT,'align'=>'left']);
$section->addTOC(['spaceAfter'=>60], ['tabLeader'=>\PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT]);
$section->addPageBreak();
#增加一页
foreach([....] as $k=> $v){
	#dump(strip_tags($v['Content']));
	$section->addTitle($v['Title'], 1);
	$textrun = $section->addTextRun();
	#dump(str_replace(' ',' ',strip_tags($v['Content']));
	$textrun->addText(str_replace(' ',' ',strip_tags($v['Content'])), $content);
	#$section->addPageBreak();
	
}
#生成Word文档
#$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
#$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
#$objWriter->save(Storage::path('01.docx'));

#下载Word文档
#$file = Storage::path('01.docx');
$file = md5($res->Title).'.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");

使用tcpdf生成pdf

//安装
# composer require tecnickcom/tcpdf
//使用
$pdf = new \TCPDF();
$pdf->writeHTML('<div>内容</div>');
//输出PDF
$pdf->Output('tt .pdf', 'I');//I输出、D下载
点赞

发表评论

邮箱地址不会被公开。 必填项已用*标注