PHP Classes

PHP ePub Generator Tool: Create ebook files dynamically in the EPub format

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 243 All time: 8,011 This week: 52Up
Version License PHP version Categories
phpepub 1.0.0The PHP License5PHP 5, Files and Folders, Content man...
Description 

Author

This package can create ebook files dynamically in the EPub format.

It can compose a ebook by adding HTML for each section of the publication.

After finalizing a ebook the class generates a ZIP archive that is the actual ebook in the ePub format.

Picture of Emilio Sanchez
Name: Emilio Sanchez <contact>
Classes: 1 package by
Country: Spain Spain

 

Example

<?php

   
use com\grandt\EPub;
    include_once(
"EPub.php");

   
error_reporting(E_ALL | E_STRICT);
   
ini_set('error_reporting', E_ALL | E_STRICT);
   
ini_set('display_errors', 1);

   
// Example.
    // Create a test book for download.
    // ePub uses XHTML 1.1, preferably strict.
   
$content_start =
   
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
   
. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
   
. " \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"
   
. "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
   
. "<head>"
   
. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
   
. "<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\" />\n"
   
. "<title>Test Book</title>\n"
   
. "</head>\n"
   
. "<body>\n";

   
$content_end = "</body>\n</html>\n";
   
$blogurl = "http://test.com/";
   
$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";

   
$book = new EPub();
   
$book->setTitle("test");

   
$authorname = "Ima Author";

   
$book->setAuthor($authorname, $authorname);
   
$book->setIdentifier($blogurl . "&amp;stamp=" . time(), EPub::IDENTIFIER_URI);
   
$book->setLanguage("en");

   
$book->addCSSFile("styles.css", "css1", $cssData);
   
$cover = $content_start . "<h1>" . "test" . "</h1>\n";
    if (
$authorname) {
       
$cover .= "<h2>By: $authorname</h2>\n";
    }

   
$cover .= "<h2>From: <a href=\"$blogurl\">$blogurl</a></h2>";
   
$cover .= $content_end;

   
$book->addChapter("Notices", "Cover.html", $cover);
   
$book->buildTOC();
   
$book->addChapter(
      
"Chapter 1",
       
"Chapter1.html",
       
$content_start . "<h1>Chapter 1</h1>\n<p>Plenty of test content</p>\n" . $content_end
   
);
   
$book->addChapter(
      
"Chapter 2",
       
"Chapter2.html",
       
$content_start . "<h1>Chapter 2</h1>\n<p>Plenty of test content</p>\n" . $content_end
   
);
   
$book->addChapter(
      
"Chapter 3",
       
"Chapter3.html",
       
$content_start . "<h1>Chapter 3</h1>\n<p>Plenty of test content</p>\n" . $content_end
   
);
   
$book->addChapter(
      
"Epilogue",
       
"Epilogue.html",
       
$content_start . "<h1>Epilogue</h1>\n<p>Plenty of test content</p>\n" . $content_end
   
);
   
$book->finalize();
   
$zipData = $book->sendBook("ExampleBook1_test");


Details

PHP ePub generator

PHPePub allows a php script to generate ePub Electronic books on the fly, and send them to the user as downloads.

PHPePub support most of the ePub 2.01 specification, and enough of the new ePub3 specification to make valid ePub 3 books as well.

The projects is also hosted on PHPClasses.org at the addresses: http://www.phpclasses.org/package/6115

PHPePub is meant to be easy to use for small projects, and still allow for comples and complete e-books should the need arise.

The Zip.php class in this project originates from http://www.phpclasses.org/package/6110

or on Github: git://github.com/Grandt/PHPZip.git

See the examples for example usage. The php files have "some" doumentation in them in the form of Javadoc style function headers.

Installation

Import

Add this requirement to your composer.json file:

    "grandt/phpepub": ">=4.0.3"

Composer

If you already have Composer installed, skip this part.

Packagist, the main composer repository has a neat and very short guide. Or you can look at the guide at the Composer site.

The easiest for first time users, is to have the composer installed in the same directory as your composer.json file, though there are better options.

Run this from the command line:

php -r "readfile('https://getcomposer.org/installer');" | php

This will check your PHP installation, and download the composer.phar, which is the composer binary. This file is not needed on the server though.

Once composer is installed you can create the composer.json file to import this package.

{
    "require": {
        "grandt/phpepub": ">=4.0.3",
        "php": ">=5.3.0"
    }
}

Followed by telling Composer to install the dependencies.

php composer.phar install

this will download and place all dependencies defined in your composer.json file in the vendor directory.

Finally, you include the autoload.php file in the new vendor directory.

<?php
    require 'vendor/autoload.php';
    .
    .
    .

TODO:

  • The goal being to encompass the majority of the features in the ePub 2.0 and 3.0 specifications, except the Daisy type files.
  • Add better handling of Reference structures.
  • Improve handling of media types and linked files.
  • A/V content is allowed, but definitely not recommended, and MUST have a fallback chain ending in a valid file. If no such chain is provided, the content should not be added.
  • Documentation, no one reads it, but everyone complains if it is missing.
  • Better examples to fully cover the capabilities of the EPub classes.
  • more TODO's.

  Files folder image Files (59)  
File Role Description
Files folder imagelegacy (4 files)
Files folder imagesrc (2 files, 1 directory)
Files folder imagetests (9 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file documentation.css Data Auxiliary data
Accessible without login HTML file ReadMe.html Doc. Documentation
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file REVISION.TXT Data Revision log
Accessible without login Plain text file test.php Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:243
This week:0
All time:8,011
This week:52Up