コード例 #1
0
ファイル: SettingsTest.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Test set/get compatibity option
  */
 public function testSetGetCompatibility()
 {
     $this->assertTrue(Settings::getCompatibility());
     $this->assertTrue(Settings::setCompatibility(false));
     $this->assertFalse(Settings::getCompatibility());
     $this->assertFalse(Settings::setCompatibility('Non boolean'));
 }
コード例 #2
0
    protected function convertImpl($text)
    {
        $descr['book_title'] = $this->nameru;
        $descr['author'] = "";
        foreach ([$this->author, $this->illustrator] as $aut) {
            if ($aut) {
                foreach (explode(',', $aut) as $au) {
                    $a = explode(' ', trim($au));
                    $descr['author'] = (isset($descr['author']) ? $descr['author'] : '') . "<h1>";
                    $descr['author'] .= $this->escapexml(trim($au));
                    $descr['author'] .= "</h1>";
                }
            }
        }
        $descr['annotation'] = '';
        if ($this->annotation) {
            $this->annotation = preg_replace('@\\n@', '</p><p>', $this->annotation);
            $this->annotation = preg_replace("@'''(.*?)'''@", '<b>\\1</b>', $this->annotation);
            $this->annotation = preg_replace("@''(.*?)''@", '<i>\\1</i>', $this->annotation);
            $this->annotation = preg_replace('@<p></p>@', '<br/>', $this->annotation);
            $descr['annotation'] = "<h2>Аннотация</h2><p>{$this->annotation}</p>";
        }
        $descr['coverpage'] = '';
        $images = [];
        if ($this->covers) {
            $innerHeight = $this->height;
            $cover = $this->covers[0];
            $image = $this->images[$cover];
            /* Width and height are unimportant. Actual resizing is done not in this class. We must save aspect ratio though. */
            $descr['coverpage'] = "<img src=\"" . $image['thumbnail'] . "\" width=\"" . $image['convert_width'] . "\" height=\"" . $image['convert_height'] . "\" />";
            $images[] = $cover;
            $descr['coverpage_n'] = $cover;
        }
        //	echo $descr['coverpage'];
        //		exit;
        if ($this->translators) {
            foreach ($this->translators as $translator) {
                if (!array_key_exists('translator', $descr)) {
                    $descr['translator'] = '';
                }
                $descr['translator'] .= "<p name=\"translator\">" . $this->escapexml($translator) . "</p>";
            }
        }
        if ($this->seriestitle) {
            $descr['sequence'] = "<h1>" . $this->escapexml($this->seriestitle) . ($this->seriesnum ? " {$this->seriesnum}" : '') . " </h1>";
        }
        $descr['date2'] = date('j F Y, H:i', $this->touched);
        $descr['id'] = 'RuRa_' . str_replace('/', '_', $this->nameurl);
        if ($this->isbn) {
            $descr['isbn'] = ";isbn:{$this->isbn}";
        }
        if ($this->command == 'RuRa-team') {
            $credit = "<h2>Реквизиты переводчиков</h2>\n \t\t\t\t         <p>Над переводом работала команда <b>RuRa-team</b></p>\n";
            foreach ($this->workers as $activity => $workers) {
                $credit .= '<p>' . $activity . ': <b>' . implode('</b>, <b>', $workers) . "</b></p>\n";
            }
            $credit .= '<p>Самый свежий перевод всегда можно найти на сайте нашего проекта:</p>
				          <p><a href="http://ruranobe.ru">http://ruranobe.ru</a></p>
 				          <p>Чтобы оставаться в курсе всех новостей, вступайте в нашу группу в Контакте:</p>
				          <p><a href="http://vk.com/ru.ranobe">http://vk.com/ru.ranobe</a></p>
						  <p>Для желающих отблагодарить переводчика материально имеются webmoney-кошельки команды:</p>
						  <p><b>R125820793397</b></p>
						  <p><b>U911921912420</b></p>
						  <p><b>Z608138208963</b></p>
						  <p>QIWI-кошелек:</p>
						  <p><b>+79116857099</b></p>
						  <p>Яндекс-деньги:</p>
						  <p><b>410012692832515</b></p>
                          <p>PayPal:</p>
                          <p><b>paypal@ruranobe.ru</b></p>
						  <p>А так же счет для перевода с кредитных карт:</p>
						  <p><b>4890 4941 5384 9302</b></p>
						  <p>Версия от ' . date('d.m.Y', $this->touched) . '</p>
						  <p></p>
						  <p></p>
						  <p></p>
						  <p><b>Любое распространение перевода за пределами нашего сайта запрещено. Если вы скачали файл на другом сайте - вы поддержали воров</b></p>
						  <p></p>
						  <p></p>
						  <p></p>';
        } elseif (strpos($this->command, 'RuRa-team') !== false) {
            $credit = "<h2>Реквизиты переводчиков</h2>\n\t\t\t\t\t\t <p>Над релизом работали {$this->command}</p>\n";
            foreach ($this->workers as $activity => $workers) {
                $credit .= '<p>' . $activity . ': <b>' . implode('</b>, <b>', $workers) . "</b></p>\n";
            }
            $credit .= '<p>Самый свежий перевод всегда можно найти на сайте нашего проекта:</p>
						  <p><a l:href="http://ruranobe.ru">http://ruranobe.ru</a></p>
						  <p>Чтобы оставаться в курсе всех новостей, вступайте в нашу группу в Контакте:</p>
						  <p><a l:href="http://vk.com/ru.ranobe">http://vk.com/ru.ranobe</a></p>
						  <p>Версия от ' . date('d.m.Y', $this->touched) . '</p>
						  <p><b>Любое коммерческое использование данного текста или его фрагментов запрещено</b></p>';
        } else {
            $credit = "<h2>Реквизиты переводчиков</h2>";
            if ($this->command) {
                $credit .= "<p>Перевод команды {$this->command}</p>";
            }
            foreach ($this->workers as $activity => $workers) {
                $credit .= '<p>' . $activity . ': <b>' . implode('</b>, <b>', $workers) . "</b></p>\n";
            }
            $credit .= '<p>Версия от ' . date('d.m.Y', $this->touched) . '</p>
						  <p><b>Любое коммерческое использование данного текста или его фрагментов запрещено</b></p>';
        }
        if ($this->height == 0) {
            $text = preg_replace('/(<p[^>]*>)?<img[^>]*>(<\\/p>)?/u', '', $text);
        } else {
            for ($i = 1; $i < count($this->covers); ++$i) {
                $image = $this->images[$this->covers[$i]];
                $text = "<img src=\"" . $image['thumbnail'] . "\" width=\"" . $image['convert_width'] . "\" height=\"" . $image['convert_height'] . "\" />" . $text;
            }
            $text = preg_replace_callback('/(<a[^>]*>)?<img[^>]*data-resource-id="(-?\\d*)"[^>]*>(<\\/a>)?/u', function ($match) use(&$images) {
                if ($match[2] < 0) {
                    return '';
                }
                $image = $this->images[$match[2]];
                /* Width and height are unimportant. Actual resizing is done not in this class. We must save aspect ratio though. */
                return "<img src=\"" . $image['thumbnail'] . "\" width=\"" . $image['convert_width'] . "\" height=\"" . $image['convert_height'] . "\" />";
            }, $text);
        }
        $footnotes = array();
        $footnotes_temp = explode(',;,', $this->footnotes);
        for ($i = 0; $i < sizeof($footnotes_temp); $i++) {
            if (is_numeric($footnotes_temp[$i])) {
                $footnotes[$footnotes_temp[$i]] = $footnotes_temp[$i + 1];
                $i++;
            }
        }
        $text = trim($text);
        $epubText = "<html>\n\t<body>\n\t\t{$descr['coverpage']}\n\t\t{$descr['author']}\n\t\t{$descr['sequence']}\n\t    {$descr['annotation']}\n\t\t{$credit}\n\t\t{$text}\n\t</body>\n\t</html>";
        $epubText = preg_replace_callback('@(<span[^>]*><a href="#cite_note-(\\d*)"[^>]*>.{0,15}</span>)@', function ($match) use(&$footnotes) {
            $footnote = $footnotes[$match[2]];
            $footnote = preg_replace('@</p>\\s*<p[^>]*>@', '<br/>', $footnote);
            if ($footnote) {
                return '<footnote>' . $footnote . '</footnote>';
            } else {
                return $match[1];
            }
        }, $epubText);
        //preg_replace('@cite_note-(\d*)@',"<footnote></footnote>", $epubText);
        //echo '<xmp>'.$epubText;
        //echo $footnotes[137603266];
        //exit;
        //echo '<xmp>'.$epubText;
        //exit;
        $epubText = preg_replace('@section@', "div", $epubText);
        /* Delete extra <br/> tag before images */
        $epubText = preg_replace('@<div>(.){0,20}<br\\/>(.){0,20}<img src@', '<div><img src', $epubText);
        /* Eliminate caret return before <h1> (Each div starts with caret return in h2d_htmlconverter.php) */
        $epubText = preg_replace('@\\s*<div>(.{0,40})(<h1>.*?<\\/h1>)@', '\\1\\2<div>', $epubText);
        /* NGNL Specific names */
        //$text=str_replace('<span style="position: relative; text-indent: 0;"><span style="display: inline-block; font-style: normal">&#12302;&#12288;&#12288;&#12288;&#12303;</span><span style="position: absolute; font-size: .7em; top: -11px; left: 50%"><span style="position: relative; left: -50%;">','&#12302;<sup>',$text);
        //$text=str_replace('</span></span></span>','</sup>&#12303;',$text);
        // Styles of elements in which footnote is nested should not count. Thus close them
        $epubText = preg_replace('@pb@', "br", $epubText);
        //echo '<xmp>'.$epubText;
        //exit;
        //PHPWord doesn't support tags nested in link element. Unnest images from them
        $epubText = preg_replace('@<a[^>]*>(<img[^>]*>)<\\/a>@', "\\1", $epubText);
        // Delete extra page breaks related to images.
        $epubText = preg_replace('@<div[^>]*>(.){0,20}(<img[^>]*>)(.){0,20}<\\/div>@', "\\1\\2\\3", $epubText);
        $epubText = preg_replace('@<p[^>]*>(.){0,20}(<img[^>]*>)(.){0,20}<\\/p>@', "\\1\\2\\3", $epubText);
        /* Swap h2 and img tags if img follows h2. (It gave a bad look in docx). */
        $epubText = preg_replace('@(<h2>.{0,100}<\\/h2>)(<img[^>]*>)@', '\\2\\1', $epubText);
        /* After swap we often needs to further lift img tag in previous <div> or <p> tag */
        $epubText = preg_replace('@<\\/div>(<img[^>]*>)<h2@', '\\1</div><h2', $epubText);
        $epubText = preg_replace('@<\\/p>(<img[^>]*>)<h2@', '\\1</p><h2', $epubText);
        //echo '<xmp>'.$epubText;
        //exit;
        $phpword_object = new \PhpOffice\PhpWord\PhpWord();
        \PhpOffice\PhpWord\Settings::setCompatibility(false);
        $html_dom = new \simple_html_dom();
        $html_dom->load($epubText);
        $html_dom_array = $html_dom->find('html', 0)->children();
        $paths = htmltodocx_paths();
        $initial_state = ['phpword_object' => &$phpword_object, 'base_root' => $paths['base_root'], 'base_path' => $paths['base_path'], 'current_style' => ['size' => '11'], 'parents' => [0 => 'body'], 'list_depth' => 0, 'context' => 'section', 'pseudo_list' => true, 'pseudo_list_indicator_font_name' => 'Wingdings', 'pseudo_list_indicator_font_size' => '7', 'pseudo_list_indicator_character' => 'l ', 'table_allowed' => true, 'treat_div_as_paragraph' => true, 'structure_headings' => true, 'structure_document' => true, 'style_sheet' => htmltodocx_styles_example()];
        htmltodocx_insert_html($phpword_object, $html_dom_array[0]->nodes, $initial_state);
        //var_dump($html_dom_array[0]->nodes);
        //		exit;
        $html_dom->clear();
        unset($html_dom);
        $h2d_file_uri = tempnam(sys_get_temp_dir(), 'htd');
        /*if ($h2d_file_uri === false) {
              var_dump(sys_get_temp_dir());
          }*/
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpword_object, 'Word2007');
        $objWriter->save($h2d_file_uri);
        $bin = file_get_contents($h2d_file_uri);
        unlink($h2d_file_uri);
        //echo 'sdfjnsdlkvjn';
        //exit;
        return $bin;
    }
コード例 #3
0
<?php

include_once 'Sample_Header.php';
// New Word Document
echo date('H:i:s'), ' Create new PhpWord object', EOL;
$phpWord = new \PhpOffice\PhpWord\PhpWord();
\PhpOffice\PhpWord\Settings::setCompatibility(false);
// New portrait section
$section = $phpWord->addSection();
// Add style definitions
$phpWord->addParagraphStyle('pStyle', array('spacing' => 100));
$phpWord->addFontStyle('BoldText', array('bold' => true));
$phpWord->addFontStyle('ColoredText', array('color' => 'FF8080'));
$phpWord->addLinkStyle('NLink', array('color' => '0000FF', 'underline' => \PhpOffice\PhpWord\Style\Font::UNDERLINE_SINGLE));
// Add text elements
$textrun = $section->addTextRun('pStyle');
$textrun->addText(htmlspecialchars('This is some lead text in a paragraph with a following footnote. ', ENT_COMPAT, 'UTF-8'), 'pStyle');
$footnote = $textrun->addFootnote();
$footnote->addText(htmlspecialchars('Just like a textrun, a footnote can contain native texts. ', ENT_COMPAT, 'UTF-8'));
$footnote->addText(htmlspecialchars('No break is placed after adding an element. ', ENT_COMPAT, 'UTF-8'), 'BoldText');
$footnote->addText(htmlspecialchars('All elements are placed inside a paragraph. ', ENT_COMPAT, 'UTF-8'), 'ColoredText');
$footnote->addTextBreak();
$footnote->addText(htmlspecialchars('But you can insert a manual text break like above, ', ENT_COMPAT, 'UTF-8'));
$footnote->addText(htmlspecialchars('links like ', ENT_COMPAT, 'UTF-8'));
$footnote->addLink('https://github.com/PHPOffice/PHPWord', htmlspecialchars('PHPWord on GitHub', ENT_COMPAT, 'UTF-8'), 'NLink');
$footnote->addText(htmlspecialchars(', image like ', ENT_COMPAT, 'UTF-8'));
$footnote->addImage('resources/_earth.jpg', array('width' => 18, 'height' => 18));
$footnote->addText(htmlspecialchars(', or object like ', ENT_COMPAT, 'UTF-8'));
$footnote->addObject('resources/_sheet.xls');
$footnote->addText(htmlspecialchars('But you can only put footnote in section, not in header or footer.', ENT_COMPAT, 'UTF-8'));
$section->addText(htmlspecialchars('You can also create the footnote directly from the section making it wrap in a paragraph ' . 'like the footnote below this paragraph. But is is best used from within a textrun.', ENT_COMPAT, 'UTF-8'));
コード例 #4
0
ファイル: XMLWriterTest.php プロジェクト: doit05/relProject
 /**
  * Test method exception
  *
  * @expectedException \BadMethodCallException
  * @expectedExceptionMessage Method 'foo' does not exists.
  */
 public function testCallException()
 {
     Settings::setCompatibility(false);
     $object = new XMLWriter();
     $object->foo();
 }
コード例 #5
0
 /**
  * Test set/get compatibity option
  */
 public function testSetGetCompatibility()
 {
     $this->assertTrue(Settings::hasCompatibility());
     $this->assertTrue(Settings::setCompatibility(false));
     $this->assertFalse(Settings::hasCompatibility());
 }