Beispiel #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;
             }
         }
     }
 }
 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;
 }
 /**
  * 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);
         }
     }
 }
 /**
  * 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;
 }
Beispiel #5
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'));
 }
Beispiel #6
1
 /**
  * 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));
 }
Beispiel #7
0
 /**
  * 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;
             }
         }
     }
 }
Beispiel #8
0
 /**
  * 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');
 }
 /**
  * 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));
 }
 /**
  * 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;
 }
Beispiel #11
0
 /**
  * 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'));
 }
Beispiel #12
0
 /**
  * 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);
 }
Beispiel #13
0
 /**
  * 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);
     }
 }
 /**
  * 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);
 }
Beispiel #15
0
 /**
  * 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);
 }
Beispiel #16
0
 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");
 }
Beispiel #17
0
 /**
  * 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;
             }
         }
     }
 }
 /**
  * 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"));
 }
 /**
  * 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'));
 }
Beispiel #20
0
 /**
  * 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());
 }
/**
 * Write documents
 *
 * @param \PhpOffice\PhpWord\PhpWord $phpWord
 * @param string $filename
 * @param array $writers
 *
 * @return string
 */
function write($phpWord, $filename, $writers)
{
    $result = '';
    // Write documents
    foreach ($writers as $format => $extension) {
        $result .= date('H:i:s') . " Write to {$format} format";
        if (null !== $extension) {
            $targetFile = __DIR__ . "/results/{$filename}.{$extension}";
            $phpWord->save($targetFile, $format);
        } else {
            $result .= ' ... NOT DONE!';
        }
        $result .= EOL;
    }
    $result .= getEndingNotes($writers);
    return $result;
}
 /**
  * Read custom document properties.
  *
  * @param \PhpOffice\PhpWord\PhpWord $phpWord
  * @return void
  */
 public function read(PhpWord $phpWord)
 {
     $xmlReader = new XMLReader();
     $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
     $docProps = $phpWord->getDocInfo();
     $nodes = $xmlReader->getElements('*');
     if ($nodes->length > 0) {
         foreach ($nodes as $node) {
             $propertyName = $xmlReader->getAttribute('name', $node);
             $attributeNode = $xmlReader->getElement('*', $node);
             $attributeType = $attributeNode->nodeName;
             $attributeValue = $attributeNode->nodeValue;
             $attributeValue = DocInfo::convertProperty($attributeValue, $attributeType);
             $attributeType = DocInfo::convertPropertyType($attributeType);
             $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType);
         }
     }
 }
Beispiel #23
0
 /**
  * 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);
 }
Beispiel #24
0
 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);
 }
 /**
  * Read core/extended document properties.
  *
  * @param \PhpOffice\PhpWord\PhpWord $phpWord
  * @return void
  */
 public function read(PhpWord $phpWord)
 {
     $xmlReader = new XMLReader();
     $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
     $docProps = $phpWord->getDocInfo();
     $nodes = $xmlReader->getElements('*');
     if ($nodes->length > 0) {
         foreach ($nodes as $node) {
             if (!isset($this->mapping[$node->nodeName])) {
                 continue;
             }
             $method = $this->mapping[$node->nodeName];
             $value = $node->nodeValue == '' ? null : $node->nodeValue;
             if (isset($this->callbacks[$node->nodeName])) {
                 $value = $this->callbacks[$node->nodeName]($value);
             }
             if (method_exists($docProps, $method)) {
                 $docProps->{$method}($value);
             }
         }
     }
 }
Beispiel #26
0
 /**
  * 生成简历文件字符串
  * @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;
 }
Beispiel #27
0
 /**
  * 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();
     }
 }
Beispiel #28
0
 /**
  * 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());
 }
Beispiel #29
-1
 /**
  * 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));
 }
Beispiel #30
-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);
 }