本篇文章主要介紹如何利用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下載