Grandt / PHPePub

PHP Classes for dynamically generating EPub files.
http://www.phpclasses.org/package/6115
241 stars 83 forks source link

TOC showing up at end of created eBook #25

Open DawsonG opened 10 years ago

DawsonG commented 10 years ago

This maybe something I'm doing wrong, but I can't seem to figure it out. I'm using the following code to create a new epub file. I need to add the Table of Contents after the cover page but before the rest of the book. Instead I'm getting the whole book THEN the TOC.

Can you help? Thanks.

function epub_generate_rss($blogurl, $authorname = false) {
    $book = new EPub();
    $book->setTitle("test");
    if (!$authorname)
        $authorname = "Ima Author";

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

    $book->addCSSFile("styles.css", "css1", $epub_css);
    $cover = $epub_content_start . "<h1>" . $feed->get_title() . "</h1>\n";
    if ($authorname)
        $cover .= "<h2>By: $authorname</h2>\n";
    $cover .= "<h2>From: <a href=\"$blogurl\">$blogurl</a></h2>";
    $cover .= $epub_content_end;
    $book->addChapter("Notices", "Cover.html", $cover);
    $book->buildTOC();

        $book->addChapter(
           "Test", 
            "Chapter1.html", 
            $epub_content_start . "<h1Test Title</h1>\nPlenty of test content" . $epub_content_end
        );
    }

    return $book;
}
Grandt commented 10 years ago

Can I get you to send me an example of the defective epub, as I get the TOC where it's supposed to be, before the "test" chapter added after buildTOC.

The mail is php@grandt.com Here's the test code I made, building on your code. One problem could be the &stamp= in the URI, you have to URL encode that.

<?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");
DawsonG commented 10 years ago

Thanks for your quick response. I'll work on it tonight and get you some more information.

Edit: (Since github won't let me comment for some reason.) I used your code and emailed you the defective file. Please note that I'm opening the file using the latest version of Calibre to open the file.

DawsonG commented 10 years ago

Possibly related issue, the ToC lists itself.

Grandt commented 10 years ago

The $addToIndex should take care of that, though I'll take a look.

/**
 * Build the Table of Contents. This is not strictly necessary, as most eReaders will build it from the navigation structure in the .ncx file.
 *
 * @param string $cssFileName Include a link to this css file in the TOC html.
 * @param string $tocCSSClass The TOC is a <div>, if you need special formatting, you can add a css class for that div. Default is "toc".
 * @param string $title Title of the Table of contents. Default is "Table of Contents". Use this for ie. languages other than English.
 * @param bool   $addReferences include reference pages in the TOC, using the $referencesOrder array to determine the order of the pages in the TOC. Default is TRUE.
 * @param bool   $addToIndex Add the TOC to the NCX index at the current leve/position. Default is FALSE
 * @param string $tocFileName Change the default name of the TOC file. The default is "TOC.xhtml"
 */
function buildTOC($cssFileName = NULL, $tocCSSClass = "toc", $title = "Table of Contents", $addReferences = TRUE, $addToIndex = FALSE, $tocFileName = "TOC.xhtml") {
waylaidwanderer commented 9 years ago

I'm also having this problem. Have you found a fix for it?

Grandt commented 9 years ago

I think I found the error. I just committed a fix to the 4.0.0 branch

waylaidwanderer commented 9 years ago

I had the opportunity to test this just now, and the problem is unfortunately still happening.

Here's an example of a generated file: https://dl.dropboxusercontent.com/u/10793087/Black-Magic-Lunatic-Howling-in-the-Night.epub

bogdangainusa commented 4 years ago

Did someone managed to add TOC after first page and not at the end of document?

hatmandu commented 3 years ago

I have the same problem - it's a real shame as this stops the epub files from validating properly with epubcheck. Any ideas?

wohoksolutions commented 1 year ago

Found a solution for that - had the problem myself. Set the following:

$book->isReferencesAddedToToc = false;