$style = array(
'orientation' => 'portrait', //landscape(橫);portrait(直)
'marginTop' => 1133.858268, //上邊界 2cm*566.929134
'colsNum' => 2, //幾欄
'pageNumberingStart' => 1, //第幾頁
'borderTopSize' => 2, //上邊框
'borderTopColor' => '0000FF', //上邊框顏色
'lineNumbering' => array('start' => 1, 'increment' => 1), //行號設定
);
$section = $phpWord->addSection($style); //建立一個區域
(1) 樣式參數可參考:http://karbone-intranet.readthedocs.io/en/latest/styles.html
(2) 其中有方向性的分別還有Top、Bottom、Left、Right,自行新增或修改即可。
(3) 若有用到長度,其單位為twip,1 cm = 566.929134 twip,1 twip = 0.001764 cm,亦可用底下方式將公分轉換為twip
use PhpOffice\PhpWord\Shared\Converter;
Converter::cmToTwip(2.5)
$section = $phpWord->addSection();
$sectionStyle = $section->getStyle();
$sectionStyle->setMarginTop(Converter::cmToTwip(2.5));
$sectionStyle->setMarginLeft(Converter::cmToTwip(2.2));
$sectionStyle->setMarginRight(Converter::cmToTwip(2.2));
$depth
是指標題N)
$phpWord->addTitleStyle($depth, $TitleStyle, $paraStyle); //設定標題N樣式
$section->addTitle('標題文字', $depth); //新增標題
如:
//產生內容
$title = "{$action['title']}簽到表";
$phpWord->addTitleStyle(1, $TitleStyle, $paraStyle); //設定標題N樣式
$section->addTitle($title, 1); //新增標題
$section->addText('文字', $fontStyle, $paraStyle);
$textrun = $section->addTextRun($paraStyle);
addTextRun()
用來將同一段落中的文字,分別套用不同文字設定,為物件。
$section->addTextBreak(2, $fontStyle, $paraStyle); //換行,可指定換幾行
$section->addPageBreak(); //換頁
$style
可用設定:width
、height
、align
,單位為px
$section->addLink($linkSrc, $linkName, $fontStyle, $paraStyle); //加入連結
$section->addImage($src, $style); //插入圖片
TYPE_NUMBER
)或無序(TYPE_BULLET_FILLED
)清單,$depth
為階層,從 0
開始。
$listStyle = array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER);
$section->addListItem($text, $depth, $fontStyle, $listStyle, $paraStyle);
word_signup.php
檔
<?php
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Shared\Converter;
use Xmf\Request;
use XoopsModules\Tad_signup\Tad_signup_actions;
use XoopsModules\Tad_signup\Tad_signup_data;
/*-----------引入檔案區--------------*/
require_once __DIR__ . '/header.php';
if (!$_SESSION['can_add']) {
redirect_header($_SERVER['PHP_SELF'], 3, "您沒有權限使用此功能");
}
$id = Request::getInt('id');
$action = Tad_signup_actions::get($id);
require_once XOOPS_ROOT_PATH . '/modules/tadtools/vendor/autoload.php';
$phpWord = new PhpWord();
$phpWord->setDefaultFontName('標楷體'); //設定預設字型
$phpWord->setDefaultFontSize(12); //設定預設字型大小
// $header = $section->addHeader(); //頁首
// $footer = $section->addFooter(); //頁尾
// $footer->addPreserveText('{PAGE} / {NUMPAGES}', $fontStyle, $paraStyle);
// 標題文字樣式設定
$Title1Style = ['color' => '000000', 'size' => 18, 'bold' => true];
$Title2Style = ['color' => '000000', 'size' => 16, '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'];
$phpWord->addTitleStyle(1, $Title1Style, $paraStyle); //設定標題1樣式
$phpWord->addTitleStyle(2, $Title2Style, $paraStyle); //設定標題1樣式
//產生內容
$section = $phpWord->addSection();
$sectionStyle = $section->getStyle();
$sectionStyle->setMarginTop(Converter::cmToTwip(2.5));
$sectionStyle->setMarginLeft(Converter::cmToTwip(2.2));
$sectionStyle->setMarginRight(Converter::cmToTwip(2.2));
$title = "{$action['title']}簽到表";
$section->addTitle($title, 1); //新增標題
$section->addTextBreak(1);
$section->addText("活動日期:{$action['action_date']}", $fontStyle, $left_paraStyle);
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
header('Content-Type: application/vnd.ms-word');
header("Content-Disposition: attachment;filename={$title}.docx");
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
link to https://github.com/tadlearn/tad_signup/commit/084658881b77fd6414770d0a8822f42ef1915c25 \