当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP 导出简单文本内容(word txt等)

PHP 导出简单文本内容(word txt等)

2018年10月22日  | 移动技术网IT编程  | 我要评论

冰雪公主pk恶魔王子,2016欧洲杯直播版权,东兴市政府网

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 . '.pdf', true);
41   //$_obj_mpdf->output($filename . '.pdf', 'd');
42 
43   $_obj_mpdf->output();//输出pdf 浏览器预览文件 可右键保存
44   exit;
45 }
46 
47 $html = '<b style="color: red">你看我哪里像好人</b>';
48 
49 $wordname = 'test-file';
50 
51 export_pdf_by_mpdf($html, $wordname);

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网