/**
  * Create a MOBI and export to defined path
  * @param $dir str directory of the source file to convert
  * @param $src str filename of the source file to convert
  * @param $path str path to export the resultant MOBI to
  * @param $chapters array chapters to convert into a single MOBI
  * @param $journalId int Id of the journal(imprint)
  * @param $args array arguments for the conversion (e.g. Description, cover image, etc)
  */
 function createMobi($dir = null, $src, $path, $chapters = array(), $journalId, $args = array())
 {
     $mobi = new MOBI();
     $CBPPlatformDao =& DAORegistry::getDAO('CBPPlatformDAO');
     $imprintType = $CBPPlatformDao->getImprintType($journalId);
     $mobiContent = $this->contentStart;
     if (isset($args['cover'])) {
         $mobiContent .= "<img src='" . $args['cover'] . "' style=\"margin: 0px auto;\" />";
     } else {
         if (isset($args['title'])) {
             $mobiContent .= "<h1>" . $args['title'] . "</h1><br />";
         }
         if (isset($args['author'])) {
             $mobiContent .= "<p><strong>" . $args['author'] . "</strong></p>";
         }
     }
     $copyrightStatement = $CBPPlatformDao->getJournalCopyrightStatement($journalId);
     if (!empty($copyrightStatement)) {
         $copyrightStatement = reset($copyrightStatement);
         $mobiContent .= "<mbp:pagebreak /><div style='width: 75%; text-align: center; margin: 0 auto;'><p>" . $copyrightStatement . "</p></div>";
     }
     if (!empty($chapters)) {
         for ($i = 0; $i < count($chapters); $i++) {
             $document = new TransformDoc();
             $document->setStrFile($chapters[$i]['src'], $chapters[$i]['dir']);
             $document->generateXHTML();
             //problem, here
             $document->validatorXHTML();
             $contentPreg = $this->stripTags($document->getStrXHTML());
             $introduction = "";
             if ($imprintType == "collection") {
                 if ($chapters[$i]['description'] != "") {
                     $introduction = "<mbp:pagebreak /><div class='submissionIntroEpub'><h1>" . $chapters[$i]['author'] . "</h1><div style='font-style: italic;'>" . $this->stripTags($chapters[$i]['description'], true) . "</div></div></body></html>";
                 }
             }
             $mobiContent .= $introduction . $contentPreg;
         }
     } else {
         $content = new TransformDoc();
         $content->setStrFile($src, $dir);
         $content->generateXHTML();
         $content->validatorXHTML();
         $contentPreg = $this->stripTags($content->getStrXHTML());
         $mobiContent .= $contentPreg;
     }
     $mobiContent .= "</body></html>";
     isset($args['title']) ? $options['title'] = $args['title'] : ($options['title'] = "No Title");
     isset($args['description']) ? $options['subject'] = $args['description'] : ($options['subject'] = "No description");
     isset($args['author']) ? $options['author'] = $args['author'] : ($options['author'] = "No author");
     isset($args['isbn']) ? $options['uniqueID'] = $args['isbn'] : ($options['uniqueID'] = "No isbn");
     $mobi->setData($mobiContent);
     $mobi->setOptions($options);
     $mobi->save($path);
 }
    /**
     * Create a PDF and export to defined path
     * @param $dir str directory of the source file to convert
     * @param $src str filename of the source file to convert
     * @param $path str path to export the resultant PDF to
     * @param $chapters array chapters to convert into a single PDF
     * @param $journalId int Id of the journal(imprint)
     * @param $args array arguments for the conversion (e.g. Description, cover image, etc)
     * @param $coverPath str path to export the front cover artwork to
     */
    function createPdf($dir = null, $src, $path, $chapters = array(), $journalId, $args = array(), $coverPath)
    {
        $mpdf = new mPDF('utf-8');
        $mpdf->useOddEven = 1;
        $htmlEncode = array('title', 'author');
        foreach ($htmlEncode as $encode) {
            $args[$encode] = htmlentities($args[$encode], ENT_QUOTES, "UTF-8");
        }
        isset($args['title']) ? $mpdf->SetTitle($args['title']) : $mpdf->SetTitle("No Title");
        isset($args['description']) ? $mpdf->SetSubject($args['description']) : $mpdf->SetSubject("No description");
        isset($args['author']) ? $mpdf->SetCreator($args['author']) : $mpdf->SetCreator("No author");
        $CBPPlatformDao =& DAORegistry::getDAO('CBPPlatformDAO');
        $imprintType = $CBPPlatformDao->getImprintType($journalId);
        $stylesheet = $CBPPlatformDao->getImprintStylesheet($journalId);
        $stylesheetContents = file_get_contents($this->stylesheetDir . "{$stylesheet}.css");
        $mpdf->WriteHTML($stylesheetContents, 1);
        $mpdf->WriteHTML($this->contentStart . '
			<htmlpagefooter name="myFooter1" style="display:none">
			<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; 
			    color: #000000; font-weight: bold; font-style: italic;"><tr>
			    <td width="33%" style="text-align: right; ">{PAGENO}</td>
			    </tr></table>
			</htmlpagefooter>
			<htmlpagefooter name="myFooter2" style="display:none">
			<table width="100%" style="vertical-align: bottom; font-family: serif; font-size: 8pt; 
			    color: #000000; font-weight: bold; font-style: italic;"><tr>
			    <td width="33%"><span style="font-weight: bold; font-style: italic;">{PAGENO}</span></td>
			    </tr></table>
			</htmlpagefooter>');
        $imagesize = getimagesize($args['cover']);
        if (substr($imagesize[1] / $imagesize[0], 0, strpos($imagesize[1] / $imagesize[0], '.') + 1 + 2) == 1.41 || substr($imagesize[1] / $imagesize[0], 0, strpos($imagesize[1] / $imagesize[0], '.') + 1 + 2) == 1.53) {
            $pdfContent .= '<div style="position: absolute; left:0; right: 0; top: 0; bottom: 0;"><img src="' . $args['cover'] . '" id="cover" /></div>';
        } else {
            $pdfContent .= "<div style='margin: 0 auto; width: 80%; text-align: center;'>";
            if (isset($args['title'])) {
                $pdfContent .= "<h1>" . $args['title'] . "</h1>";
            }
            if (isset($args['cover'])) {
                $pdfContent .= "<img src=\"" . $args['cover'] . "\" >";
            } else {
                $pdfContent .= "<br/>";
            }
            if (isset($args['author'])) {
                $pdfContent .= "<h2>" . $args['author'] . "</h2>";
            }
            $pdfContent .= "</div>";
        }
        $mpdf->WriteHTML($pdfContent);
        $mpdf->AddPage('', '', '', '', 'Off');
        $copyrightStatement = $CBPPlatformDao->getJournalCopyrightStatement($journalId);
        if (!empty($copyrightStatement)) {
            $copyrightStatement = reset($copyrightStatement);
            $mpdf->AddPage('', '', '', '', 'Off');
            $innerPageConent = "<div style='width: 90%; text-align: center; margin: 0 auto;'><p>" . $copyrightStatement . "</p></div>";
            $mpdf->WriteHTML($innerPageConent);
        }
        if (!empty($chapters)) {
            $mpdf->TOCpagebreakByArray(array('TOCusePaging' => true, 'TOCuseLinking' => true, 'toc_preHTML' => '<h1>Table of Contents</h1>', 'toc_postHTML' => '', 'resetpagenum' => 1, 'suppress' => 'true'));
            $chapterCount = 0;
            $authorBiographies = 0;
            foreach ($chapters as $chapter) {
                if (!isset($chapter['type']) && $chapter['type'] != "supp") {
                    $chapterCount++;
                } else {
                    if ($chapter['desc'] == "Author Biography") {
                        $authorBiographies++;
                    }
                    $suppChapters = true;
                }
            }
            for ($i = 0; $i < count($chapters); $i++) {
                $htmlEncode = array('name', 'author');
                foreach ($htmlEncode as $encode) {
                    $chapters[$i][$encode] = htmlentities($chapters[$i][$encode], ENT_QUOTES, "UTF-8");
                }
                $document = new TransformDoc();
                $document->setStrFile($chapters[$i]['src'], $chapters[$i]['dir']);
                $document->generateXHTML();
                //problem, here
                $document->validatorXHTML();
                if ($chapterCount == 1) {
                    $contentPreg = $this->stripTagsAddChapters($document->getStrXHTML());
                    $contentPreg = ltrim($contentPreg);
                    if (substr($contentPreg, 0, 13) == "<pagebreak />") {
                        $contentPreg = substr_replace($contentPreg, '', 0, 13);
                    }
                    $mpdf->addPage('', '', '', '', 'On');
                    $mpdf->PageNumSubstitutions[] = array('from' => $mpdf->page + 1, 'reset' => 1, 'type' => '1', 'suppress' => 'off');
                    $mpdf->WriteHTML("<div class='content'>", 2);
                    $mpdf->WriteHTML($contentPreg, 2);
                    if ($suppChapters == true) {
                        foreach ($chapters as $chapter) {
                            if (isset($chapter['type']) && $chapter['type'] == "supp") {
                                $document = new TransformDoc();
                                $document->setStrFile($chapter['src'], $chapter['dir']);
                                $document->generateXHTML();
                                $document->validatorXHTML();
                                if ($authorBiographies > 1) {
                                    $contentPreg = $this->stripTags($document->getStrXHTML());
                                    $mpdf->TOC_Entry($chapter['name']);
                                    $mpdf->WriteHTML("<pagebreak />" . $contentPreg, 2);
                                } else {
                                    $addAuthorBiographyToBack = true;
                                    $authorBiography = $this->stripTags($document->getStrXHTML());
                                }
                            }
                        }
                    }
                    break;
                } else {
                    $contentPreg = $this->stripTags($document->getStrXHTML());
                    $contentPreg = ltrim($contentPreg);
                    if (substr($contentPreg, 0, 13) == "<pagebreak />") {
                        $contentPreg = substr_replace($contentPreg, '', 0, 13);
                    }
                    if ($i != 0) {
                        $prepend = "<pagebreak />";
                    } else {
                        $mpdf->addPage('', 'E', '', '', 'On');
                        $mpdf->PageNumSubstitutions[] = array('from' => $mpdf->page + 1, 'reset' => 1, 'type' => '1', 'suppress' => 'off');
                        $mpdf->WriteHTML("<div class='content'>", 2);
                    }
                    if ($imprintType == "atomistic") {
                        $mpdf->WriteHTML($prepend . "<tocentry content='" . $chapters[$i]['name'] . "' level='0' />" . $contentPreg, 2);
                    } elseif ($imprintType == "collection") {
                        if ($chapters[$i]['description'] != "") {
                            $introduction = "<div class='submissionIntro'><h1>" . $chapters[$i]['author'] . "</h1>" . $this->stripTags($chapters[$i]['description'], true) . "</div><pagebreak /><tocentry content='" . $chapters[$i]['name'] . " by " . $chapters[$i]['author'] . "' level='0' />";
                        }
                        $mpdf->WriteHTML($prepend . $introduction . $contentPreg, 2);
                    }
                }
            }
            $mpdf->writeHTML("</div>");
            if (isset($args['description'])) {
                $mpdf->WriteHTML("<pagebreak page-selector='none' odd-footer-value = '' even-footer-value= '' /><pagebreak /><div style='width: 90%; text-align: center; margin: 0 auto;'><p>" . $this->stripTags($args['description'], true) . "</p></div>", 2);
                if ($addAuthorBiographyToBack == true) {
                    $backCoverContent .= "<div style='width: 90%; text-align: center; margin: 0 auto; margin-top: 10px;'><p>" . $authorBiography . "</p></div>";
                }
                $backCoverContent .= "<p style='width: 90%; text-align: center; margin: 0 auto;'><strong>Published " . date("F Y") . ", Scarborough, UK</strong></p>";
                $mpdf->WriteHTML($backCoverContent, 2);
            }
            $mpdf->WriteHTML("</body></html>");
            $pdfData = $mpdf->Output('', 'S');
            $pageCount = $mpdf->page;
            file_put_contents($path, $pdfData);
            if (file_exists($this->stylesheetDir . "{$stylesheet}-FC.css")) {
                $this->createCoverPdf($stylesheet, $pageCount, $args['cover'], $this->stripTags($args['description'], true), $addAuthorBiographyToBack, $authorBiography, $args['title'], $args['imprint'], $coverPath);
            }
            return true;
        } else {
            $document = new TransformDoc();
            $document->setStrFile($src, $dir);
            $document->generateXHTML();
            $document->validatorXHTML();
            $contentPreg = $this->stripTagsAddChapters($document->getStrXHTML());
            $contentPreg = ltrim($contentPreg);
            if (substr($contentPreg, 0, 13) == "<pagebreak />") {
                $contentPreg = substr_replace($contentPreg, '', 0, 13);
            }
            $mpdf->addPage('', 'E', '', '', 'On');
            $mpdf->PageNumSubstitutions[] = array('from' => $mpdf->page + 1, 'reset' => 1, 'type' => '1', 'suppress' => 'off');
            $mpdf->WriteHTML("<div class='content'>", 2);
            $mpdf->WriteHTML($contentPreg, 2);
            $mpdf->WriteHTML("</div></body></html>");
            $pdfData = $mpdf->Output('', 'S');
            file_put_contents($path, $pdfData);
            return true;
        }
    }
 /**
  * Create an EPUB and export to defined path
  * @param $dir str directory of the source file to convert
  * @param $src str filename of the source file to convert
  * @param $path str path to export the resultant EPUB to
  * @param $chapters array chapters to convert into a single EPUB
  * @param $journalId int Id of the journal(imprint)
  * @param $args array arguments for the conversion (e.g. Description, cover image, etc)
  */
 function createEpub($dir = null, $src, $path, $chapters = array(), $journalId, $args = array())
 {
     $book = new EPub();
     if (isset($args['title'])) {
         $book->setTitle($args['title']);
     } else {
         $book->setTitle("No Title");
     }
     $book->setLanguage("en");
     $book->setPublisher(Config::getVar('general', 'base_url'), Config::getVar('general', 'base_url'));
     $book->setDate(time());
     $book->setRights("Copyright and licence information specific for the book.");
     //TODO: import specific copyright/licence information
     $book->setSourceURL(Config::getVar('general', 'base_url'));
     $CBPPlatformDao =& DAORegistry::getDAO('CBPPlatformDAO');
     $imprintType = $CBPPlatformDao->getImprintType($journalId);
     $stylesheet = $CBPPlatformDao->getImprintStylesheet($journalId);
     $book->addCSSFile("styles.css", "css1", file_get_contents($this->stylesheetDir . "{$stylesheet}.css"));
     isset($args['description']) ? $book->setDescription($args['description']) : $book->setDescription("No description");
     isset($args['author']) ? $book->setAuthor($args['author'], $args['author']) : $book->setAuthor("No author", "No author");
     $args['isbn'] != null ? $book->setIdentifier($args['isbn'], EPub::IDENTIFIER_URI) : $book->setIdentifier(Config::getVar('general', 'base_url'), EPub::IDENTIFIER_URI);
     if (isset($args['cover'])) {
         $cover = $args['cover'];
         $coverSrc = "\n\t\t\t\t\t<svg version='1.1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='100%' height='100%' viewBox='0 0 410 597' preserveAspectRatio='xMidYMid meet'>\n\t\t\t\t\t\t<image width='410' height='597' xlink:href='images/Cover.jpg' />\n\t\t\t\t\t</svg>";
         $book->addChapter("Cover Page", "Cover.html", $this->contentStart . "{$coverSrc}</body>\n</html>\n", false, EPub::EXTERNAL_REF_ADD, $this->fileDir);
         $book->setCoverImage("Cover.jpg", file_get_contents($args['cover']));
     } else {
         $book->addChapter("Cover Page", "Cover.html", $this->contentStart . "<h1>" . $book->getTitle() . "</h1><h3>" . $book->getAuthor() . "</h3></body>\n</html>\n", false, EPub::EXTERNAL_REF_ADD, $this->fileDir);
     }
     $copyrightStatement = $CBPPlatformDao->getJournalCopyrightStatement($journalId);
     if (!empty($copyrightStatement)) {
         $copyrightStatement = reset($copyrightStatement);
         $copyrightStatement = "<div style='width: 75%; text-align: center; margin: 0 auto;'><p>" . $copyrightStatement . "</p></div>";
         $book->addChapter("Copyright Notice", "notice.html", $this->contentStart . $copyrightStatement, false, EPub::EXTERNAL_REF_ADD, $this->fileDir);
     }
     if (!empty($chapters)) {
         $chapterCount = 0;
         foreach ($chapters as $chapter) {
             if (!isset($chapter['type']) && $chapter['type'] != "supp") {
                 $chapterCount++;
             } else {
                 $suppChapters = true;
             }
         }
         for ($i = 0; $i < count($chapters); $i++) {
             $document = new TransformDoc();
             $document->setStrFile($chapters[$i]['src'], $chapters[$i]['dir']);
             $document->generateXHTML();
             //problem, here
             $document->validatorXHTML();
             $contentPreg = $this->stripTags($document->getStrXHTML());
             if ($chapterCount == 1) {
                 $splitter = new EPubChapterSplitter();
                 $xhtml = $this->contentStart . $contentPreg . "</body></html>";
                 $html2 = $splitter->splitChapter($xhtml, true, '@<h1[^>]*?.*?</h1>@siu');
                 /* '#^<.+?>Chapter \d*#i'); */
                 foreach ($html2 as $key => $value) {
                     $cTitles[] = $key;
                     $cContent[] = $value;
                 }
                 for ($i = 0; $i < count($cContent); $i++) {
                     $html3[$cTitles[$i]] = $cContent[$i];
                 }
                 $idx = 0;
                 if (!empty($html3)) {
                     foreach ($html3 as $k => $v) {
                         $idx++;
                         $cName = preg_replace('#<br.+?>#i', " - ", $k);
                         // Remove any other tags
                         $cName = preg_replace('/<[^>]*>/', '', $cName);
                         // clean the chapter name by removing any double spaces left behind to single space.
                         $cName = preg_replace('#\\s+#i', " ", $cName);
                         $book->addChapter($cName, "Chapter" . $idx . ".html", $v, false, EPub::EXTERNAL_REF_ADD, $fileDir);
                     }
                 }
                 if ($suppChapters == true) {
                     $i = 0;
                     foreach ($chapters as $chapter) {
                         if (isset($chapter['type']) && $chapter['type'] == "supp") {
                             $document = new TransformDoc();
                             $document->setStrFile($chapter['src'], $chapter['dir']);
                             $document->generateXHTML();
                             $document->validatorXHTML();
                             $contentPreg = $this->stripTags($document->getStrXHTML());
                             $book->addChapter($chapter['name'], "Chapter{$i}.html", $this->contentStart . $contentPreg . "</body></html>", false, EPub::EXTERNAL_REF_ADD, $this->fileDir);
                         }
                     }
                     $i++;
                 }
                 break;
             } else {
                 if ($imprintType == "atomistic") {
                     $book->addChapter($cName, "Chapter" . $i . ".html", $this->contentStart . $contentPreg . "</body></html>", false, EPub::EXTERNAL_REF_ADD, $fileDir);
                 } elseif ($imprintType == "collection") {
                     if ($chapters[$i]['description'] != "") {
                         $book->addChapter("Introduction to " . $chapters[$i]['name'] . " by " . $chapters[$i]['author'], "Chapter" . "{$i}-I" . ".html", $this->contentStart . "<div class='submissionIntroEpub'><h1>" . $chapters[$i]['author'] . "</h1>" . $this->stripTags($chapters[$i]['description'], true) . "</div></body></html>", false, EPub::EXTERNAL_REF_ADD, $fileDir, false);
                     }
                     $book->addChapter($chapters[$i]['name'] . " by " . $chapters[$i]['author'], "Chapter" . $i . ".html", $this->contentStart . "{$contentPreg}</body></html>", false, EPub::EXTERNAL_REF_ADD, $fileDir);
                 }
             }
         }
     } else {
         $content = new TransformDoc();
         $splitter = new EPubChapterSplitter();
         $content->setStrFile($src, $dir);
         $content->generateXHTML();
         $content->validatorXHTML();
         $contentPreg = $this->stripTags($content->getStrXHTML());
         $xhtml = $this->contentStart . $contentPreg . "</body></html>";
         $html2 = $splitter->splitChapter($xhtml, true, '@<h1[^>]*?.*?</h1>@siu');
         foreach ($html2 as $key => $value) {
             $cTitles[] = $key;
             $cContent[] = $value;
         }
         array_shift($cContent);
         for ($i = 0; $i < count($cContent); $i++) {
             $html3[$cTitles[$i]] = $cContent[$i];
         }
         $idx = 0;
         if (!empty($html3)) {
             foreach ($html3 as $k => $v) {
                 $idx++;
                 $cName = preg_replace('#<br.+?>#i', " - ", $k);
                 // Remove any other tags
                 $cName = preg_replace('/<[^>]*>/', '', $cName);
                 //clean the chapter name by removing any double spaces left behind to single space.
                 $cName = preg_replace('#\\s+#i', " ", $cName);
                 $book->addChapter($cName, "Chapter" . $idx . ".html", $v, false, EPub::EXTERNAL_REF_ADD, $fileDir);
             }
         } else {
             $book->addChapter("Body Text", "Chapter1.html", $xhtml, false, EPub::EXTERNAL_REF_ADD, $fileDir);
         }
     }
     $book->finalize();
     $zipData = $book->getBook();
     file_put_contents($path, $zipData);
 }
Example #4
0
<?php

/**
 * Transform DOCX into HTML
 *
 * @category   Phpdocx
 * @package    examples
 * @subpackage easy
 * @copyright  Copyright (c) Narcea Producciones Multimedia S.L.
 *             (http://www.2mdc.com)
 * @license    LGPL
 * @version    3.0
 * @link       http://www.phpdocx.com
 * @since      File available since Release 3.0
 */
require_once '../../classes/TransformDoc.inc';
$document = new TransformDoc();
$document->setStrFile('../files/Text.docx');
$document->generateXHTML();
$document->validatorXHTML();
echo $document->getStrXHTML();