Example #1
11
 /**
  * Test unit conversion functions with various numbers
  */
 public function testAddHtml()
 {
     $content = '';
     // Default
     $section = new Section(1);
     $this->assertCount(0, $section->getElements());
     // Heading
     $styles = array('strong', 'em', 'sup', 'sub');
     for ($level = 1; $level <= 6; $level++) {
         $content .= "<h{$level}>Heading {$level}</h{$level}>";
     }
     // Styles
     $content .= '<p style="text-decoration: underline; text-decoration: line-through; ' . 'text-align: center; color: #999; background-color: #000;">';
     foreach ($styles as $style) {
         $content .= "<{$style}>{$style}</{$style}>";
     }
     $content .= '</p>';
     // Add HTML
     Html::addHtml($section, $content);
     $this->assertCount(7, $section->getElements());
     // Other parts
     $section = new Section(1);
     $content = '';
     $content .= '<table><tr><th>Header</th><td>Content</td></tr></table>';
     $content .= '<ul><li>Bullet</li><ul><li>Bullet</li></ul></ul>';
     $content .= '<ol><li>Bullet</li></ol>';
     $content .= "'Single Quoted Text'";
     $content .= '"Double Quoted Text"';
     $content .= '& Ampersand';
     $content .= '&lt;&gt;&ldquo;&lsquo;&rsquo;&laquo;&raquo;&lsaquo;&rsaquo;';
     $content .= '&amp;&bull;&deg;&hellip;&trade;&copy;&reg;&mdash;';
     $content .= '&ndash;&nbsp;&emsp;&ensp;&sup2;&sup3;&frac14;&frac12;&frac34;';
     Html::addHtml($section, $content);
 }
Example #2
2
 /**
  * Test unit conversion functions with various numbers
  */
 public function testAddHtml()
 {
     $content = '';
     // Default
     $section = new Section(1);
     $this->assertEquals(0, $section->countElements());
     // Heading
     $styles = array('strong', 'em', 'sup', 'sub');
     for ($level = 1; $level <= 6; $level++) {
         $content .= "<h{$level}>Heading {$level}</h{$level}>";
     }
     // Styles
     $content .= '<p style="text-decoration: underline; text-decoration: line-through; ' . 'text-align: center; color: #999; background-color: #000;">';
     foreach ($styles as $style) {
         $content .= "<{$style}>{$style}</{$style}>";
     }
     $content .= '</p>';
     // Add HTML
     Html::addHtml($section, $content);
     $this->assertEquals(7, $section->countElements());
     // Other parts
     $section = new Section(1);
     $content = '';
     $content .= '<table><tr><th>Header</th><td>Content</td></tr></table>';
     $content .= '<ul><li>Bullet</li><ul><li>Bullet</li></ul></ul>';
     $content .= '<ol><li>Bullet</li></ol>';
     Html::addHtml($section, $content);
 }
Example #3
0
 /**
  * Add header exception
  *
  * @expectedException Exception
  * @expectedExceptionMesssage Invalid header/footer type.
  */
 public function testAddHeaderException()
 {
     $object = new Section(1);
     $header = $object->addHeader('ODD');
 }
 /**
  * Add header/footer content.
  *
  * @param \PhpOffice\PhpWord\Element\Section &$section
  * @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
  * @param string $elmType header|footer
  * @param integer &$rId
  * @return void
  */
 private function addHeaderFooterContent(Section &$section, ZipArchive $zip, $elmType, &$rId)
 {
     $getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
     $elmCount = ($section->getSectionId() - 1) * 3;
     $elements = $section->{$getFunction}();
     foreach ($elements as &$element) {
         /** @var \PhpOffice\PhpWord\Element\AbstractElement $element Type hint */
         $elmCount++;
         $element->setRelationId(++$rId);
         $elmFile = "{$elmType}{$elmCount}.xml";
         // e.g. footer1.xml
         $this->contentTypes['override']["/word/{$elmFile}"] = $elmType;
         $this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
         /** @var \PhpOffice\PhpWord\Writer\Word2007\Part\AbstractPart $writerPart Type hint */
         $writerPart = $this->getWriterPart($elmType)->setElement($element);
         $zip->addFromString("word/{$elmFile}", $writerPart->write());
     }
 }
Example #5
0
 /**
  * Create new section
  *
  * @param array $settings
  * @return \PhpOffice\PhpWord\Element\Section
  */
 public function addSection($settings = null)
 {
     $section = new Section(count($this->sections) + 1, $settings);
     $section->setPhpWord($this);
     $this->sections[] = $section;
     return $section;
 }
Example #6
0
 /**
  * Read w:sectPr node
  *
  * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader
  * @param \DOMElement $node
  * @param \PhpOffice\PhpWord\Element\Section $section
  */
 private function readWSectPrNode(XMLReader $xmlReader, \DOMElement $node, Section &$section)
 {
     $settings = $this->readSectionStyle($xmlReader, $node);
     $section->setSettings($settings);
     $this->readHeaderFooter($settings, $section);
 }
Example #7
0
 /**
  * Read paragraph.
  *
  * @param array $directives
  * @return void
  */
 private function readParagraph($directives)
 {
     list($property, $value) = $directives;
     $this->textrun = $this->section->addTextRun();
     $this->flags[$property] = $value;
 }
Example #8
0
 /**
  * Add header/footer content
  *
  * @param mixed $objZip
  * @param string $elmType
  * @param integer $rId
  */
 private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$rId)
 {
     $getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
     $writeFunction = $elmType == 'header' ? 'writeHeader' : 'writeFooter';
     $elmCount = ($section->getSectionId() - 1) * 3;
     $elmObjects = $section->{$getFunction}();
     foreach ($elmObjects as &$elmObject) {
         $elmCount++;
         $elmObject->setRelationId(++$rId);
         $elmFile = "{$elmType}{$elmCount}.xml";
         $objZip->addFromString("word/{$elmFile}", $this->getWriterPart($elmType)->{$writeFunction}($elmObject));
         $this->cTypes['override']["/word/{$elmFile}"] = $elmType;
         $this->docRels[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
     }
 }
 /**
  * Write end section.
  *
  * @param \PhpOffice\Common\XMLWriter $xmlWriter
  * @param \PhpOffice\PhpWord\Element\Section $section
  * @return void
  */
 private function writeSectionSettings(XMLWriter $xmlWriter, Section $section)
 {
     $xmlWriter->startElement('w:sectPr');
     // Header reference
     foreach ($section->getHeaders() as $header) {
         $rId = $header->getRelationId();
         $xmlWriter->startElement('w:headerReference');
         $xmlWriter->writeAttribute('w:type', $header->getType());
         $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
         $xmlWriter->endElement();
     }
     // Footer reference
     foreach ($section->getFooters() as $footer) {
         $rId = $footer->getRelationId();
         $xmlWriter->startElement('w:footerReference');
         $xmlWriter->writeAttribute('w:type', $footer->getType());
         $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
         $xmlWriter->endElement();
     }
     // Different first page
     if ($section->hasDifferentFirstPage()) {
         $xmlWriter->startElement('w:titlePg');
         $xmlWriter->endElement();
     }
     // Section settings
     $styleWriter = new SectionStyleWriter($xmlWriter, $section->getStyle());
     $styleWriter->write();
     $xmlWriter->endElement();
     // w:sectPr
 }