/** * 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 .= '<>“‘’«»‹›'; $content .= '&•°…™©®—'; $content .= '–   ²³¼½¾'; Html::addHtml($section, $content); }
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; }
/** * 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); }
/** * @param Section $section * @param Page $page * * @return Section */ public function write(Section $section, Page $page) { foreach ($page->getText() as $text) { if ($text instanceof HtmlText) { Html::addHtml($section, $text->getText(), $text->isFullHtml()); } else { $section->addText($text->getText()); } } if ($page->getHeaders()) { foreach ($page->getHeaders() as $header) { if ($header->getRawText() instanceof PreserveText) { $section->addHeader()->addPreserveText($header->getText(), $header->getRawText()->getStyleFont(), $header->getRawText()->getStyleParagraph()); } else { $section->addHeader()->addText($header->getText()); } } } if ($page->getFooters()) { foreach ($page->getFooters() as $footer) { if ($footer->getRawText() instanceof PreserveText) { $section->addFooter()->addPreserveText($footer->getText(), $footer->getRawText()->getStyleFont(), $footer->getRawText()->getStyleParagraph()); } else { $section->addFooter()->addText($footer->getText(), $footer->getRawText()->getStyleFont(), $footer->getRawText()->getStyleParagraph()); } } } return $section; }
/** * 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 array $pages * @param string $fileName * @param array|int $format */ public static function html2doc($pages, $fileName, $format = array()) { if (is_array($format)) { // PDF Page Format parameters passed in - merge with defaults $format += CRM_Core_BAO_PdfFormat::getDefaultValues(); } else { // PDF Page Format ID passed in $format = CRM_Core_BAO_PdfFormat::getById($format); } $paperSize = CRM_Core_BAO_PaperSize::getByName($format['paper_size']); $metric = CRM_Core_BAO_PdfFormat::getValue('metric', $format); $pageStyle = array('orientation' => CRM_Core_BAO_PdfFormat::getValue('orientation', $format), 'pageSizeW' => self::toTwip($paperSize['width'], $paperSize['metric']), 'pageSizeH' => self::toTwip($paperSize['height'], $paperSize['metric']), 'marginTop' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_top', $format), $metric), 'marginRight' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_right', $format), $metric), 'marginBottom' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_bottom', $format), $metric), 'marginLeft' => self::toTwip(CRM_Core_BAO_PdfFormat::getValue('margin_left', $format), $metric)); $ext = pathinfo($fileName, PATHINFO_EXTENSION); $phpWord = new \PhpOffice\PhpWord\PhpWord(); $phpWord->getDocInfo()->setCreator(CRM_Core_DAO::getFieldValue('CRM_Contact_BAO_Contact', CRM_Core_Session::getLoggedInContactID(), 'display_name')); foreach ((array) $pages as $page => $html) { $section = $phpWord->addSection($pageStyle + array('breakType' => 'nextPage')); \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html); } self::printDoc($phpWord, $ext, $fileName); }
/** * @param Section $section * @param Page $page * * @return Section */ public function write(Section $section, Page $page) { foreach ($page->getText() as $text) { if ($text instanceof HtmlText) { Html::addHtml($section, $text->getText(), $text->isFullHtml()); } else { $section->addText($text->getText()); } } if ($page->getHeaders()) { $wordHeader = $section->addHeader(); $table = $wordHeader->addTable(); $table->addRow(); foreach ($page->getHeaders() as $header) { $cell = $table->addCell(9000 / count($page->getHeaders())); if ($header->getRawText() instanceof PreserveText) { $cell->addHeader()->addPreserveText($header->getText(), $header->getRawText()->getStyleFont(), $header->getRawText()->getStyleParagraph()); } else { $cell->addHeader()->addText($header->getText()); } } } if ($page->getFooters()) { $wordFooter = $section->addFooter(); $table = $wordFooter->addTable(); $table->addRow(); foreach ($page->getFooters() as $footer) { $cell = $table->addCell(9000 / count($page->getFooters())); if ($footer->getRawText() instanceof PreserveText) { $cell->addPreserveText($footer->getText(), $footer->getRawText()->getStyleFont(), $footer->getRawText()->getStyleParagraph()); } else { $cell->addText($footer->getText(), $footer->getRawText()->getStyleFont(), $footer->getRawText()->getStyleParagraph()); } } } return $section; }
<?php include_once 'Sample_Header.php'; // New Word Document echo date('H:i:s'), ' Create new PhpWord object', EOL; $phpWord = new \PhpOffice\PhpWord\PhpWord(); $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><p>Item 1</p></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); // Save file echo write($phpWord, basename(__FILE__, '.php'), $writers); if (!CLI) { include_once 'Sample_Footer.php'; }
$cellule_contenu = $table_competences->addCell($cell_width); \PhpOffice\PhpWord\Shared\Html::addHtml($cellule_contenu, $competences); // Fin de la section de l'énoncé des compétences // ---------------------------------------------- // ---------------------------------------------- // Section des objectifs d'apprentissage $section_apprentissage = $php_word->addSection(); $titre = "Objectifs d'apprentissage"; $table_apprentissage = $section_apprentissage->addTable('style_table'); $nombre_colonnes = 1; $cell_width = $table_width / $nombre_colonnes; $table_apprentissage->addRow($style_row_titre); $cellule_titre = $table_apprentissage->addCell($cell_width)->addText($titre, $style_font_titre, $style_align_center); $table_apprentissage->addRow($style_row); $cellule_contenu = $table_apprentissage->addCell($cell_width); \PhpOffice\PhpWord\Shared\Html::addHtml($cellule_contenu, $apprentissage); // Fin de la section des objectifs d'apprentissage // ---------------------------------------------- $plancadre = fetchPlanCadreElaboration_PlanCadre($_POST['id_plancadre']); $path_docx = "../plancadre/" . $plancadre[0]['No_PlanCadre'] . "_" . $plancadre[0]['CodeCours'] . ".docx"; $php_word->save($path_docx); //header('Location: ../view/view_create_plancadre.php'); } else { if (isset($_POST['open'])) { header('Location: ../view/view_elaboration_plancadre.php'); } } function getPlanCadre($id_plancadre) { return fetchPlanCadreElaboration_PlanCadre($id_plancadre); }
$textcenter3 = $section->addTextRun(array('align' => 'center')); $textcenter3->addText('Sub: ' . $status_name . '', array('name' => 'Times New Roman', 'size' => 12, 'align' => 'center', 'bold' => true, 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE)); //$textleft2 = $section->addTextRun(array('align' => 'left')); //$textleft2 = $section->addTable(); //$textleft2->addRow(); //$cell = $textleft2->addCell(10000); //$textleftrow = $cell->addTextRun(array('align' => 'left')); //$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, $para1); \PhpOffice\PhpWord\Shared\Html::addHtml($section, $para2); //$textleft2->addText($para1[0],array('name'=>'Times New Roman', 'size'=>12)); //$textleft2->addText($para1[1],array('name'=>'Times New Roman', 'size'=>12, 'bold'=>true,)); //$textleft2->addText($para1[2],array('name'=>'Times New Roman', 'size'=>12)); //$textleft2->addTextBreak(); //$textleft2->addTextBreak(); //$textleft2->addText($para1[3],array('name'=>'Times New Roman', 'size'=>12)); // //$textleft4 = $section->addTextRun(array('align' => 'left')); //$textleft4->addText($para2[0],array('name'=>'Times New Roman', 'size'=>12)); //$textleft4->addTextBreak(); //$textleft4->addTextBreak(); //$textleft4->addText($para2[1],array('name'=>'Times New Roman', 'size'=>12)); //$textleft4->addTextBreak(); //$textleft4->addTextBreak(); //$textleft4->addText($para2[2],array('name'=>'Times New Roman', 'size'=>12, 'bold'=>true,));
function addSection($php_word, $titre_section, $contenu_section) { $section = $php_word->addSection(); $table = $section->addTable('style_table'); $nombre_colonnes = 1; $cell_width = $GLOBALS["table_width"] / $nombre_colonnes; $table->addRow($GLOBALS["style_row_titre"]); $cellule_titre = $table->addCell($cell_width)->addText($titre_section, $GLOBALS["style_font_titre"], $GLOBALS["style_align_center"]); $table->addRow($GLOBALS["style_row"]); $cellule_contenu = $table->addCell($cell_width); \PhpOffice\PhpWord\Shared\Html::addHtml($cellule_contenu, $contenu_section); }