コード例 #1
0
$log->logLine("Zip version: " . Zip::VERSION);
$log->logLine("getCurrentServerURL: " . $book->getCurrentServerURL());
$log->logLine("getCurrentPageURL..: " . $book->getCurrentPageURL());
// Title and Identifier are mandatory!
$book->setTitle("Test book");
$book->setIdentifier("http://JohnJaneDoePublications.com/books/TestBook.html", EPub::IDENTIFIER_URI);
// Could also be the ISBN number, prefered for published books, or a UUID.
$book->setLanguage("en");
// Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc.
$book->setDescription("This is a brief description\nA test ePub book as an example of building a book in PHP");
$book->setAuthor("John Doe Johnson", "Johnson, John Doe");
$book->setPublisher("John and Jane Doe Publications", "http://JohnJaneDoePublications.com/");
// I hope this is a non existant address :)
$book->setDate(time());
// Strictly not needed as the book date defaults to time().
$book->setRights("Copyright and licence information specific for the book.");
// As this is generated, this _could_ contain the name or licence information of the user who purchased the book, if needed. If this is used that way, the identifier must also be made unique for the book.
$book->setSourceURL("http://JohnJaneDoePublications.com/books/TestBook.html");
$book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
$book->setSubject("Test book");
$book->setSubject("keywords");
$book->setSubject("Chapter levels");
// Insert custom meta data to the book, in this cvase, Calibre series index information.
$book->addCustomMetadata("calibre:series", "PHPePub Test books");
$book->addCustomMetadata("calibre:series_index", "2");
$log->logLine("Set up parameters");
$cssData = "body {\n  margin-left: .5em;\n  margin-right: .5em;\n  text-align: justify;\n}\n\np {\n  font-family: serif;\n  font-size: 10pt;\n  text-align: justify;\n  text-indent: 1em;\n  margin-top: 0px;\n  margin-bottom: 1ex;\n}\n\nh1, h2 {\n  font-family: sans-serif;\n  font-style: italic;\n  text-align: center;\n  background-color: #6b879c;\n  color: white;\n  width: 100%;\n}\n\nh1 {\n    margin-bottom: 2px;\n}\n\nh2 {\n    margin-top: -2px;\n    margin-bottom: 2px;\n}\n";
$log->logLine("Add css");
$book->addCSSFile("styles.css", "css1", $cssData);
// This test requires you have an image, change "demo/cover-image.jpg" to match your location.
$log->logLine("Add Cover Image");
コード例 #2
0
ファイル: epub.php プロジェクト: YesWiki/yeswiki-sandstorm
 include_once "tools/tags/libs/tags.functions.php";
 include_once "tools/tags/libs/vendor/PHPePub/EPub.php";
 $book = new EPub();
 // Title and Identifier are mandatory!
 $book->setTitle($metadatas["ebook-title"]);
 $book->setIdentifier($this->href('', $this->getPageTag()), EPub::IDENTIFIER_URI);
 // Could also be the ISBN number, prefered for published books, or a UUID.
 $book->setLanguage($this->config["lang"]);
 // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc.
 $book->setDescription($metadatas["ebook-description"]);
 $book->setAuthor($metadatas["ebook-author"], $metadatas["ebook-author-biblio"]);
 $book->setPublisher($metadatas["ebook-author"], $this->href('', $this->getPageTag()));
 // I hope this is a non existant address :)
 $book->setDate(time());
 // Strictly not needed as the book date defaults to time().
 $book->setRights(_t('TAGS_PUBLISHED_UNDER_CREATIVE_COMMONS_BY_SA'));
 // As this is generated, this _could_ contain the name or licence information of the user who purchased the book, if needed. If this is used that way, the identifier must also be made unique for the book.
 $book->setSourceURL($this->href('', $this->getPageTag()));
 // on concatene tous les styles dans un css
 $styles = $this->Format("{{linkstyle}}");
 preg_match_all('/href="(.*css)"/U', $styles, $matches);
 $cssData = '';
 foreach ($matches[1] as $key => $value) {
     $cssData .= file_get_contents($value);
 }
 $book->addCSSFile("styles.css", "css1", $cssData);
 // This test requires you have an image, change "demo/cover-image.jpg" to match your location.
 $book->setCoverImage("Cover.jpg", file_get_contents($metadatas["ebook-cover-image"]), "image/jpeg");
 // Titre et courte description de l'ouvrage
 $cover = $content_start . '<h1>' . $metadatas["ebook-title"] . '</h1>' . "\n" . '<h2>' . _t('TAGS_BY') . ': ' . $metadatas["ebook-author"] . '</h2>' . "\n" . $metadatas["ebook-description"] . $bookEnd;
 $book->addChapter(_t('TAGS_ABOUT_THIS_EBOOK'), "Cover.html", $cover);
コード例 #3
0
 /**
  * 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);
 }