• 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 . '.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);
  • 相关阅读:
    2020-12-13 助教一周总结(第十五周)
    2020-12-06 助教一周总结(第十四周)
    2020-11-29 助教一周总结(第十三周)
    2020-11-22 助教一周总结(第十二周)
    2020-11-15 助教一周总结(第十一周)
    暗时间读后感
    2020-11-08 助教一周总结(第十周)
    软件工程助教总结
    2020-12-27助教一周总结(第十七周)
    2020-12-20助教一周总结(第十六周)
  • 原文地址:https://www.cnblogs.com/PHPcoder404/p/9815986.html
Copyright © 2020-2023  润新知