关于PhpWord类文件的更多问题,示例对我不起作用。我正在尝试使用它,即使给出的样本也很困难。在这种情况下,我试图在页面上包含一个页脚,页面编号,然后还尝试拉入外部文件的内容以显示在文档中。
test.txt文件只包含这行文字:
这是一个测试文本文件,包含在phpword生成的文档中。
不会显示在页面上。这是我的代码:
<?php
//testing the phpword functionality to get it to output and format correctly
require_once 'includes/vendor/phpoffice/phpword/bootstrap.php';
// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/* Note: any element you append to a document must reside inside of a Section. */
// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
'"Learn from yesterday, live for today, hope for tomorrow. '
. 'The important thing is not to stop questioning." '
. '(Albert Einstein)'
);
/*
* Note: it's possible to customize font style of the Text element you add in three ways:
* - inline;
* - using named font style (new font style object will be implicitly created);
* - using explicitly created font style object.
*/
// Adding Text element with font customized inline...
$section->addText(
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)',
array('name' => 'Tahoma', 'size' => 10)
);
// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
$fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)',
$fontStyleName
);
// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);
$footer = $section->addFooter();
$footer->addPreserveText('Page {PAGE} of {NUMPAGES}.', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
$section->addPageBreak();
//get an external text file to display
$filename = "test.txt";
$textfile = \PhpOffice\PhpWord\IOFactory::load($filename);
$section->addText($textfile);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'RTF');
$objWriter->save('testdoc.rtf');
?>
感谢。我真的很想让这个工作或找到另一个更好的开源解决方案。
答案 0 :(得分:0)
而不是
hi.html
试
//get an external text file to display
$filename = "test.txt";
$textfile = \PhpOffice\PhpWord\IOFactory::load($filename);
$section->addText($textfile);