8-4-1
產生docx或odt檔案的基本架構
您沒有觀看影片的權限
請先登入,登入後,確認您的權限後,即可觀看影片。
- PHPWord同屬PHPOffice的系列,官網:https://github.com/PHPOffice/PHPWord
- PHPWord 0.14手冊:http://phpword.readthedocs.io/en/latest/
- PHPWord 0.13手冊:http://karbone-intranet.readthedocs.io/en/latest/
- 需要的環境如下:PHP 5.3.3 以上、ZipArchive、xmllib等PHP函式庫
- 若是您的MS Office是2007以下,那麼可能需要Microsoft Office Compatibility Pack(https://www.microsoft.com/zh-TW/download/details.aspx?id=27836)才能開啟之。PHPWord無法產生2003的doc檔(因為非公開格式)。
- 建立一個word檔,如
word_signup.php
其基本結構如下:
require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
//產生內容
$filename = iconv("UTF-8", "Big5", $filename);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
header('Content-Type: application/vnd.ms-word');
header("Content-Disposition: attachment;filename={$filename}.docx");
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
- 亦可輸出成開放格式odt檔
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
header('Content-Type: application/vnd.oasis.opendocument.text');
header("Content-Disposition: attachment;filename={$filename}.odt");
- 一些預設及建立頁面、設定頁面邊界功能
$phpWord->setDefaultFontName('標楷體'); //設定預設字型
$phpWord->setDefaultFontSize(12); //設定預設字型大小
- 樣式設定有分文字樣式和段落樣式,個人常用的外觀設定:
// 標題文字樣式設定
$TitleStyle = ['color' => '000000', 'size' => 18, 'bold' => true];
// 內文文字設定
$fontStyle = ['color' => '000000', 'size' => 14, 'bold' => false];
// 置中段落樣式設定
$paraStyle = ['align' => 'center', 'valign' => 'center'];
// 靠左段落樣式設定
$left_paraStyle = ['align' => 'left', 'valign' => 'center'];
// 靠又段落樣式設定
$right_paraStyle = ['align' => 'right', 'valign' => 'center'];
- 加入頁首或頁尾
$header = $section->addHeader(); //頁首
$footer = $section->addFooter(); //頁尾
$footer->addPreserveText('{PAGE} / {NUMPAGES}', $fontStyle, $paraStyle);
link to https://github.com/tadlearn/tad_signup/commit/6eb6aacf5704481164e54b0e852a27c350b1e691 \