예제 #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;
             }
         }
     }
 }
예제 #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'));
 }
예제 #3
0
 /**
  * covers ::_writeTitle
  */
 public function testWriteTitle()
 {
     $phpWord = new PhpWord();
     $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
     $phpWord->addSection()->addTitle(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8'), 1);
     $doc = TestHelperDOCX::getDocument($phpWord);
     $element = '/w:document/w:body/w:p/w:pPr/w:pStyle';
     $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
 }
예제 #4
0
 /**
  * Test add title style
  */
 public function testAddTitleStyle()
 {
     $phpWord = new PhpWord();
     $titleLevel = 1;
     $titleName = "Heading_{$titleLevel}";
     $phpWord->addTitleStyle($titleLevel, array());
     $this->assertInstanceOf('PhpOffice\\PhpWord\\Style\\Font', Style::getStyle($titleName));
 }
예제 #5
0
파일: Book.php 프로젝트: honzatrtik/esa2015
 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);
 }
예제 #6
0
 /**
  * covers ::_writeTitle
  */
 public function testWriteTitle()
 {
     $phpWord = new PhpWord();
     $phpWord->addTitleStyle(1, array('bold' => true), array('spaceAfter' => 240));
     $phpWord->addSection()->addTitle('Test', 1);
     $doc = TestHelperDOCX::getDocument($phpWord);
     $element = "/w:document/w:body/w:p/w:pPr/w:pStyle";
     $this->assertEquals('Heading1', $doc->getElementAttribute($element, 'w:val'));
     $element = "/w:document/w:body/w:p/w:r/w:fldChar";
     $this->assertEquals('end', $doc->getElementAttribute($element, 'w:fldCharType'));
 }
 /**
  * @param $fakeDir
  *
  * @return string
  *
  * @throws PhpOffice\PhpWord\Exception\Exception
  */
 protected function createWordFile($fakeDir)
 {
     $randomFont = $this->randomFonts[array_rand($this->randomFonts)];
     $faker = Faker\Factory::create('fr_FR');
     $phpWord = new PhpOffice\PhpWord\PhpWord();
     $phpWord->addTitleStyle(1, array('name' => $randomFont, 'size' => 26));
     $phpWord->addTitleStyle(2, array('name' => $randomFont, 'size' => 16));
     $section = $phpWord->addSection();
     $section->addTitle($faker->sentence(), 1);
     $section->addTextBreak();
     for ($i = 0; $i <= mt_rand(10, 30); $i++) {
         $section->addTitle($faker->sentence(), 2);
         $section->addTextBreak();
         $section->addText(htmlspecialchars($faker->paragraph(mt_rand(30, 50))), array('name' => $randomFont, 'size' => 11));
         $section->addText("This is a fake Microsoft Word file.");
         $section->addTextBreak();
     }
     $objWriter = PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
     $result = sprintf("%s/%s.%s", $fakeDir, $faker->slug(3), 'docx');
     $objWriter->save($result);
     return $result;
 }
예제 #8
-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);
 }