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: 241 All time: 8,024 This week: 46Up
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
Age: ???
All time rank: 4013107 in Spain Spain
Week rank: 178 Up6 in Spain Spain Up

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

  Files folder image Files (59)  /  legacy  
File Role Description
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file EPub.Test.Example.php Example Example script
  Accessible without login Plain text file install.bat Data Auxiliary data
  Accessible without login Plain text file install.sh Data Auxiliary data

  Files folder image Files (59)  /  src  
File Role Description
Files folder imagePHPePub (2 directories)
  Accessible without login HTML file lib.uuid.manual.html Doc. Documentation
  Plain text file lib.uuid.php Class Class source

  Files folder image Files (59)  /  src  /  PHPePub  
File Role Description
Files folder imageCore (4 files, 1 directory)
Files folder imageHelpers (8 files, 2 directories)

  Files folder image Files (59)  /  src  /  PHPePub  /  Core  
File Role Description
Files folder imageStructure (2 files, 2 directories)
  Plain text file EPub.php Class Class source
  Plain text file EPubChapterSplitter.php Class Class source
  Plain text file Logger.php Class Class source
  Plain text file StaticData.php Class Class source

  Files folder image Files (59)  /  src  /  PHPePub  /  Core  /  Structure  
File Role Description
Files folder imageNCX (3 files)
Files folder imageOPF (10 files)
  Plain text file Ncx.php Class Class source
  Plain text file Opf.php Class Class source

  Files folder image Files (59)  /  src  /  PHPePub  /  Core  /  Structure  /  NCX  
File Role Description
  Plain text file AbstractNavEntry.php Class Class source
  Plain text file NavMap.php Class Class source
  Plain text file NavPoint.php Class Class source

  Files folder image Files (59)  /  src  /  PHPePub  /  Core  /  Structure  /  OPF  
File Role Description
  Plain text file DublinCore.php Class Class source
  Plain text file Guide.php Class Class source
  Plain text file Item.php Class Class source
  Plain text file Itemref.php Class Class source
  Plain text file Manifest.php Class Class source
  Plain text file MarcCode.php Class Class source
  Plain text file Metadata.php Class Class source
  Plain text file MetaValue.php Class Class source
  Plain text file Reference.php Class Class source
  Plain text file Spine.php Class Class source

  Files folder image Files (59)  /  src  /  PHPePub  /  Helpers  
File Role Description
Files folder imageenums (1 file)
Files folder imageRendition (1 file)
  Plain text file CalibreHelper.php Class Class source
  Plain text file Enum.php Class Class source
  Plain text file FileHelper.php Class Class source
  Plain text file IBooksHelper.php Class Class source
  Plain text file ImageHelper.php Class Class source
  Plain text file MimeHelper.php Class Class source
  Plain text file StringHelper.php Class Class source
  Plain text file URLHelper.php Class Class source

  Files folder image Files (59)  /  src  /  PHPePub  /  Helpers  /  enums  
File Role Description
  Plain text file Boolean.php Class Class source

  Files folder image Files (59)  /  src  /  PHPePub  /  Helpers  /  Rendition  
File Role Description
  Plain text file RenditionHelper.php Class Class source

  Files folder image Files (59)  /  tests  
File Role Description
Files folder imagedemo (9 files)
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file EPub.Example1.php Example Example script
  Accessible without login Plain text file EPub.Example2.php Example Example script
  Accessible without login Plain text file EPub.Example2b.php Example Example script
  Accessible without login Plain text file EPub.Example3.php Example Example script
  Accessible without login Plain text file EPub.Example3_1.php Example Example script
  Accessible without login Plain text file EPub.Example3_2.php Example Example script
  Accessible without login Plain text file EPub.ExampleImg.php Example Example script
  Accessible without login Plain text file EPub.ExampleSimple.php Example Example script

  Files folder image Files (59)  /  tests  /  demo  
File Role Description
  Accessible without login Image file 512x700.jpg Data Auxiliary data
  Accessible without login Image file 512x700_1.jpg Data Auxiliary data
  Accessible without login Image file 512x700_2.jpg Data Auxiliary data
  Accessible without login Image file 512x700_3.jpg Data Auxiliary data
  Accessible without login Image file 512x700_g1.gif Data Auxiliary data
  Accessible without login Image file cover-image.jpg Data Auxiliary data
  Accessible without login Image file DemoInlineImage.jpg Data Auxiliary data
  Accessible without login Image file rxhVVUP.gif Icon Icon image
  Accessible without login Plain text file test.svg Data Auxiliary data

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:241
This week:0
All time:8,024
This week:46Up