首頁  >  文章  >  後端開發  >  如何利用PHP將word文檔轉html和pdf

如何利用PHP將word文檔轉html和pdf

墨辰丷
墨辰丷原創
2018-05-15 17:18:314791瀏覽

本篇文章主要介紹如何利用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(&#39;<p>内容</p>&#39;); 
//输出PDF$pdf->Output(&#39;tt .pdf&#39;, &#39;I&#39;);
//I输出、D下载

相關推薦:

##對Python中gensim庫word2vec的使用


使用Python透過win32 COM實作Word文件的寫入與儲存方法

##用python處理MS Word的實例

以上是如何利用PHP將word文檔轉html和pdf的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn