设为首页 加入收藏

TOP

PHP 导出简单文本内容(word txt等)(一)
2019-08-23 00:34:57 】 浏览:34
Tags:PHP 导出 简单 文本 内容 word txt

PHP导出word文件,简单拓展可导出其他文本类文件

 

 1 /**
 2  * PHP 导出简单文本内容(word txt等)
 3  * @param $content mixed 导出内容 (文本string  /  html代码)
 4  * @param $filename string 需保存文件名
 5  * @param string $extension 文件类型 (doc docx txt xml)
 6  */
 7 function export_html_to_word($content, $filename, $extension = 'doc')
 8 {
 9   ob_start();
10 
11   $_export_content = '';
12   if ($extension == 'doc' || $extension == 'docx') {
13     $_export_content .= '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">';
14   }
15 
16   $_export_content .= $content;
17 
18   if ($extension == 'doc' || $extension == 'docx') $_export_content .= '</html>';
19 
20   echo $_export_content;
21 
22   ob_get_contents();
23 
24   if ($extension == 'doc' || $extension == 'docx') header('Content-type:application/word');
25   header('Content-Disposition: attachment; filename=' . $filename . '.' . $extension);
26 
27   ob_flush();
28   flush();
29 }
30 
31 $html = '<b style="color: red">你看我哪里像好人</b>';
32 
33 $wordname = 'test-file';
34 
35 export_html_to_word($html, $wordname);

 

 

附:mpdf导出PDF

 1 /**
 2  * PHP 使用 mpdf 导出PDF文件
 3  * @param $content     string  PDF文件内容   若为html代码,css内容分离   非id,class选择器可能失效,解决办法直接写进标签style中
 4  * @param $filename    string 保存文件名
 5  * @param $css         string css样式内容
 6  */
 7 function export_pdf_by_mpdf($content, $filename, $css = '')
 8 {
 9   set_time_limit(0);
10 
11   include_once './mpdf/mpdf.php';
12 
13   //实例化mpdf
14   $_obj_mpdf = new \mPDF('utf-8', 'A4', '', '宋体', 0, 0, 20, 10);
15 
16   //设置PDF页眉内容 (自定义编辑样式)
17   $header = '<table width="95%" style="margin:0 auto;border-bottom: 1px solid #4F81BD; vertical-align: middle; font-family:serif; font-size: 9pt; color: #000088;">
18                 <tr><td width="10%"></td><td width="80%" align="center" style="font-size:16px;color:#A0A0A0">页眉</td><td width="10%" style="text-align: right;"></td></tr></table>';
19 
20   //设置PDF页脚内容 (自定义编辑样式)
21   $footer = '<table width="100%" style=" vertical-align: bottom; font-family:serif; font-size: 9pt; color: #000088;"><tr style="height:30px"></tr><tr>
22                 <td width="10%"></td><td width="80%" align="center" style="font-size:14px;color:#A0A0A0">页脚</td><td width="10%" style="text-align: left;">
23                 页码:{PAGENO}/{nb}</td></tr></table>';
24 
25   //添加页眉和页脚到PDF中
26   $_obj_mpdf->SetHTMLHeader($header);
27   $_obj_mpdf->SetHTMLFooter($footer);
28 
29   $_obj_mpdf->SetDisplayMode('fullpage');//设置PDF显示方式
30 
31   $_obj_mpdf->WriteHTML('<pagebreak sheet-size="210mm 297mm" />');//设置PDF的尺寸   A4纸规格尺寸:210mm*297mm
32 
33   !empty($css) && $_obj_mpdf->WriteHTML($css, 1);//设置PDF css样式
34 
35   $_obj_mpdf->WriteHTML($content);//将$content内容写入PDF
36 
37   $_obj_mpdf->DeletePages(1, 1);//删除PDF第一页(由于设置PDF尺寸导致多出的一页)
38 
39   //输出PDF 直接下载PDF文件
40   //$_obj_mpdf->Output($filename
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇[PHP] strpos stripos strrpos st.. 下一篇百度编辑器修改

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目