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; }
/** * 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; }
/** * 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; } } } }
/** * 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; } } } }
/** * 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'); }
/** * 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; }
/** * 生成简历文件字符串 * @param $sp_id 快照主键 * @param string $type 类型 doc/doc_no_contact(隐藏了联系方式) */ public function getDocFileStr($id, $type = 'doc_no_contact') { $sp_model = new ResumeSnapshot(); $sp_info = $sp_model->getResumeSnapshotInfoById($id); //根据简历快照生成word $phpWord = new PhpWord(); // New portrait section $section = $phpWord->addSection(); // Add header for all other pages //todo logo图片需传到线上 $subsequent = $section->addHeader(); $subsequent->addText(htmlspecialchars('51CTO高招-中高端IT人才的招聘平台')); $subsequent->addImage('http://job.51cto.com/pic/logo_s.jpg', array('width' => 80, 'height' => 80, 'align' => 'right')); //// $section = $phpWord->addSection(); // $html = '<h1>Adding element via HTML</h1>'; // $html .= '<p>Some well formed HTML snippet needs to be used</p>'; // $html .= '<p>With for example <strong>some<sup>1</sup> <em>inline</em> formatting</strong><sub>1</sub></p>'; // $html .= '<p>Unordered (bulleted) list:</p>'; // $html .= '<ul><li>Item 1</li><li>Item 2</li><ul><li>Item 2.1</li><li>Item 2.1</li></ul></ul>'; // $html .= '<p>Ordered (numbered) list:</p>'; // $html .= '<ol><li>Item 1</li><li>Item 2</li></ol>'; // // \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html); $section = $phpWord->addSection(); $header = array('size' => 16, 'bold' => true); //1.Use EastAisa FontStyle $section->addText(htmlspecialchars('邵燕'), array('name' => '微软雅黑', 'size' => '二号', 'color' => '1B2232')); $section->addText(htmlspecialchars('邵燕'), array('name' => '微软雅黑', 'size' => '二号', 'color' => '1B2232')); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $rand = time() . rand(10000, 99999); $file = WEB_ROOT . "/runtime/{$rand}.docx"; //临时文件 $file = WEB_ROOT . "/runtime/test.docx"; //临时文件 $objWriter->save($file, 'Word2007', true); // $file_str = file_get_contents($file); // unlink($file);//删除文件 // // return $file_str; }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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')); }
/** * 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")); }
/** * Read w:p node * * @param \PhpOffice\PhpWord\Shared\XMLReader $xmlReader * @param \DOMElement $node * @param \PhpOffice\PhpWord\Element\Section $section * * @todo <w:lastRenderedPageBreak> */ private function readWPNode(XMLReader $xmlReader, \DOMElement $node, Section &$section) { // Page break if ($xmlReader->getAttribute('w:type', $node, 'w:r/w:br') == 'page') { $section->addPageBreak(); // PageBreak } // Paragraph $this->readParagraph($xmlReader, $node, $section); // Section properties if ($xmlReader->elementExists('w:pPr/w:sectPr', $node)) { $sectPrNode = $xmlReader->getElement('w:pPr/w:sectPr', $node); if ($sectPrNode !== null) { $this->readWSectPrNode($xmlReader, $sectPrNode, $section); } $section = $this->phpWord->addSection(); } }
/** * Test write content */ public function testWriteContent() { $imageSrc = __DIR__ . '/../../../_files/images/PhpWord.png'; $objectSrc = __DIR__ . '/../../../_files/documents/sheet.xls'; $expected = 'Expected'; $phpWord = new PhpWord(); $docProps = $phpWord->getDocInfo(); $docProps->setCustomProperty('Company', 'PHPWord'); $phpWord->setDefaultFontName('Verdana'); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('alignment' => Jc::CENTER)); $phpWord->addTableStyle('tblStyle', array('width' => 100)); $section = $phpWord->addSection(array('colsNum' => 2)); $section->addText(htmlspecialchars($expected, ENT_COMPAT, 'UTF-8')); $section->addText(htmlspecialchars('Test font style', ENT_COMPAT, 'UTF-8'), 'Font'); $section->addText(htmlspecialchars('Test paragraph style', ENT_COMPAT, 'UTF-8'), null, 'Paragraph'); $section->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8')); $section->addTitle(htmlspecialchars('Test title', ENT_COMPAT, 'UTF-8'), 1); $section->addTextBreak(); $section->addPageBreak(); $section->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8')); $section->addImage($imageSrc, array('width' => 50)); $section->addObject($objectSrc); $section->addTOC(); $textrun = $section->addTextRun(); $textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8')); $table = $section->addTable(array('width' => 50)); $cell = $table->addRow()->addCell(); $cell = $table->addRow()->addCell(); $cell->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8')); $cell->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8')); $cell->addTextBreak(); $cell->addListItem(htmlspecialchars('Test list item', ENT_COMPAT, 'UTF-8')); $cell->addImage($imageSrc); $cell->addObject($objectSrc); $textrun = $cell->addTextRun(); $textrun->addText(htmlspecialchars('Test text run', ENT_COMPAT, 'UTF-8')); $footer = $section->addFooter(); $footer->addPreserveText(htmlspecialchars('{PAGE}', ENT_COMPAT, 'UTF-8')); $table = $section->addTable('tblStyle')->addRow()->addCell(); $doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); $element = '/office:document-content/office:body/office:text/text:section/text:p'; $this->assertEquals($expected, $doc->getElement($element, 'content.xml')->nodeValue); }
/** * Test write content */ public function testWriteContent() { $imageSrc = __DIR__ . "/../../../_files/images/PhpWord.png"; $objectSrc = __DIR__ . "/../../../_files/documents/sheet.xls"; $expected = 'Expected'; $phpWord = new PhpWord(); $docProps = $phpWord->getDocumentProperties(); $docProps->setCustomProperty('Company', 'PHPWord'); $phpWord->setDefaultFontName('Verdana'); $phpWord->addFontStyle('Font', array('size' => 11)); $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); $phpWord->addTableStyle('tblStyle', array('width' => 100)); $section = $phpWord->addSection(array('colsNum' => 2)); $section->addText($expected); $section->addText('Test font style', 'Font'); $section->addText('Test paragraph style', null, 'Paragraph'); $section->addLink('http://test.com', 'Test link'); $section->addTitle('Test title', 1); $section->addTextBreak(); $section->addPageBreak(); $section->addListItem('Test list item'); $section->addImage($imageSrc, array('width' => 50)); $section->addObject($objectSrc); $section->addTOC(); $textrun = $section->addTextRun(); $textrun->addText('Test text run'); $table = $section->addTable(array('width' => 50)); $cell = $table->addRow()->addCell(); $cell = $table->addRow()->addCell(); $cell->addText('Test'); $cell->addLink('http://test.com', 'Test link'); $cell->addTextBreak(); $cell->addListItem('Test list item'); $cell->addImage($imageSrc); $cell->addObject($objectSrc); $textrun = $cell->addTextRun(); $textrun->addText('Test text run'); $footer = $section->addFooter(); $footer->addPreserveText('{PAGE}'); $table = $section->addTable('tblStyle')->addRow()->addCell(); $doc = TestHelperDOCX::getDocument($phpWord, 'ODText'); $element = "/office:document-content/office:body/office:text/text:section/text:p"; $this->assertEquals($expected, $doc->getElement($element, 'content.xml')->nodeValue); }
/** * Test set line height */ public function testLineHeight() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); // Test style array $text = $section->addText(htmlspecialchars('This is a test', ENT_COMPAT, 'UTF-8'), array('line-height' => 2.0)); $doc = TestHelperDOCX::getDocument($phpWord); $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing'); $lineHeight = $element->getAttribute('w:line'); $lineRule = $element->getAttribute('w:lineRule'); $this->assertEquals(480, $lineHeight); $this->assertEquals('auto', $lineRule); // Test setter $text->getFontStyle()->setLineHeight(3.0); $doc = TestHelperDOCX::getDocument($phpWord); $element = $doc->getElement('/w:document/w:body/w:p/w:pPr/w:spacing'); $lineHeight = $element->getAttribute('w:line'); $lineRule = $element->getAttribute('w:lineRule'); $this->assertEquals(720, $lineHeight); $this->assertEquals('auto', $lineRule); }
public function prepare() { $file = path('tmp') . sha1(microtime()); $lines = $this->getData(); $phpWord = new PhpWord(); $section = $phpWord->addSection(['orientation' => 'landscape']); /** * Make header */ $table = $section->addTable(['width' => 100 * 50]); $i = 0; $j = 0; $table->addRow(); foreach ($lines[0] as $key => $val) { $table->addCell(1750)->addText($key); $j++; } /** * Make data */ foreach ($lines as $line) { $i++; $j = 0; $table->addRow(); foreach ($line as $val) { $table->addCell(1750)->addText($val); $j++; } } /** * Save file. */ $objWriter = IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save($file); /** * Implement strategy. */ $this->setFileContent(file_get_contents($file)); unlink($file); }
/** * Render Word object * * @return PhpWord */ protected function renderWord() { //Init $this->init(true); //Build table $this->build(); //PHPWord instance $word = new PhpWord(); $section = $word->addSection(); $word->addTableStyle('std-table', ['borderColor' => '000000', 'borderSize' => 2, 'cellMargin' => 10], ['bgColor' => 'CCCCCC', 'tblHeader' => true]); $font = ['name' => 'Calibri', 'size' => 10]; $margin = 3000; //Title $section->addText($this->title, ['name' => 'Calibri', 'size' => 14, 'bold' => true]); $section->addTextBreak(); //Create table $table = $section->addTable('std-table'); $width = round(($section->getStyle()->getPageSizeW() - $margin) / count($this->columns)); //Header $table->addRow(); foreach (array_except($this->headLinks(), [':actions']) as $caption) { $table->addCell($width)->addText(htmlspecialchars(trim(strip_tags($caption))), $font + ['bold' => true]); } //Body foreach ($this->rendered as $row) { $table->addRow(); foreach ($row->getCells() as $cell) { if ($cell->getField() != ':actions') { $table->addCell($width)->addText(htmlspecialchars(strip_tags($cell->getCurrentValue())), $font); } } } return $word; }
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); }
/** * Test save */ public function testSave() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $section->addText('Hello world!'); $this->assertTrue($phpWord->save('test.docx', 'Word2007', true)); }
/** * Parse RTF content * * - Marks controlling characters `{`, `}`, and `\` * - Removes line endings * - Builds control words and control symbols * - Pushes every other character into the text queue * * @param \PhpOffice\PhpWord\PhpWord $phpWord * @return void * @todo Use `fread` stream for scalability */ public function read(PhpWord $phpWord) { $markers = array(123 => 'markOpening', 125 => 'markClosing', 92 => 'markBackslash', 10 => 'markNewline', 13 => 'markNewline'); $this->phpWord = $phpWord; $this->section = $phpWord->addSection(); $this->textrun = $this->section->addTextRun(); $this->length = strlen($this->rtf); $this->flags['paragraph'] = true; // Set paragraph flag from the beginning // Walk each characters while ($this->offset < $this->length) { $char = $this->rtf[$this->offset]; $ascii = ord($char); if (isset($markers[$ascii])) { // Marker found: {, }, \, LF, or CR $markerFunction = $markers[$ascii]; $this->{$markerFunction}(); } else { if (false === $this->isControl) { // Non control word: Push character $this->pushText($char); } else { if (preg_match("/^[a-zA-Z0-9-]?\$/", $char)) { // No delimiter: Buffer control $this->control .= $char; $this->isFirst = false; } else { // Delimiter found: Parse buffered control if ($this->isFirst) { $this->isFirst = false; } else { if (' ' == $char) { // Discard space as a control word delimiter $this->flushControl(true); } } } } } $this->offset++; } $this->flushText(); }
/** * Test write gutter and line numbering */ public function testWriteGutterAndLineNumbering() { $pageMarginPath = '/w:document/w:body/w:sectPr/w:pgMar'; $lineNumberingPath = '/w:document/w:body/w:sectPr/w:lnNumType'; $phpWord = new PhpWord(); $phpWord->addSection(array('gutter' => 240, 'lineNumbering' => array())); $doc = TestHelperDOCX::getDocument($phpWord); $this->assertEquals(240, $doc->getElement($pageMarginPath)->getAttribute('w:gutter')); $this->assertTrue($doc->elementExists($lineNumberingPath)); }
/** * Save * * @todo Haven't got any method to test this */ public function testSavePhpOutput() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $section->addText(htmlspecialchars('Test', ENT_COMPAT, 'UTF-8')); $writer = new RTF($phpWord); $writer->save('php://output'); }
/** * Set/get use disk caching */ public function testSetGetUseDiskCaching() { $phpWord = new PhpWord(); $phpWord->addSection(); $object = new Word2007($phpWord); $object->setUseDiskCaching(true, PHPWORD_TESTS_BASE_DIR); $writer = new Word2007($phpWord); $writer->save('php://output'); $this->assertTrue($object->isUseDiskCaching()); }
/** * @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; }
private function generatePhpWord() { foreach ($this->arraySections as $itmSection) { $oSection = $this->phpWord->addSection(); $oSection->setSettings($itmSection->styleSection); $sHYPERLINK = ''; foreach ($this->arrayParagraphs as $itmParagraph) { $textPara = $itmParagraph; foreach ($this->arrayCharacters as $oCharacters) { $subText = substr($textPara, $oCharacters->pos_start, $oCharacters->pos_len); $subText = str_replace(chr(13), PHP_EOL, $subText); $arrayText = explode(PHP_EOL, $subText); if (end($arrayText) == '') { array_pop($arrayText); } if (reset($arrayText) == '') { array_shift($arrayText); } // Style Character $styleFont = array(); if (isset($oCharacters->style)) { if (isset($oCharacters->style->styleFont)) { $styleFont = $oCharacters->style->styleFont; } } foreach ($arrayText as $sText) { // HyperLink if (empty($sText) && !empty($sHYPERLINK)) { $arrHYPERLINK = explode('"', $sHYPERLINK); $oSection->addLink($arrHYPERLINK[1], null); // print_r('>addHyperLink<'.$sHYPERLINK.'>'.ord($sHYPERLINK[0]).EOL); $sHYPERLINK = ''; } // TextBreak if (empty($sText)) { $oSection->addTextBreak(); $sHYPERLINK = ''; // print_r('>addTextBreak<' . EOL); } if (!empty($sText)) { if (!empty($sHYPERLINK) && ord($sText[0]) > 20) { $sHYPERLINK .= $sText; } if (empty($sHYPERLINK)) { if (ord($sText[0]) > 20) { if (strpos(trim($sText), 'HYPERLINK "') === 0) { $sHYPERLINK = $sText; } else { $oSection->addText($sText, $styleFont); // print_r('>addText<'.$sText.'>'.ord($sText[0]).EOL); } } if (ord($sText[0]) == 1) { if (isset($oCharacters->style->image)) { $fileImage = tempnam(sys_get_temp_dir(), 'PHPWord_MsDoc') . '.' . $oCharacters->style->image['format']; file_put_contents($fileImage, $oCharacters->style->image['data']); $oSection->addImage($fileImage, array('width' => $oCharacters->style->image['width'], 'height' => $oCharacters->style->image['height'])); // print_r('>addImage<'.$fileImage.'>'.EOL); } } } } } } } } }
/** * Test SDT elements */ public function testSDTElements() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $section->addSDT('comboBox'); $section->addSDT('dropDownList'); $section->addSDT('date'); $doc = TestHelperDOCX::getDocument($phpWord); $path = "/w:document/w:body/w:p/w:sdt/w:sdtPr"; $this->assertTrue($doc->elementExists($path . '/w:comboBox')); $this->assertTrue($doc->elementExists($path . '/w:dropDownList')); $this->assertTrue($doc->elementExists($path . '/w:date')); }
/** * Test create/get section */ public function testCreateGetSections() { $phpWord = new PhpWord(); $this->assertEquals(new Section(1), $phpWord->addSection()); $phpWord->addSection(); $this->assertEquals(2, count($phpWord->getSections())); }
/** * 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); }
/** * Test line element */ public function testLineElement() { $phpWord = new PhpWord(); $section = $phpWord->addSection(); $section->addLine(array('width' => 1000, 'height' => 1000, 'positioning' => 'absolute', 'flip' => true)); $doc = TestHelperDOCX::getDocument($phpWord); $element = "/w:document/w:body/w:p/w:r/w:pict/v:shapetype"; $this->assertTrue($doc->elementExists($element)); }