Exemplo n.º 1
9
 /**
  * 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;
             }
         }
     }
 }
Exemplo n.º 2
3
 /**
  * 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'));
 }
Exemplo n.º 3
0
 /**
  * Save
  */
 public function testSave()
 {
     $localImage = __DIR__ . '/../_files/images/earth.jpg';
     $remoteImage = 'http://php.net//images/logos/php-med-trans-light.gif';
     $phpWord = new PhpWord();
     $phpWord->addFontStyle('Font', array('size' => 11));
     $phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER));
     $section = $phpWord->addSection();
     $section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font', 'Paragraph');
     $section->addTextBreak();
     $section->addText(htmlspecialchars('Test 2', ENT_COMPAT, 'UTF-8'));
     $section = $phpWord->addSection();
     $textrun = $section->addTextRun();
     $textrun->addText(htmlspecialchars('Test 3', ENT_COMPAT, 'UTF-8'));
     $footnote = $textrun->addFootnote();
     $footnote->addLink('https://github.com/PHPOffice/PHPWord');
     $header = $section->addHeader();
     $header->addImage($localImage);
     $footer = $section->addFooter();
     $footer->addImage($remoteImage);
     $writer = new Word2007($phpWord);
     $file = __DIR__ . '/../_files/temp.docx';
     $writer->save($file);
     $this->assertTrue(file_exists($file));
     unlink($file);
 }
Exemplo n.º 4
0
 /**
  * Save
  */
 public function testSave()
 {
     $imageSrc = __DIR__ . "/../_files/images/PhpWord.png";
     $objectSrc = __DIR__ . "/../_files/documents/sheet.xls";
     $file = __DIR__ . "/../_files/temp.odt";
     $phpWord = new PhpWord();
     $phpWord->addFontStyle('Font', array('size' => 11));
     $phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
     $section = $phpWord->addSection();
     $section->addText('Test 1', 'Font');
     $section->addTextBreak();
     $section->addText('Test 2', null, 'Paragraph');
     $section->addLink('http://test.com');
     $section->addTitle('Test', 1);
     $section->addPageBreak();
     $section->addTable()->addRow()->addCell()->addText('Test');
     $section->addListItem('Test');
     $section->addImage($imageSrc);
     $section->addObject($objectSrc);
     $section->addTOC();
     $section = $phpWord->addSection();
     $textrun = $section->addTextRun();
     $textrun->addText('Test 3');
     $writer = new ODText($phpWord);
     $writer->save($file);
     $this->assertTrue(file_exists($file));
     unlink($file);
 }
Exemplo n.º 5
0
 /**
  * Save
  */
 public function testSave()
 {
     $imageSrc = __DIR__ . '/../_files/images/PhpWord.png';
     $objectSrc = __DIR__ . '/../_files/documents/sheet.xls';
     $file = __DIR__ . '/../_files/temp.rtf';
     $phpWord = new PhpWord();
     $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => '00FF00'));
     $phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER));
     $section = $phpWord->addSection();
     $section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font', 'Paragraph');
     $section->addTextBreak();
     $section->addText(htmlspecialchars('Test 2', ENT_COMPAT, 'UTF-8'), array('name' => 'Tahoma', 'bold' => true, 'italic' => true));
     $section->addLink('https://github.com/PHPOffice/PHPWord');
     $section->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1);
     $section->addPageBreak();
     // Rowspan
     $table = $section->addTable();
     $table->addRow()->addCell(null, array('vMerge' => 'restart'))->addText('Test');
     $table->addRow()->addCell(null, array('vMerge' => 'continue'))->addText('Test');
     // Nested table
     $cell = $section->addTable()->addRow()->addCell();
     $cell->addTable()->addRow()->addCell();
     $section->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
     $section->addImage($imageSrc);
     $section->addObject($objectSrc);
     $section->addTOC();
     $section = $phpWord->addSection();
     $textrun = $section->addTextRun();
     $textrun->addText(htmlspecialchars('Test 3', ENT_COMPAT, 'UTF-8'));
     $textrun->addTextBreak();
     $writer = new RTF($phpWord);
     $writer->save($file);
     $this->assertTrue(file_exists($file));
     @unlink($file);
 }
Exemplo n.º 6
0
 /**
  * Save
  */
 public function testSave()
 {
     $localImage = __DIR__ . '/../_files/images/earth.jpg';
     $remoteImage = 'http://php.net//images/logos/php-med-trans-light.gif';
     $phpWord = new PhpWord();
     $phpWord->addFontStyle('Font', array('size' => 11));
     $phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
     $section = $phpWord->addSection();
     $section->addText('Test 1', 'Font', 'Paragraph');
     $section->addTextBreak();
     $section->addText('Test 2');
     $section = $phpWord->addSection();
     $textrun = $section->addTextRun();
     $textrun->addText('Test 3');
     $footnote = $textrun->addFootnote();
     $footnote->addLink('http://test.com');
     $header = $section->addHeader();
     $header->addImage($localImage);
     $footer = $section->addFooter();
     $footer->addImage($remoteImage);
     $writer = new Word2007($phpWord);
     $file = __DIR__ . "/../_files/temp.docx";
     $writer->save($file);
     $this->assertTrue(file_exists($file));
     unlink($file);
 }
Exemplo n.º 7
0
 /**
  * Save
  */
 public function testSave()
 {
     $imageSrc = __DIR__ . "/../_files/images/PhpWord.png";
     $objectSrc = __DIR__ . "/../_files/documents/sheet.xls";
     $file = __DIR__ . "/../_files/temp.rtf";
     $phpWord = new PhpWord();
     $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => 'FF0000'));
     $phpWord->addParagraphStyle('Paragraph', array('align' => 'center'));
     $section = $phpWord->addSection();
     $section->addText('Test 1', 'Font', 'Paragraph');
     $section->addTextBreak();
     $section->addText('Test 2', array('name' => 'Tahoma', 'bold' => true, 'italic' => true));
     $section->addLink('http://test.com');
     $section->addTitle('Test', 1);
     $section->addPageBreak();
     $section->addTable();
     $section->addListItem('Test');
     $section->addImage($imageSrc);
     $section->addObject($objectSrc);
     $section->addTOC();
     $section = $phpWord->addSection();
     $textrun = $section->addTextRun();
     $textrun->addText('Test 3');
     $textrun->addTextBreak();
     $writer = new RTF($phpWord);
     $writer->save($file);
     $this->assertTrue(file_exists($file));
     unlink($file);
 }
Exemplo n.º 8
0
 /**
  * Save
  */
 public function testSave()
 {
     $imageSrc = __DIR__ . '/../_files/images/PhpWord.png';
     $objectSrc = __DIR__ . '/../_files/documents/sheet.xls';
     $file = __DIR__ . '/../_files/temp.odt';
     $phpWord = new PhpWord();
     $phpWord->addFontStyle('Font', array('size' => 11));
     $phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER));
     $section = $phpWord->addSection();
     $section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font');
     $section->addTextBreak();
     $section->addText(htmlspecialchars('Test 2', ENT_COMPAT, 'UTF-8'), null, 'Paragraph');
     $section->addLink('https://github.com/PHPOffice/PHPWord');
     $section->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1);
     $section->addPageBreak();
     $section->addTable()->addRow()->addCell()->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
     $section->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
     $section->addImage($imageSrc);
     $section->addObject($objectSrc);
     $section->addTOC();
     $section = $phpWord->addSection();
     $textrun = $section->addTextRun();
     $textrun->addText(htmlspecialchars('Test 3', ENT_COMPAT, 'UTF-8'));
     $writer = new ODText($phpWord);
     $writer->save($file);
     $this->assertTrue(file_exists($file));
     unlink($file);
 }
Exemplo n.º 9
0
 /**
  * Test write text break
  */
 public function testWriteTextBreak()
 {
     $fArray = array('size' => 12);
     $pArray = array('spacing' => 240);
     $fName = 'fStyle';
     $pName = 'pStyle';
     $phpWord = new PhpWord();
     $phpWord->addFontStyle($fName, $fArray);
     $phpWord->addParagraphStyle($pName, $pArray);
     $section = $phpWord->addSection();
     $section->addTextBreak();
     $section->addTextBreak(1, $fArray, $pArray);
     $section->addTextBreak(1, $fName, $pName);
     $doc = TestHelperDOCX::getDocument($phpWord);
     $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:rPr/w:rStyle');
     $this->assertEquals($fName, $element->getAttribute('w:val'));
     $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:pStyle');
     $this->assertEquals($pName, $element->getAttribute('w:val'));
 }
Exemplo n.º 10
0
 /**
  * Test no paragraph style
  */
 public function testWriteNoStyle()
 {
     $phpWord = new PhpWord();
     $phpWord->addFontStyle('Font', array('size' => 11));
     $doc = TestHelperDOCX::getDocument($phpWord, 'ODText');
     $element = "/office:document-content/office:automatic-styles/style:style";
     $this->assertTrue($doc->elementExists($element, 'content.xml'));
 }
Exemplo n.º 11
0
 public function export($path)
 {
     $phpWord = new PhpWord();
     $phpWord->setDefaultFontName('Times New Roman');
     $phpWord->setDefaultFontSize(12);
     $phpWord->addTitleStyle(1, ['size' => 26, 'bold' => true], []);
     $phpWord->addTitleStyle(2, ['size' => 22, 'bold' => true], []);
     $phpWord->addTitleStyle(3, ['size' => 18, 'bold' => true], []);
     $phpWord->addTitleStyle(4, ['size' => 16, 'bold' => true], []);
     $phpWord->addFontStyle('f_timeAndPlace', ['size' => 16, 'bold' => false]);
     $phpWord->addFontStyle('f_bold', ['bold' => true]);
     $phpWord->addFontStyle('f_italic', ['italic' => true]);
     $phpWord->addFontStyle('f_label', []);
     $phpWord->addFontStyle('f_chairName', []);
     $phpWord->addFontStyle('f_chairOrganisation', []);
     $phpWord->addFontStyle('f_presentationAuthor', ['bold' => true]);
     $phpWord->addFontStyle('f_presentationOrganisation', []);
     $phpWord->addFontStyle('f_presentationName', ['italic' => true]);
     $phpWord->addFontStyle('f_contributionPaperLabel', []);
     $phpWord->addParagraphStyle('p_contributionPaperLabel', ['lineHeight' => 1]);
     $phpWord->addParagraphStyle('p_presentation', ['lineHeight' => 1]);
     $phpWord->addParagraphStyle('p_presentationContributionPaper', ['lineHeight' => 1, 'basedOn' => 'presentation']);
     $phpWord->addParagraphStyle('p_chairs', []);
     $phpWord->addParagraphStyle('p_timeAndPlace', []);
     $section = $phpWord->addSection();
     $section->addTitle($this->text('Research Network / Research Stream Sessions'));
     foreach ($this->getTypes() as $type) {
         if (!$this->isExportType($type)) {
             continue;
         }
         $section->addTitle($this->text(sprintf('%s - %s', $type, static::$typeNames[$type])), 2);
         foreach ($this->getSessions($type) as $session) {
             $start = new \DateTime($session['start']);
             $end = new \DateTime($session['end']);
             $timeAndPlace = sprintf('%s - %s / %s / %s', $start->format('H:i'), $end->format('H:i'), $start->format('jS l'), $session['room']);
             $section->addText($timeAndPlace, 'f_timeAndPlace', 'p_timeAndPlace');
             $section->addTitle($this->text(sprintf('%s / %s', $session['short'], $session['title'])), 3);
             $chairs = $this->getChairs($session);
             if ($chairs) {
                 $textRun = $section->addTextRun('p_chairs');
                 $text = count($chairs) == 1 ? 'Chair: ' : 'Chairs: ';
                 $textRun->addText($this->text($text), 'f_label', null);
                 foreach ($chairs as $chair) {
                     $textRun->addText($this->text(sprintf('%s ', $chair['name'])), 'f_chairName', null);
                     $textRun->addText($this->text(sprintf('(%s)', $chair['organisation'])), 'f_chairOrganisation', null);
                 }
             }
             $presentations = $this->getPresentations($session['id']);
             $contributingPapers = array_filter($presentations, function ($p) {
                 return $p['acceptance'] === 'Contributing Paper';
             });
             $otherPresentations = array_filter($presentations, function ($p) {
                 return $p['acceptance'] !== 'Contributing Paper';
             });
             foreach ($otherPresentations as $presentation) {
                 $textRun = $section->addTextRun('p_presentation');
                 foreach ($this->getAuthors($presentation) as $author) {
                     $textRun->addText($this->text(sprintf('%s ', $author['name'])), 'f_presentationAuthor');
                     $textRun->addText($this->text(sprintf('(%s)', $author['organisation'])), 'f_presentationOrganisation');
                     $textRun->addText(', ');
                 }
                 $textRun->addText($this->text($presentation['title']), 'f_presentationName');
             }
             if ($contributingPapers) {
                 $section->addText('Contributed papers', 'f_contributionPaperLabel', 'p_contributionPaperLabel');
                 foreach ($contributingPapers as $presentation) {
                     $textRun = $section->addTextRun('p_presentationContributionPaper');
                     foreach ($this->getAuthors($presentation) as $author) {
                         $textRun->addText($this->text(sprintf('%s ', $author['name'])), 'f_presentationAuthor');
                         $textRun->addText($this->text(sprintf('(%s)', $author['organisation'])), 'f_presentationOrganisation');
                         $textRun->addText(', ');
                     }
                     $textRun->addText($this->text($presentation['title']), 'f_presentationName');
                 }
             }
         }
         $section->addPageBreak();
     }
     $phpWord->save($path);
 }
Exemplo n.º 12
0
 /**
  * Set automatic styles
  */
 private function getAutomaticStyles(PhpWord $phpWord)
 {
     $sections = $phpWord->getSections();
     $sectionCount = count($sections);
     if ($sectionCount > 0) {
         $paragraphStyleCount = 0;
         $fontStyleCount = 0;
         foreach ($sections as $section) {
             $elements = $section->getElements();
             foreach ($elements as $element) {
                 if ($element instanceof Text) {
                     $fontStyle = $element->getFontStyle();
                     $paragraphStyle = $element->getParagraphStyle();
                     // Font
                     if ($fontStyle instanceof Font) {
                         $fontStyleCount++;
                         $arrStyle = array('color' => $fontStyle->getColor(), 'name' => $fontStyle->getName());
                         $phpWord->addFontStyle('T' . $fontStyleCount, $arrStyle);
                         $element->setFontStyle('T' . $fontStyleCount);
                         // Paragraph
                     } elseif ($paragraphStyle instanceof Paragraph) {
                         $paragraphStyleCount++;
                         $phpWord->addParagraphStyle('P' . $paragraphStyleCount, array());
                         $element->setParagraphStyle('P' . $paragraphStyleCount);
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 13
0
 /**
  * covers ::_writeCheckbox
  */
 public function testWriteCheckbox()
 {
     $rStyle = 'rStyle';
     $pStyle = 'pStyle';
     $phpWord = new PhpWord();
     $phpWord->addFontStyle($rStyle, array('bold' => true));
     $phpWord->addParagraphStyle($pStyle, array('hanging' => 120, 'indent' => 120));
     $section = $phpWord->addSection();
     $section->addCheckbox('Check1', 'Test', $rStyle, $pStyle);
     $doc = TestHelperDOCX::getDocument($phpWord);
     $element = '/w:document/w:body/w:p/w:r/w:fldChar/w:ffData/w:name';
     $this->assertEquals('Check1', $doc->getElementAttribute($element, 'w:val'));
 }
Exemplo n.º 14
-1
 /**
  * Save
  */
 public function testSave()
 {
     $localImage = __DIR__ . '/../_files/images/PhpWord.png';
     $archiveImage = 'zip://' . __DIR__ . '/../_files/documents/reader.docx#word/media/image1.jpeg';
     $gdImage = 'http://php.net/images/logos/php-med-trans-light.gif';
     $objectSrc = __DIR__ . '/../_files/documents/sheet.xls';
     $file = __DIR__ . '/../_files/temp.html';
     $phpWord = new PhpWord();
     $docProps = $phpWord->getDocInfo();
     $docProps->setTitle(htmlspecialchars('HTML Test', ENT_COMPAT, 'UTF-8'));
     $phpWord->addTitleStyle(1, array('bold' => true));
     $phpWord->addFontStyle('Font', array('name' => 'Verdana', 'size' => 11, 'color' => 'FF0000', 'fgColor' => 'FF0000'));
     $phpWord->addParagraphStyle('Paragraph', array('align' => 'center', 'spaceAfter' => 20, 'spaceBefore' => 20));
     $section = $phpWord->addSection();
     $section->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), 'Font', 'Paragraph');
     $section->addTextBreak();
     $section->addText(htmlspecialchars('Test 2', ENT_COMPAT, 'UTF-8'), array('name' => 'Tahoma', 'bold' => true, 'italic' => true, 'subscript' => true));
     $section->addLink('https://github.com/PHPOffice/PHPWord');
     $section->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1);
     $section->addPageBreak();
     $section->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
     $section->addImage($localImage);
     $section->addImage($archiveImage);
     $section->addImage($gdImage);
     $section->addObject($objectSrc);
     $section->addFootnote();
     $section->addEndnote();
     $section = $phpWord->addSection();
     $textrun = $section->addTextRun(array('align' => 'center'));
     $textrun->addText(htmlspecialchars('Test 3', ENT_COMPAT, 'UTF-8'));
     $textrun->addTextBreak();
     $textrun = $section->addTextRun('Paragraph');
     $textrun->addLink('https://github.com/PHPOffice/PHPWord');
     $textrun->addImage($localImage);
     $textrun->addFootnote()->addText(htmlspecialchars('Footnote', ENT_COMPAT, 'UTF-8'));
     $textrun->addEndnote()->addText(htmlspecialchars('Endnote', ENT_COMPAT, 'UTF-8'));
     $section = $phpWord->addSection();
     $table = $section->addTable();
     $cell = $table->addRow()->addCell();
     $cell->addText(htmlspecialchars('Test 1', ENT_COMPAT, 'UTF-8'), array('superscript' => true, 'underline' => 'dash', 'strikethrough' => true));
     $cell->addTextRun();
     $cell->addLink('https://github.com/PHPOffice/PHPWord');
     $cell->addTextBreak();
     $cell->addListItem(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'));
     $cell->addImage($localImage);
     $cell->addObject($objectSrc);
     $cell->addFootnote();
     $cell->addEndnote();
     $cell = $table->addRow()->addCell();
     $writer = new HTML($phpWord);
     $writer->save($file);
     $this->assertTrue(file_exists($file));
     unlink($file);
 }