您好,欢迎访问一九零五行业门户网

如何利用PHP将word文档转html和pdf

本篇文章主要介绍如何利用php将word文档转html和pdf,感兴趣的朋友参考下,希望对大家有所帮助。
一 : 使用phpword生成word文档,具体步骤如下:
安装环境配置: **必须安装:**
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。*” } }
然后执行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');
//word转html $phpword = \phpoffice\phpword\iofactory::load('./word/hello.docx'); $xmlwriter = \phpoffice\phpword\iofactory::createwriter($phpword, "html"); $xmlwriter->save('./html/hello.html');
二:使用tcpdf生成pdf
使用composer安装:composer require tecnickcom/tcpdf
使用方法:
$pdf = new \tcpdf();$pdf->writehtml('<p>内容</p>'); //输出pdf$pdf->output('tt .pdf', 'i'); //i输出、d下载
相关推荐:
对python中gensim库word2vec的使用
使用python通过win32 com实现word文档的写入与保存方法
用python处理ms word的实例
以上就是如何利用php将word文档转html和pdf的详细内容。
其它类似信息

推荐信息