Пример #1
0
error_reporting(E_ALL | E_STRICT);
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 1);
// setting timezone for time functions used for logging to work properly
date_default_timezone_set('Europe/Berlin');
$log = new Logger("Example", TRUE);
$fileDir = './PHPePub';
// ePub 3 is not fully implemented. but aspects of it is, in order to help implementers.
// ePub 3 uses HTML5, formatted strictly as if it was XHTML but still using just the HTML5 doctype (aka XHTML5)
$book = new EPub(EPub::BOOK_VERSION_EPUB3, "en", EPub::DIRECTION_LEFT_TO_RIGHT);
// Default is ePub 2
$log->logLine("new EPub()");
$log->logLine("EPub class version.: " . EPub::VERSION);
$log->logLine("Zip version........: " . Zip::VERSION);
$log->logLine("getCurrentServerURL: " . URLHelper::getCurrentServerURL());
$log->logLine("getCurrentPageURL..: " . URLHelper::getCurrentPageURL());
// Title and Identifier are mandatory!
$book->setTitle("ePub 3 Test book");
$book->setIdentifier("http://JohnJaneDoePublications.com/books/TestBookEPub3.xhtml", EPub::IDENTIFIER_URI);
// Could also be the ISBN number, preferred 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 existent 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/TestBookEPub3.xhtml");
Пример #2
0
 /**
  * Check for mandatory parameters and finalize the e-book.
  * Once finalized, the book is locked for further additions.
  *
  * @return bool $success
  */
 function finalize()
 {
     if ($this->isFinalized || $this->chapterCount == 0 || empty($this->title) || empty($this->language)) {
         return false;
     }
     if (empty($this->identifier) || empty($this->identifierType)) {
         $this->setIdentifier(StringHelper::createUUID(4), EPub::IDENTIFIER_UUID);
     }
     if ($this->date == 0) {
         $this->date = time();
     }
     if (empty($this->sourceURL)) {
         $this->sourceURL = URLHelper::getCurrentPageURL();
     }
     if (empty($this->publisherURL)) {
         $this->sourceURL = URLHelper::getCurrentServerURL();
     }
     // Generate OPF data:
     $this->opf->setIdent("BookId");
     $this->opf->initialize($this->title, $this->language, $this->identifier, $this->identifierType);
     $DCdate = new DublinCore(DublinCore::DATE, gmdate($this->dateformat, $this->date));
     $DCdate->addOpfAttr("event", "publication");
     $this->opf->metadata->addDublinCore($DCdate);
     if (!empty($this->description)) {
         $this->opf->addDCMeta(DublinCore::DESCRIPTION, StringHelper::decodeHtmlEntities($this->description));
     }
     if (!empty($this->publisherName)) {
         $this->opf->addDCMeta(DublinCore::PUBLISHER, StringHelper::decodeHtmlEntities($this->publisherName));
     }
     if (!empty($this->publisherURL)) {
         $this->opf->addDCMeta(DublinCore::RELATION, StringHelper::decodeHtmlEntities($this->publisherURL));
     }
     if (!empty($this->author)) {
         $author = StringHelper::decodeHtmlEntities($this->author);
         $this->opf->addCreator($author, StringHelper::decodeHtmlEntities($this->authorSortKey), MarcCode::AUTHOR);
         $this->ncx->setDocAuthor($author);
     }
     if (!empty($this->rights)) {
         $this->opf->addDCMeta(DublinCore::RIGHTS, StringHelper::decodeHtmlEntities($this->rights));
     }
     if (!empty($this->coverage)) {
         $this->opf->addDCMeta(DublinCore::COVERAGE, StringHelper::decodeHtmlEntities($this->coverage));
     }
     if (!empty($this->sourceURL)) {
         $this->opf->addDCMeta(DublinCore::SOURCE, $this->sourceURL);
     }
     if (!empty($this->relation)) {
         $this->opf->addDCMeta(DublinCore::RELATION, StringHelper::decodeHtmlEntities($this->relation));
     }
     if ($this->isCoverImageSet) {
         $this->opf->addMeta("cover", "CoverImage");
     }
     if (!empty($this->generator)) {
         $gen = StringHelper::decodeHtmlEntities($this->generator);
         $this->opf->addMeta("generator", $gen);
         $this->ncx->addMetaEntry("dtb:generator", $gen);
     }
     if ($this->EPubMark) {
         $this->opf->addMeta("generator", "EPub (Version " . self::VERSION . ") by A. Grandt, http://www.phpclasses.org/package/6115 or https://github.com/Grandt/PHPePub/");
     }
     reset($this->ncx->chapterList);
     list($firstChapterName, $firstChapterNavPoint) = each($this->ncx->chapterList);
     /** @var $firstChapterNavPoint NavPoint */
     $firstChapterFileName = $firstChapterNavPoint->getContentSrc();
     $this->opf->addReference(Reference::TEXT, StringHelper::decodeHtmlEntities($firstChapterName), $firstChapterFileName);
     $this->ncx->setUid($this->identifier);
     $this->ncx->setDocTitle(StringHelper::decodeHtmlEntities($this->title));
     $this->ncx->referencesOrder = $this->referencesOrder;
     if ($this->isReferencesAddedToToc) {
         $this->ncx->finalizeReferences();
     }
     $this->finalizeTOC();
     if (!$this->isEPubVersion2()) {
         $this->addEPub3TOC("epub3toc.xhtml", $this->buildEPub3TOC());
     }
     $opfFinal = StringHelper::fixEncoding($this->opf->finalize());
     $ncxFinal = StringHelper::fixEncoding($this->ncx->finalize());
     if (mb_detect_encoding($opfFinal, 'UTF-8', true) === "UTF-8") {
         $this->zip->addFile($opfFinal, $this->bookRoot . "book.opf");
     } else {
         $this->zip->addFile(mb_convert_encoding($opfFinal, "UTF-8"), $this->bookRoot . "book.opf");
     }
     if (mb_detect_encoding($ncxFinal, 'UTF-8', true) === "UTF-8") {
         $this->zip->addFile($ncxFinal, $this->bookRoot . "book.ncx");
     } else {
         $this->zip->addFile(mb_convert_encoding($ncxFinal, "UTF-8"), $this->bookRoot . "book.ncx");
     }
     $this->opf = null;
     $this->ncx = null;
     $this->isFinalized = true;
     return true;
 }