PHPePub\Core\EPub::addChapter PHP Method

addChapter() public method

These will still only show up as a single chapter in the book TOC.
public addChapter ( string $chapterName, string $fileName, string $chapterData = null, boolean $autoSplit = false, integer $externalReferences = EPub::EXTERNAL_REF_IGNORE, string $baseDir = "" ) : mixed
$chapterName string Name of the chapter, will be use din the TOC
$fileName string Filename to use for the chapter, must be unique for the book.
$chapterData string Chapter text in XHTML or array $chapterData valid XHTML data for the chapter. File should NOT exceed 250kB.
$autoSplit boolean Should the chapter be split if it exceeds the default split size? Default=FALSE, only used if $chapterData is a string.
$externalReferences integer How to handle external references, EPub::EXTERNAL_REF_IGNORE, EPub::EXTERNAL_REF_ADD or EPub::EXTERNAL_REF_REMOVE_IMAGES? See documentation for processChapterExternalReferences for explanation. Default is EPub::EXTERNAL_REF_IGNORE.
$baseDir string Default is "", meaning it is pointing to the document root. NOT used if $externalReferences is set to EPub::EXTERNAL_REF_IGNORE.
return mixed $success FALSE if the addition failed, else the new NavPoint.
    function addChapter($chapterName, $fileName, $chapterData = null, $autoSplit = false, $externalReferences = EPub::EXTERNAL_REF_IGNORE, $baseDir = "")
    {
        if ($this->isFinalized) {
            return false;
        }
        $fileName = RelativePath::getRelativePath($fileName);
        $fileName = preg_replace('#^[/\\.]+#i', "", $fileName);
        $navPoint = false;
        $chapter = $chapterData;
        if ($autoSplit && is_string($chapterData) && mb_strlen($chapterData) > $this->splitDefaultSize) {
            $splitter = new EPubChapterSplitter();
            $splitter->setSplitSize($this->splitDefaultSize);
            $chapterArray = $splitter->splitChapter($chapterData);
            if (count($chapterArray) > 1) {
                $chapter = $chapterArray;
            }
        }
        if (!empty($chapter) && is_string($chapter)) {
            if ($externalReferences !== EPub::EXTERNAL_REF_IGNORE) {
                $htmlDirInfo = pathinfo($fileName);
                $htmlDir = preg_replace('#^[/\\.]+#i', "", $htmlDirInfo["dirname"] . "/");
                $this->processChapterExternalReferences($chapter, $externalReferences, $baseDir, $htmlDir);
            }
            if ($this->encodeHTML === true) {
                $chapter = StringHelper::encodeHtml($chapter);
            }
            $this->chapterCount++;
            $this->addFile($fileName, "chapter" . $this->chapterCount, $chapter, "application/xhtml+xml");
            $this->extractIdAttributes("chapter" . $this->chapterCount, $chapter);
            $this->opf->addItemRef("chapter" . $this->chapterCount);
            $navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount);
            $this->ncx->addNavPoint($navPoint);
            $this->ncx->chapterList[$chapterName] = $navPoint;
        } elseif (is_array($chapter)) {
            $this->log->logLine("addChapter: \$chapterName: {$chapterName} ; \$fileName: {$fileName} ; ");
            $fileNameParts = pathinfo($fileName);
            $extension = $fileNameParts['extension'];
            $name = $fileNameParts['filename'];
            $partCount = 0;
            $this->chapterCount++;
            $oneChapter = each($chapter);
            while ($oneChapter) {
                /** @noinspection PhpUnusedLocalVariableInspection */
                list($k, $v) = $oneChapter;
                if ($this->encodeHTML === true) {
                    $v = StringHelper::encodeHtml($v);
                }
                if ($externalReferences !== EPub::EXTERNAL_REF_IGNORE) {
                    $this->processChapterExternalReferences($v, $externalReferences, $baseDir);
                }
                $partCount++;
                $partName = $name . "_" . $partCount;
                $this->addFile($partName . "." . $extension, $partName, $v, "application/xhtml+xml");
                $this->extractIdAttributes($partName, $v);
                $this->opf->addItemRef($partName);
                $oneChapter = each($chapter);
            }
            $partName = $name . "_1." . $extension;
            $navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $partName, $partName);
            $this->ncx->addNavPoint($navPoint);
            $this->ncx->chapterList[$chapterName] = $navPoint;
        } elseif (!isset($chapterData) && strpos($fileName, "#") > 0) {
            $this->chapterCount++;
            //$this->opf->addItemRef("chapter" . $this->chapterCount);
            $id = preg_split("/[#]/", $fileName);
            if (sizeof($id) == 2 && $this->isLogging) {
                $name = preg_split('/[\\.]/', $id[0]);
                if (sizeof($name) > 1) {
                    $name = $name[0];
                }
                $rv = $this->opf->getItemByHref($name, true);
                if ($rv != false) {
                    /** @var Item $item */
                    foreach ($rv as $item) {
                        if ($item->hasIndexPoint($id[1])) {
                            $fileName = $item->getHref() . "#" . $id[1];
                            break;
                        }
                    }
                }
            }
            $navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount);
            $this->ncx->addNavPoint($navPoint);
            $this->ncx->chapterList[$chapterName] = $navPoint;
        } elseif (!isset($chapterData) && $fileName == "TOC.xhtml") {
            $this->chapterCount++;
            $this->opf->addItemRef("toc");
            $navPoint = new NavPoint(StringHelper::decodeHtmlEntities($chapterName), $fileName, "chapter" . $this->chapterCount);
            $this->ncx->addNavPoint($navPoint);
            $this->ncx->chapterList[$chapterName] = $navPoint;
            $this->tocNavAdded = true;
        }
        return $navPoint;
    }

Usage Example

Beispiel #1
0
 /**
  * Use PHPePub to dump a .epub file.
  *
  * @return Response
  */
 private function produceEpub()
 {
     /*
      * Start and End of the book
      */
     $content_start = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" . '<head>' . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" . "<title>wallabag articles book</title>\n" . "</head>\n" . "<body>\n";
     $bookEnd = "</body>\n</html>\n";
     $book = new EPub(EPub::BOOK_VERSION_EPUB3);
     /*
      * Book metadata
      */
     $book->setTitle($this->title);
     // Could also be the ISBN number, prefered for published books, or a UUID.
     $book->setIdentifier($this->title, EPub::IDENTIFIER_URI);
     // 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->setLanguage($this->language);
     $book->setDescription('Some articles saved on my wallabag');
     foreach ($this->authors as $author) {
         $book->setAuthor($author, $author);
     }
     // I hope this is a non existant address :)
     $book->setPublisher('wallabag', 'wallabag');
     // Strictly not needed as the book date defaults to time().
     $book->setDate(time());
     $book->setSourceURL($this->wallabagUrl);
     $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
     $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
     /*
      * Front page
      */
     if (file_exists($this->logoPath)) {
         $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png');
     }
     $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd);
     $book->buildTOC();
     /*
      * Adding actual entries
      */
     // set tags as subjects
     foreach ($this->entries as $entry) {
         foreach ($entry->getTags() as $tag) {
             $book->setSubject($tag->getLabel());
         }
         // the reader in Kobo Devices doesn't likes special caracters
         // in filenames, we limit to A-z/0-9
         $filename = preg_replace('/[^A-Za-z0-9\\-]/', '', $entry->getTitle());
         $chapter = $content_start . $entry->getContent() . $bookEnd;
         $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
     }
     return Response::create($book->getBook(), 200, ['Content-Description' => 'File Transfer', 'Content-type' => 'application/epub+zip', 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"', 'Content-Transfer-Encoding' => 'binary']);
 }
All Usage Examples Of PHPePub\Core\EPub::addChapter