ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP实现word文档转html以及pdf

2021-03-07 09:59:35  阅读:644  来源: 互联网

标签:word section addText 2000 phpWord addCell html table pdf


一 : 使用phpword生成word文档,具体步骤如下:

安装环境配置:

 **必须安装:**1

1. 》=PHP 5.3.3 + 
2. XML Parser extension 
3. Zend\Escaper component 
4. Zend\Stdlib component 
5. Zend\Validator component 
选择性安装: 
Zip extension (Template模板需要的扩展) 
GD extension 
XMLWriter extension 
XSL extension 
dompdf library

可到https://packagist.org/查找所需要的php包类库。

可通过composer安装PHPword,composer require phpoffice\phpword; 
也可以在配置文件中直接添加对PHPword的依赖:

{ 
    “require” : { 
       “phpoffice / phpword” : “v0.14。*” 
    } 
}12345

然后执行composer update,(composer 版本过低,用 composer self-update

使用方法:

        //设置默认样式
        $phpWord->setDefaultFontName('仿宋');//字体
        $phpWord->setDefaultFontSize(16);//字号

        //添加页面
        $section = $phpWord->createSection();        //添加目录
        $styleTOC  = ['tabLeader' => \PhpOffice\PhpWord\Style\TOC::TABLEADER_DOT];        $styleFont = ['spaceAfter' => 60, 'name' => 'Tahoma', 'size' => 12];        $section->addTOC($styleFont, $styleTOC);        //默认样式
        $section->addText('Hello PHP!');        $section->addTextBreak();//换行符

        //指定的样式
        $section->addText(            'Hello world!',
            [                'name' => '宋体',                'size' => 16,                'bold' => true,
            ]
        );        $section->addTextBreak(5);//多个换行符

        //自定义样式
        $myStyle = 'myStyle';        $phpWord->addFontStyle(            $myStyle,
            [                'name' => 'Verdana',                'size' => 12,                'color' => '1BFF32',                'bold' => true,                'spaceAfter' => 20,
            ]
        );        $section->addText('Hello laravel!', $myStyle);        $section->addText('Hello Vue.js!', $myStyle);        $section->addPageBreak();//分页符

        //添加文本资源
        $textrun = $section->createTextRun();        $textrun->addText('加粗', ['bold' => true]);        $section->addTextBreak();//换行符
        $textrun->addText('倾斜', ['italic' => true]);        $section->addTextBreak();//换行符
        $textrun->addText('字体颜色', ['color' => 'AACC00']);        //列表
        $listStyle = ['listType' => \PhpOffice\PhpWord\Style\ListItem::TYPE_NUMBER];        $section->addListItem('List Item I', 0, null, 'listType');        $section->addListItem('List Item I.a', 1, null, 'listType');        $section->addListItem('List Item I.b', 1, null, 'listType');        $section->addListItem('List Item I.c', 2, null, 'listType');        $section->addListItem('List Item II', 0, null, 'listType');        $section->addListItem('List Item II.a', 1, null, 'listType');        $section->addListItem('List Item II.b', 1, null, 'listType');        //超链接
        $linkStyle = ['color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE];        $phpWord->addLinkStyle('myLinkStyle', $linkStyle);        $section->addLink('http://www.baidu.com', '百度一下', 'myLinkStyle');        $section->addLink('http://www.baidu.com', null, 'myLinkStyle');        //添加图片
        $imageStyle = ['width' => 480, 'height' => 640, 'align' => 'center'];        $section->addImage('./img/t1.jpg', $imageStyle);        $section->addImage('./img/t2.jpg',$imageStyle);        //添加标题
        $phpWord->addTitleStyle(1, ['bold' => true, 'color' => '1BFF32', 'size' => 38, 'name' => 'Verdana']);        $section->addTitle('标题1', 1);        $section->addTitle('标题2', 1);        $section->addTitle('标题3', 1);        //添加表格
        $styleTable = [            'borderColor' => '006699',            'borderSize' => 6,            'cellMargin' => 50,
        ];        $styleFirstRow = ['bgColor' => '66BBFF'];//第一行样式
        $phpWord->addTableStyle('myTable', $styleTable, $styleFirstRow);        $table = $section->addTable('myTable');        $table->addRow(400);//行高400
        $table->addCell(2000)->addText('学号');        $table->addCell(2000)->addText('姓名');        $table->addCell(2000)->addText('专业');        $table->addRow(400);//行高400
        $table->addCell(2000)->addText('2015123');        $table->addCell(2000)->addText('小明');        $table->addCell(2000)->addText('计算机科学与技术');        $table->addRow(400);//行高400
        $table->addCell(2000)->addText('2016789');        $table->addCell(2000)->addText('小傻');        $table->addCell(2000)->addText('教育学技术');        //页眉与页脚
        $header = $section->createHeader();        $footer = $section->createFooter();        $header->addPreserveText('页眉');        $footer->addPreserveText('页脚 - 页数 {PAGE} - {NUMPAGES}.');        //生成的文档为Word2007
        $writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');        $writer->save('./word/hello.docx');123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  //Word转HTML
        $phpWord = \PhpOffice\PhpWord\IOFactory::load('./word/hello.docx');        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");        $xmlWriter->save('./html/hello.html'); 1234

二:使用tcpdf生成pdf 
使用composer安装:composer require tecnickcom/tcpdf

使用方法:

$pdf = new \TCPDF();$pdf->writeHTML('<div>内容</div>'); //输出PDF$pdf->Output('tt .pdf', 'I');//I输出、D下载1234

参考链接:http://phpword.readthedocs.io/en/latest/intro.html

相关推荐:

  1. laravel中使用的PDF扩展包——laravel-dompdf和laravel-snappy 

  2. php office文件(word/excel/ppt)转pdf文件

  3. Laravel生成Word文档 - phpword

标签:word,section,addText,2000,phpWord,addCell,html,table,pdf
来源: https://blog.csdn.net/nana837/article/details/114477516

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有