- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用 PHPWord 时遇到了一些问题。总的来说,该模块相当简单易用,但有一件事我似乎做不到:为标题添加样式(或者根本不添加标题,真的)。
背景:
我的文档生成正确,我正在从数据库加载变量和文本,它完全按照我想要的方式生成(在实现了一些\n-replacement 解决方法之后),但我的标题仍然是 Arial,10pt,而不是标题风格。
这是我的代码(我在中间删除了一小块生成我的介绍,但它不包含标题):
//############################### STARTING DOCUMENT AND DEFINING STYLES ###############################
$phpWord = new \PhpOffice\PhpWord\PhpWord(); //start new document
$file = $document->titel . '.docx';
// adding the necessary font styles
$phpWord->addFontStyle('font_default', array('name'=>'HelveticaNeueLT Std Lt', 'size'=>11, 'color'=>'000000'));
$phpWord->addFontStyle('font_h1', array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000'));
$phpWord->addFontStyle('font_h2', array('name'=>'HelveticaNeueLT Std Med', 'size'=>14, 'color'=>'6D6D6D'));
$phpWord->addFontStyle('font_h3', array('name'=>'HelveticaNeueLT Std Med', 'size'=>12, 'color'=>'949494'));
//adding the necessary header/title styles
$phpWord->addTitleStyle(1, "font_h1"); //h1
$phpWord->addTitleStyle(2, "font_h2"); //h2
$phpWord->addTitleStyle(3, "font_h3"); //h3
//adding the necessary paragraph styles
$phpWord->addParagraphStyle('paragraph_default', array('spaceBefore' => 0, 'spaceAfter' => 0));
//############################### STARTING DOCUMENT AND DEFINING STYLES ###############################
//############################### INTRODUCTION ###############################
$introduction = $phpWord->addSection();
//cut out some code that adds a bunch of text here
//############################### INTRODUCTION ###############################
//############################### CHAPTERS ###############################
//prepping the textblocks (to get rid of newlines, but still have their functionality)
for ($i = 0; $i < count($documentTextblocks); $i++) //split each textblock into its paragraphs
{
$documentTextblocks[$i]->inhoud = explode("\n", $documentTextblocks[$i]->inhoud);
}
for ($i = 0; $i < count($documentChapters); $i++) //for each chapter
{
$chapter = $phpWord->addSection();
$chapter->addTitle($documentChapters[$i]->koptekst, 1); //add the chapter (h1) header
for ($x = 0; $x < count($documentTextblocks); $x++) //for each textblock
{
if ($documentTextblocks[$x]->offerte_hoofdstuk == $documentChapters[$i]->id)
{
//$block = $phpWord->addSection(); //this places every textblock on a new page, so lets not
$chapter->addTitle($documentTextblocks[$x]->koptekst, 2); //add the textblock (h2) header
for ($y = 0; $y < count($documentTextblocks[$x]->inhoud); $y++) //add all paragraphs to the chapter/textblock
{
$chapter->addText($documentTextblocks[$x]->inhoud[$y], "font_default", "paragraph_default");
}
$chapter->addTextBreak(2, null, "paragraph_default"); //add an empty line between adjacent textblocks
}
}
}
//############################### CHAPTERS ###############################
我可能只是遗漏了一些非常明显的东西,但我一直盯着自己看,我发现绝对有 0 个 PHPWord 支持提及标题。任何帮助将不胜感激。
最佳答案
--- 所有功劳归于 XATENEV ---
Xatenev 发布了答案:我错误地将预定义字体传递给了 addTitleStyle) 函数。
您应该仅传递用于创建字体的选项数组。
错误:
$phpWord->addFontStyle('font_h1', array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000'));
$phpWord->addTitleStyle(1, "font_h1"); //h1
右:
$phpWord->addTitleStyle(1, array('name'=>'HelveticaNeueLT Std Med', 'size'=>16, 'color'=>'990000')); //h1
总的来说这不是一个难题,但是如果您像我一样误解了 PHPWord 中的 addTitleStyle 和 addTitle 函数,那么希望现在更清楚一些。
关于PHPWord - 未应用标题样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24487079/
是否可以使用 PHPWord 对 Word 文档上的文本进行简单的查找和替换?据我所知,您能得到的最接近的结果就是将文本添加到一个部分,并且除了字体等之外,您无法操作现有文本。如果没有,有什么免费的东
尝试使用 PHP 库 PHPWord 创建 MS Word 文档。 你能帮我如何合并文档中的两个或多个单元格吗?或者如何在每个表格行中有不同数量的单元格? 我尝试将单元格定义为: $cell11 =
尝试使用 PHP 库 PHPWord 创建 MS Word 文档。 你能帮我如何合并文档中的两个或多个单元格吗?或者如何在每个表格行中有不同数量的单元格? 我尝试将单元格定义为: $cell11 =
我正在使用 PHPWord 0.12.0。我创建了一个表格,但似乎无法对齐单元格中的文本。这是我的特定行代码: $table->addCell(1540, array('bgColor' => 'dd
是否可以在 PHPWord 中将项目符号颜色更改为黑色以外的任何其他颜色?我尝试为列表和文本添加颜色,但似乎没有任何效果。 $phpWord->addFontStyle('myOwnStyle', a
我在使用 PHPWord 时遇到了一些问题。总的来说,该模块相当简单易用,但有一件事我似乎做不到:为标题添加样式(或者根本不添加标题,真的)。 背景: 我的文档生成正确,我正在从数据库加载变量和文本,
我在使用 PHPWord 时遇到了一些问题。总的来说,该模块相当简单易用,但有一件事我似乎做不到:为标题添加样式(或者根本不添加标题,真的)。 背景: 我的文档生成正确,我正在从数据库加载变量和文本,
我收到这个错误: Fatal error: Uncaught exception 'Exception' with message 'Could not close zip file.' in /ho
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
我有一个 docx 文件,我需要替换一些文本。这是在 codeigniter 框架内完成的;这是代码: $this->load->library('word'); $template =
我的基本 PHPWord 设置正在运行。 这是我的代码: '; $result .= 'Results: '; foreach ($
我正在尝试用 PHPWord 编写一个表格,该表格在一个单元格中具有多种文本格式,例如在 HTML 中: L1 = 999 Blah 我会这样写: $table = $se
我正在使用 PHPWord 生成报告,但我没有找到在每个页面中保留表格标题的方法。问题是我无法为单元格提供样式,并且标题和主表之间有一些空间。 我试试这个,但我认为这不是最好的解决方案: create
我目前正在使用 PHPWord 生成我的文档,但我想在文档中添加一条水平线。就像在 HMTL 中一样。在 Word 中,您可以通过在 Enter 中键入三个下划线来完成此操作,但我想在生成的文档中使用
我正在使用https://github.com/PHPOffice/PHPWord生成word文档。函数 addTOC 生成带有标题的页面索引,但由于某种原因,页码不显示。 // add table
当我使用addText()时,它与align=center(向左或向右)一起工作正常,但是当我尝试使用align=justify时脚本工作没有问题,但是当我尝试打开 .docx 文件时,它给出错误并且
如何在文本运行中添加文本分隔符或转到下一行/行?我试图在文本运行时只执行 $section->addTextBreak(2); 但它只是在文本运行后将中断添加到该部分。我也试过 $textrun->a
没有太多使用 phpWord 的经验,遇到以下错误: Fatal error: Uncaught exception 'BadMethodCallException' with message 'Ca
一切正常,但我真的尝试在 phpword 中制作一个表格,在表格内的文本下方没有空格。请帮助我形成这种情况。 这是我的代码.... $phpWord = new \PhpOffice\PhpWord\
我对 phpword 表有疑问。 我有下表,我想克隆第一个表行并替换其中的信息。到目前为止,我没有任何进展。我使用 getVariables() 方法从文档中获取所有变量并循环遍历它们。我检查了该值是
我是一名优秀的程序员,十分优秀!