英文:
PHPWord - setting font inside Template Processor
问题
我正在使用PHPWord加载一个模板文件并从中创建一个新文件。
$templateName = 'QuoteTemplate1.docx';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templateName);
$values = ['clientAddressName' => $quote->company_name]; // 这个数组通常包含更多值
foreach ($values as $key => $value) {
$templateProcessor->setValue($key, $value);
}
然后,我在这个模板中添加一个自定义表格,代码如下:
$table = new PhpOffice\PhpWord\Element\Table([
'borderSize' => 0,
'borderColor' => 'none',
'width' => 9200,
'unit' => PhpOffice\PhpWord\SimpleType\TblWidth::TWIP
]);
$table->addRow();
$table->addCell(150)->addText('Cell A1');
$table->addCell(150)->addText('Cell A2');
$table->addCell(150)->addText('Cell A3');
$table->addRow();
$table->addCell(150)->addText('Cell B1');
$table->addCell(150)->addText('Cell B2');
$table->addCell(150)->addText('Cell B3');
$templateProcessor->setComplexBlock('quoteItemTable', $table);
我想在这个自定义表格中添加字体和段落样式 - 这里出现了问题。
如果我尝试像这样:
$templateProcessor->addParagraphStyle('rightAlign', ['alignment' => 'right']);
那么我会得到错误(addParagraphStyle不是$templateProcessor的一个已识别的方法)。如果我尝试:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('rightAlign', ['alignment' => 'right']);
$table->addCell(25)->addText('Cell A1', 'fontStyle', 'rightAlign');
然后我不会得到错误,但我的rightAlign段落样式会被忽略。请注意,当我尝试字体样式和段落样式时,我得到相同的结果。
如何在模板处理器内设置自定义字体和段落样式?
英文:
I am using PHPWord to load a template file and create a new one from it.
$templateName = 'QuoteTemplate1.docx';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templateName);
$values = ['clientAddressName' => $quote->company_name]; // this array typically has more values
foreach ($values as $key => $value) {
$templateProcessor->setValue($key, $value);
}
Then I am adding a custom built table to this template, the code is like this:
$table = new PhpOffice\PhpWord\Element\Table([
'borderSize' => 0,
'borderColor' => 'none',
'width' => 9200,
'unit' => PhpOffice\PhpWord\SimpleType\TblWidth::TWIP
]);
$table->addRow();
$table->addCell(150)->addText('Cell A1');
$table->addCell(150)->addText('Cell A2');
$table->addCell(150)->addText('Cell A3');
$table->addRow();
$table->addCell(150)->addText('Cell B1');
$table->addCell(150)->addText('Cell B2');
$table->addCell(150)->addText('Cell B3');
$templateProcessor->setComplexBlock('quoteItemTable', $table);
I want to add font and paragraph styles to the text in this custom table - and here lies the problem.
If I try something like this:
$templateProcessor->addParagraphStyle('rightAlign', ['alignment' => 'right']);
The I get errors (addParagraphStyle is not a recognized method of $templateProcessor). And if I try:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord->addParagraphStyle('rightAlign', ['alignment' => 'right']);
$table->addCell(25)->addText('Cell A1', 'fontStyle', 'rightAlign');
Then I get no errors, but my rightAlign paragraph style is ignored. Please note I get the same results when I try the steps with font styles as well as paragraph styles.
How can I set my own font and paragraph styles inside a template processor?
答案1
得分: 2
以下是已翻译的代码部分:
以下是已翻译的代码部分:
$my_template = new \PhpOffice\PhpWord\TemplateProcessor(storage_path('template1.docx'));
$table = new \PhpOffice\PhpWord\Element\Table();
$myFontStyle = array('name' => 'Minion Pro', 'size' => 10, 'bold' => true);
$myParagraphStyle = array('align'=>'center', 'spaceBefore'=>50, 'spaceafter' => 50);
$table->addRow();
$table->addCell()->addText('Cell 1', $myFontStyle, $myParagraphStyle );
$table->addCell()->addText('Cell 2', $myFontStyle, $myParagraphStyle );
$table->addCell()->addText('Cell 3', $myFontStyle, $myParagraphStyle );
$my_template->setComplexBlock('table', $table);
英文:
The following worked for me:
$my_template = new \PhpOffice\PhpWord\TemplateProcessor(storage_path('template1.docx'));
$table = new \PhpOffice\PhpWord\Element\Table();
$myFontStyle = array('name' => 'Minion Pro', 'size' => 10, 'bold' => true);
$myParagraphStyle = array('align'=>'center', 'spaceBefore'=>50, 'spaceafter' => 50);
$table->addRow();
$table->addCell()->addText('Cell 1', $myFontStyle, $myParagraphStyle );
$table->addCell()->addText('Cell 2', $myFontStyle, $myParagraphStyle );
$table->addCell()->addText('Cell 3', $myFontStyle, $myParagraphStyle );
$my_template->setComplexBlock('table', $table);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论