Skip to content

Instantly share code, notes, and snippets.

@thomasjao
Last active October 23, 2015 04:44
Show Gist options
  • Save thomasjao/316fb835dd1e3a3333aa to your computer and use it in GitHub Desktop.
Save thomasjao/316fb835dd1e3a3333aa to your computer and use it in GitHub Desktop.
<?php
// PHPWord 在 DocumentRoot 的上一層目錄
$path = dirname(dirname(__FILE__))."/PHPWord/";
require_once($path."PHPWord.php");
$doc = new PHPWord();
// 以 section 物件加入內容
$sec = $doc->createSection();
$sec->addText("ABC");
$sec->addText("藉由 PHPWord 程式庫建立 .docx, .odt, .rtf 檔", array("name"=>"Arial", "size"=>14, "bold"=>true));
// 加入分頁符號
$sec->addPageBreak();
// 列表
$sec->addListItem($text, [$depth], [$styleText], [$styleList], [$styleParagraph]);
$listStyle = array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER);
$sec->addListItem('Listitem 1', 0, null, $listStyle);
/* List Style Properties
- listType List point style.
*/
// 超連結
// $sec->addLink( $linkSrc, [$linkName], [$styleFont], [$styleParagraph]);
$linkStyle = array('color'=>'0000FF', 'underline'=>PHPWord_Style_Font::UNDERLINE_SINGLE);
$sec->addLink('http://www.google.com', null, $linkStyle);
$section->addImage('EARTH.jpg', $imageStyle);
// 圖形
// $sec->addImage( $src, [$style]);
$imageStyle = array('width'=>350, 'height'=>350, 'align'=>'center');
// 加入記憶體中的影像
// $section->addMemoryImage( $link, [$style]);
$sec->addMemoryImage('http://localhost/image.php');
// 加入浮水印
// $section->addWatermark( $src, [$style]);
// 加入物件
// $section->addObject( $src, [$style]);
// 加入標題
// $PHPWord->addTitleStyle( $titleCount, [$fontStyle]);
// $sec->addTitle( $text, [$depth]);
// 加入目錄
$styleTOC = array('tabLeader'=>PHPWord_Style_TOC::TABLEADER_DOT);
$styleFont = array('spaceAfter'=>60, 'name'=>'Tahoma', 'size'=>12);
$sec->addTOC($styleFont, $styleTOC);
// 表格
// $table = $section->addTable([$tableStyle]);
// 加入列
// $table->addRow([$height]);
// $cell = $table->addCell( $width, [$cellStyle]);
$table = $sec->addTable();
$table->addRow();
$cell = $table->addCell(2000);
$cell->addText('Cell 1');
$cell = $table->addCell(2000);
$cell->addText('Cell 2');
// 格式化儲存格
$cellStyle = array('textDirection'=>PHPWord_Style_Cell::TEXT——DIR——BTLR, 'bgColor'=>'C0C0C0');
$table = $section->addTable();
$table->addRow(1000);
$table->addCell(2000, $cellStyle)->addText('Cell 1');
.....
$table->addRow();
$table->addCell(2000)->addText('Cell 4');
.....
// 頁尾
// $footer = $section->createFooter();
// addPreserveText( $text, [$style]);
// $footer->addPreserveText('Page {PAGE} of {NUMPAGES}.');
// 頁首
// $header = $section->createHeader();
// Header 和 Footer 可以設定的參數一樣
// 樣板
$template = $PHPWord->localTemplate('Template.docx');
$template->setValue('Name', 'Somebody someone');
$template->setValue('Street', 'Coming-Undone-Street 32');
require_once($path."PHPWord/IOFactory.php");
$obj = PHPWord_IOFactory::createWriter($doc, "Word2007");
$filename = "myWord.docx";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Desposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream");
header("Content-Transfer-Encoding: binary");
ob_end_clean(); // Clean PHP output buffer to avoid ERROR 500 Web Page Not Available message
$obj->save("php://output");
?>
/* 以上程式段落簡短,然而實際情況沒有那麼簡單,在原始程式庫中有幾個檔案必須稍作修改。
$... = utf8_encoding($text)... ====> $... = ($text) // 可能是其他變數
這個方法是依據 http:uranus.im.chu.edu.tw/wordpress/?p=286,比其他的解決方法簡單多了
有好幾個檔案須作修改的,記錄如下:
PHPWord/Section.php // Line 112, Line 129, Line 131, Line 183, Line 291
PHPWord/Template.php // Line 89
PHPWord/Section/Footer.php // Line 75 & Line 165
PHPWord/Sectoin/Header.php // Line 75
PHPWord/Section/TextRun.php // Line 83, Line 98 & Line 100 $linkSrc & $linkName
PHPWord/Section/Table/Cell.php // Line 111, Line 127, Line 129, Line 163, Line 272 $text/$linkSrc/$linkName */
此外,還需修改 PHPWord/Writer/Word2007/Base.php中
if($font != 'Arial') {
$objWriter->startElement('w:rFonts');
$objWriter->writeAttribute('w:eastAsia', $font); // Line 312,這一行是額外加上的,作用待深入研究
$objWriter->writeAttribute('w:ascii', $font);
*/
@thomasjao
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment