Usage:
include("MOBIClass/MOBI.php");
$mobi = new MOBI();
Then use one of the following ways to prepare information (it should be in the form of valid html)
$mobi->setInternetSource($url); //Load URL, the result will be cleaned using a Readability port
$mobi->setFileSource($file); //Load a local file without any extra changes
$mobi->setData($data); //Load data
If you want, you can set some optional settings (see Settings.php for all recognized settings)
$options = array(
"title"=>"Insert title here",
"author"=>"Author"
);
$mobi->setOptions($options);
Then there are two ways to output it:
$mobi->save($file); //Save the file locally
$mobi->download($name); //Let the client download the file, make sure the page
that calls it doesn't output anything, otherwise it might
conflict with the download. $name contains the file name,
usually something like "title.mobi" (where the title should
be cleaned so as not to contain illegal characters).
/** * 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); }
//If handler not found or if the recognition was turned off $content = new OnlineArticle($url); } $mobi->setContentProvider($content); //Get title and make it a 12 character long filename $title = $mobi->getTitle(); if ($title === false) { $title = 'file'; } $title = urlencode(str_replace(' ', '_', strtolower(substr($title, 0, 12)))); //Send the mobi file as download $mobi->download($title . '.mobi'); die; } else { //Create the mobi object $mobi = new MOBI(); $content = new MOBIFile(); $content->set('title', 'My first eBook'); $content->set('author', 'Me'); $content->appendChapterTitle('Introduction'); for ($i = 0, $lenI = rand(5, 10); $i < $lenI; ++$i) { $content->appendParagraph('P' . ($i + 1)); } //Based on PHP's imagecreatetruecolor help paage $im = imagecreatetruecolor(220, 200); $text_color = imagecolorallocate($im, 233, 14, 91); imagestring($im, 10, 5, 5, 'A Simple Text String', $text_color); imagestring($im, 5, 15, 75, 'A Simple Text String', $text_color); imagestring($im, 3, 25, 125, 'A Simple Text String', $text_color); imagestring($im, 2, 10, 155, 'A Simple Text String', $text_color); $content->appendImage($im);
if (!empty($content) && !empty($title)) { $title = formatTitle($title, $i); $filename = "Chapter" . ($i + 1) . ".html"; $book->addChapter($title, $filename, $content_start . "<h2>{$title}</h2>" . $content . $bookEnd, true, EPub::EXTERNAL_REF_ADD); } } $book->finalize(); $filename = $uniqid . "_" . $story["title"] . " - " . $story["author"] . ".epub"; $book->saveBook($filename, "./tmp"); if (!empty($email)) { mailAttachment($filename, "./tmp/", $email, $uniqid); } } else { if ($format == "mobi") { include_once "mobi/MOBI.php"; $mobi = new MOBI(); $mobiContent = new MOBIFile(); $mobiContent->set("title", $story["title"]); $mobiContent->set("author", $story["author"]); $mobiContent->set("toc", true); $mobiContent->appendChapterTitle($story["title"]); $mobiContent->appendParagraph("by: " . $story["author"]); $mobiContent->appendPageBreak(); for ($i = 0; $i < $numChapter; $i++) { $title = isset($story["chapters"]["title"][$i]) ? $story["chapters"]["title"][$i] : ""; $content = isset($story["chapters"]["content"][$i]) ? $story["chapters"]["content"][$i] : ""; if (!empty($content) && !empty($title)) { $title = formatTitle($title, $i); $mobiContent->appendChapterTitle($title); $mobiContent->appendParagraph($content); $mobiContent->appendPageBreak();
/** * Use PHPMobi to dump a .mobi file. * * @return Response */ private function produceMobi() { $mobi = new \MOBI(); $content = new \MOBIFile(); /* * Book metadata */ $content->set('title', $this->title); $content->set('author', implode($this->authors)); $content->set('subject', $this->title); /* * Front page */ $content->appendParagraph($this->getExportInformation('PHPMobi')); if (file_exists($this->logoPath)) { $content->appendImage(imagecreatefrompng($this->logoPath)); } $content->appendPageBreak(); /* * Adding actual entries */ foreach ($this->entries as $entry) { $content->appendChapterTitle($entry->getTitle()); $content->appendParagraph($entry->getContent()); $content->appendPageBreak(); } $mobi->setContentProvider($content); // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9 $this->title = preg_replace('/[^A-Za-z0-9\\-]/', '', $this->title); return Response::create($mobi->toString(), 200, ['Accept-Ranges' => 'bytes', 'Content-Description' => 'File Transfer', 'Content-type' => 'application/x-mobipocket-ebook', 'Content-Disposition' => 'attachment; filename="' . $this->title . '.mobi"', 'Content-Transfer-Encoding' => 'binary']); }
/** * MOBI Class * @author Sander Kromwijk */ public function produceMobi() { try { Tools::logm('Starting to produce Mobi file'); $mobi = new MOBI(); $content = new MOBIFile(); $messages = new Messages(); // for later Tools::logm('Filling metadata for Mobi...'); $content->set("title", $this->bookTitle); $content->set("author", $this->author); $content->set("subject", $this->bookTitle); # introduction $content->appendParagraph('<div style="text-align:center;" ><p>' . _('Produced by wallabag with PHPMobi') . '</p><p>' . _('Please open <a href="https://github.com/wallabag/wallabag/issues" >an issue</a> if you have trouble with the display of this E-Book on your device.') . '</p></div>'); $content->appendImage(imagecreatefrompng("themes/_global/img/appicon/apple-touch-icon-152.png")); $content->appendPageBreak(); Tools::logm('Adding actual content...'); foreach ($this->entries as $item) { $content->appendChapterTitle($item['title']); $content->appendParagraph($item['content']); $content->appendPageBreak(); } $mobi->setContentProvider($content); // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9 $this->bookFileName = preg_replace('/[^A-Za-z0-9\\-]/', '', $this->bookFileName); // we offer file to download $mobi->download($this->bookFileName . '.mobi'); Tools::logm('Mobi file produced'); } catch (Exception $e) { Tools::logm('PHPMobi has encountered an error : ' . $e->getMessage()); $this->wallabag->messages->add('e', $e->getMessage()); } }