作者:doit0
项目:relProjec
/**
* Read styles.xml.
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @return void
*/
public function read(PhpWord $phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$nodes = $xmlReader->getElements('w:style');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$type = $xmlReader->getAttribute('w:type', $node);
$name = $xmlReader->getAttribute('w:styleId', $node);
if (is_null($name)) {
$name = $xmlReader->getAttribute('w:val', $node, 'w:name');
}
preg_match('/Heading(\\d)/', $name, $headingMatches);
// $default = ($xmlReader->getAttribute('w:default', $node) == 1);
switch ($type) {
case 'paragraph':
$paragraphStyle = $this->readParagraphStyle($xmlReader, $node);
$fontStyle = $this->readFontStyle($xmlReader, $node);
if (!empty($headingMatches)) {
$phpWord->addTitleStyle($headingMatches[1], $fontStyle, $paragraphStyle);
} else {
if (empty($fontStyle)) {
if (is_array($paragraphStyle)) {
$phpWord->addParagraphStyle($name, $paragraphStyle);
}
} else {
$phpWord->addFontStyle($name, $fontStyle, $paragraphStyle);
}
}
break;
case 'character':
$fontStyle = $this->readFontStyle($xmlReader, $node);
if (!empty($fontStyle)) {
$phpWord->addFontStyle($name, $fontStyle);
}
break;
case 'table':
$tStyle = $this->readTableStyle($xmlReader, $node);
if (!empty($tStyle)) {
$phpWord->addTableStyle($name, $tStyle);
}
break;
}
}
}
}
作者:denisristi
项目:word-service-provide
public function generateDOC($html)
{
$objPHPWord = new PhpWord();
// Create new PHPWord object
$section = $objPHPWord->addSection();
Html::addHtml($section, $html, true);
$objWriter = IOFactory::createWriter($objPHPWord, 'Word2007');
ob_start();
$objWriter->save('php://output');
$contents = ob_get_clean();
return $contents;
}
作者:matiasvillanuev
项目:laravel5-CRUD-LOGI
/**
* Read meta.xml.
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @return void
* @todo Process property type
*/
public function read(PhpWord $phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$docProps = $phpWord->getDocInfo();
$metaNode = $xmlReader->getElement('office:meta');
// Standard properties
$properties = array('title' => 'dc:title', 'subject' => 'dc:subject', 'description' => 'dc:description', 'keywords' => 'meta:keyword', 'creator' => 'meta:initial-creator', 'lastModifiedBy' => 'dc:creator');
foreach ($properties as $property => $path) {
$method = "set{$property}";
$propertyNode = $xmlReader->getElement($path, $metaNode);
if ($propertyNode !== null && method_exists($docProps, $method)) {
$docProps->{$method}($propertyNode->nodeValue);
}
}
// Custom properties
$propertyNodes = $xmlReader->getElements('meta:user-defined', $metaNode);
foreach ($propertyNodes as $propertyNode) {
$property = $xmlReader->getAttribute('meta:name', $propertyNode);
// Set category, company, and manager property
if (in_array($property, array('Category', 'Company', 'Manager'))) {
$method = "set{$property}";
$docProps->{$method}($propertyNode->nodeValue);
// Set other custom properties
} else {
$docProps->setCustomProperty($property, $propertyNode->nodeValue);
}
}
}
作者:sadhakb
项目:resourcecontracts.or
/**
* Create word file using phpWord library
*
* @param array $text
* @param $file
* @return string
* @throws \PhpOffice\PhpWord\Exception\Exception
*/
public function createDocx(array $text, $file)
{
$file_path = public_path($file);
$phpWord = new PhpWord();
foreach ($text as $page) {
$section = $phpWord->addSection();
$page = $this->escape($page);
Html::addHtml($section, $page);
}
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($file_path);
return $file_path;
}
作者:kaantun
项目:MYK-BO
/**
* Test write styles
*/
public function testWriteStyles()
{
$phpWord = new PhpWord();
$pStyle = array('align' => 'both');
$pBase = array('basedOn' => 'Normal');
$pNew = array('basedOn' => 'Base Style', 'next' => 'Normal');
$rStyle = array('size' => 20);
$tStyle = array('bgColor' => 'FF0000', 'cellMarginTop' => 120, 'cellMarginBottom' => 120, 'cellMarginLeft' => 120, 'cellMarginRight' => 120, 'borderTopSize' => 120, 'borderBottomSize' => 120, 'borderLeftSize' => 120, 'borderRightSize' => 120, 'borderInsideHSize' => 120, 'borderInsideVSize' => 120);
$phpWord->setDefaultParagraphStyle($pStyle);
$phpWord->addParagraphStyle('Base Style', $pBase);
$phpWord->addParagraphStyle('New Style', $pNew);
$phpWord->addFontStyle('New Style', $rStyle, $pStyle);
$phpWord->addTableStyle('Table Style', $tStyle, $tStyle);
$phpWord->addTitleStyle(1, $rStyle, $pStyle);
$doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/styles.xml';
// Normal style generated?
$path = '/w:styles/w:style[@w:styleId="Normal"]/w:name';
$element = $doc->getElement($path, $file);
$this->assertEquals('Normal', $element->getAttribute('w:val'));
// Parent style referenced?
$path = '/w:styles/w:style[@w:styleId="New Style"]/w:basedOn';
$element = $doc->getElement($path, $file);
$this->assertEquals('Base Style', $element->getAttribute('w:val'));
// Next paragraph style correct?
$path = '/w:styles/w:style[@w:styleId="New Style"]/w:next';
$element = $doc->getElement($path, $file);
$this->assertEquals('Normal', $element->getAttribute('w:val'));
}
作者:HaiLeade
项目:quiz
/**
* Write footnotes
*/
public function testWriteNumbering()
{
$xmlFile = 'word/numbering.xml';
$phpWord = new PhpWord();
$phpWord->addNumberingStyle('numStyle', array('type' => 'multilevel', 'levels' => array(array('start' => 1, 'format' => 'decimal', 'restart' => 1, 'suffix' => 'space', 'text' => '%1.', 'alignment' => Jc::START, 'left' => 360, 'hanging' => 360, 'tabPos' => 360, 'font' => 'Arial', 'hint' => 'default'))));
$doc = TestHelperDOCX::getDocument($phpWord, 'Word2007');
$this->assertTrue($doc->elementExists('/w:numbering/w:abstractNum', $xmlFile));
}
作者:kaantun
项目:MYK-BO
/**
* Read content.xml
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord &$phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$nodes = $xmlReader->getElements('office:body/office:text/*');
if ($nodes->length > 0) {
$section = $phpWord->addSection();
foreach ($nodes as $node) {
// $styleName = $xmlReader->getAttribute('text:style-name', $node);
switch ($node->nodeName) {
case 'text:h':
// Heading
$depth = $xmlReader->getAttribute('text:outline-level', $node);
$section->addTitle($node->nodeValue, $depth);
break;
case 'text:p':
// Paragraph
$section->addText($node->nodeValue);
break;
case 'text:list':
// List
$listItems = $xmlReader->getElements('text:list-item/text:p', $node);
foreach ($listItems as $listItem) {
// $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
$section->addListItem($listItem->nodeValue);
}
break;
}
}
}
}
作者:kaantun
项目:MYK-BO
/**
* Save php output
*
* @todo Haven't got any method to test this
*/
public function testSavePhpOutput()
{
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Test');
$writer = new ODText($phpWord);
$writer->save('php://output');
}
作者:matiasvillanuev
项目:laravel5-CRUD-LOGI
/**
* Test compatibility
*/
public function testCompatibility()
{
$phpWord = new PhpWord();
$phpWord->getCompatibility()->setOoxmlVersion(15);
$doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/settings.xml';
$path = '/w:settings/w:compat/w:compatSetting';
$this->assertTrue($doc->elementExists($path, $file));
}
作者:matiasvillanuev
项目:laravel5-CRUD-LOGI
/**
* Loads PhpWord from file
*
* @param string $docFile
*
* @throws \Exception
*
* @return \PhpOffice\PhpWord\PhpWord
*/
public function load($docFile)
{
$phpWord = new PhpWord();
if ($this->canRead($docFile)) {
$section = $phpWord->addSection();
HTMLParser::addHtml($section, file_get_contents($docFile), true);
} else {
throw new \Exception("Cannot read {$docFile}.");
}
return $phpWord;
}
作者:kaantun
项目:MYK-BO
/**
* Test if the tabs has been created properly
*/
public function testTabsStyle()
{
$phpWord = new PhpWord();
$phpWord->addParagraphStyle('tabbed', array('tabs' => array(new Tab('left', 1440, 'dot'))));
$doc = TestHelperDOCX::getDocument($phpWord);
$file = 'word/styles.xml';
$path = '/w:styles/w:style[@w:styleId="tabbed"]/w:pPr/w:tabs/w:tab[1]';
$element = $doc->getElement($path, $file);
$this->assertEquals('left', $element->getAttribute('w:val'));
$this->assertEquals(1440, $element->getAttribute('w:pos'));
$this->assertEquals('dot', $element->getAttribute('w:leader'));
}
作者:doit0
项目:relProjec
/**
* Test construct
*/
public function testConstruct()
{
$file = __DIR__ . '/../../_files/mpdf.pdf';
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'));
$rendererName = Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/mpdf/mpdf');
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
$writer = new PDF($phpWord);
$writer->save($file);
$this->assertTrue(file_exists($file));
unlink($file);
}
作者:brunodebarro
项目:phpwor
/**
* Read numbering.xml.
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
* @return void
*/
public function read(PhpWord $phpWord)
{
$abstracts = array();
$numberings = array();
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
// Abstract numbering definition
$nodes = $xmlReader->getElements('w:abstractNum');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$abstractId = $xmlReader->getAttribute('w:abstractNumId', $node);
$abstracts[$abstractId] = array('levels' => array());
$abstract =& $abstracts[$abstractId];
$subnodes = $xmlReader->getElements('*', $node);
foreach ($subnodes as $subnode) {
switch ($subnode->nodeName) {
case 'w:multiLevelType':
$abstract['type'] = $xmlReader->getAttribute('w:val', $subnode);
break;
case 'w:lvl':
$levelId = $xmlReader->getAttribute('w:ilvl', $subnode);
$abstract['levels'][$levelId] = $this->readLevel($xmlReader, $subnode, $levelId);
break;
}
}
}
}
// Numbering instance definition
$nodes = $xmlReader->getElements('w:num');
if ($nodes->length > 0) {
foreach ($nodes as $node) {
$numId = $xmlReader->getAttribute('w:numId', $node);
$abstractId = $xmlReader->getAttribute('w:val', $node, 'w:abstractNumId');
$numberings[$numId] = $abstracts[$abstractId];
$numberings[$numId]['numId'] = $numId;
$subnodes = $xmlReader->getElements('w:lvlOverride/w:lvl', $node);
foreach ($subnodes as $subnode) {
$levelId = $xmlReader->getAttribute('w:ilvl', $subnode);
$overrides = $this->readLevel($xmlReader, $subnode, $levelId);
foreach ($overrides as $key => $value) {
$numberings[$numId]['levels'][$levelId][$key] = $value;
}
}
}
}
// Push to Style collection
foreach ($numberings as $numId => $numbering) {
$phpWord->addNumberingStyle("PHPWordList{$numId}", $numbering);
}
}
作者:productioncompiler
项目:PHPWor
/**
* Test construct
*/
public function testConstruct()
{
$file = __DIR__ . "/../../_files/tcpdf.pdf";
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Test 1');
$rendererName = Settings::PDF_RENDERER_TCPDF;
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/tecnick.com/tcpdf');
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
$writer = new PDF($phpWord);
$writer->save($file);
$this->assertTrue(file_exists($file));
unlink($file);
}
作者:kaantun
项目:MYK-BO
/**
* Test construct
*/
public function testConstruct()
{
define('DOMPDF_ENABLE_AUTOLOAD', false);
$file = __DIR__ . "/../../_files/temp.pdf";
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText('Test 1');
$rendererName = Settings::PDF_RENDERER_DOMPDF;
$rendererLibraryPath = realpath(PHPWORD_TESTS_BASE_DIR . '/../vendor/dompdf/dompdf');
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
$writer = new PDF($phpWord);
$writer->save($file);
$this->assertTrue(file_exists($file));
unlink($file);
}
作者:RobertoRier
项目:WordCounte
public function generateDoc()
{
$phpWord = new PhpWord();
$section = $phpWord->createSection();
$section->addText('Hello World!');
$file = 'HelloWorld.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $file . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
$xmlWriter = IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("prueba.docx");
}
作者:kaantun
项目:MYK-BO
/**
* Read document.xml
*
* @param \PhpOffice\PhpWord\PhpWord $phpWord
*/
public function read(PhpWord &$phpWord)
{
$xmlReader = new XMLReader();
$xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
$nodes = $xmlReader->getElements('w:body/*');
if ($nodes->length > 0) {
$section = $phpWord->addSection();
foreach ($nodes as $node) {
switch ($node->nodeName) {
case 'w:p':
// Paragraph
// Page break
// @todo <w:lastRenderedPageBreak>
if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') {
$section->addPageBreak();
// PageBreak
}
// Paragraph
$this->readParagraph($xmlReader, $node, $section, 'document');
// Section properties
if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) {
$settingsNode = $xmlReader->getElement('w:pPr/w:sectPr', $node);
if (!is_null($settingsNode)) {
$settings = $this->readSectionStyle($xmlReader, $settingsNode);
$section->setSettings($settings);
if (!is_null($settings)) {
$this->readHeaderFooter($settings, $section);
}
}
$section = $phpWord->addSection();
}
break;
case 'w:tbl':
// Table
$this->readTable($xmlReader, $node, $section, 'document');
break;
case 'w:sectPr':
// Last section
$settings = $this->readSectionStyle($xmlReader, $node);
$section->setSettings($settings);
if (!is_null($settings)) {
$this->readHeaderFooter($settings, $section);
}
break;
}
}
}
}
作者:productioncompiler
项目:PHPWor
/**
* Write footnotes
*/
public function testWriteFootnotes()
{
$phpWord = new PhpWord();
$phpWord->addParagraphStyle('pStyle', array('align' => 'left'));
$section = $phpWord->addSection();
$section->addText('Text');
$footnote1 = $section->addFootnote('pStyle');
$footnote1->addText('Footnote');
$footnote1->addTextBreak();
$footnote1->addLink('http://google.com');
$footnote2 = $section->addEndnote(array('align' => 'left'));
$footnote2->addText('Endnote');
$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertTrue($doc->elementExists("/w:document/w:body/w:p/w:r/w:footnoteReference"));
$this->assertTrue($doc->elementExists("/w:document/w:body/w:p/w:r/w:endnoteReference"));
}
作者:var23ra
项目:toword_by_PhpWor
/**
* Write footnotes
*/
public function testWriteFootnotes()
{
$phpWord = new PhpWord();
$phpWord->addParagraphStyle('pStyle', array('align' => 'left'));
$section = $phpWord->addSection();
$section->addText(htmlspecialchars('Text', ENT_COMPAT, 'UTF-8'));
$footnote1 = $section->addFootnote('pStyle');
$footnote1->addText(htmlspecialchars('Footnote', ENT_COMPAT, 'UTF-8'));
$footnote1->addTextBreak();
$footnote1->addLink('https://github.com/PHPOffice/PHPWord');
$footnote2 = $section->addEndnote(array('align' => 'left'));
$footnote2->addText(htmlspecialchars('Endnote', ENT_COMPAT, 'UTF-8'));
$doc = TestHelperDOCX::getDocument($phpWord);
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:footnoteReference'));
$this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:endnoteReference'));
}
作者:brunodebarro
项目:phpwor
/**
* Set/get minDepth and maxDepth
*/
public function testSetGetMinMaxDepth()
{
$titles = array('Heading 1' => 1, 'Heading 2' => 2, 'Heading 3' => 3, 'Heading 4' => 4);
$phpWord = new PhpWord();
foreach ($titles as $text => $depth) {
$phpWord->addTitle(new Title(htmlspecialchars($text, ENT_COMPAT, 'UTF-8'), $depth));
}
$toc = new TOC();
$toc->setPhpWord($phpWord);
$this->assertEquals(1, $toc->getMinDepth());
$this->assertEquals(9, $toc->getMaxDepth());
$toc->setMinDepth(2);
$toc->setMaxDepth(3);
$toc->getTitles();
$this->assertEquals(2, $toc->getMinDepth());
$this->assertEquals(3, $toc->getMaxDepth());
}