Intro + Validator and EPUB3

4 posts / 0 new
Last post


I am currently working on a content management system (not yet ready for public consumption) and I like to do things well. I was pulling my hair out at how to offer download of content to users for off-line reading, most places seem to do that with PDF but use PDF generation tools that have crappy typesetting, my plan was to write code to export html5 article to a LaTeX file and I was just pulling my hair out at how ugly and yucky that would be, and requiring a TeXLive install on a server seemed insane.

Then I discovered epub and this is definitely the right way to do it, especially with the proliferation of digital eBook readers out there. So I'm working on a php class that takes the users content (normally displayed on web page) and creates an epub 3 file. It's a natural fit for my existing code because I already do everything server side via XML and XML tools.

I suspect that soon epub is going to be the dominant way web applications allow their content to be downloaded and viewed off-line. PDF I think is on its way out for that purpose.

I'm having problems though with the validator -

For example:

ERROR -1 -1 Mimetype contains wrong type (application/epub+zip expected).

That's the exact mime type that is in my mimetype file, and when I unzip my .epub and diff my mimetype file against other .epub files I have, there is no difference.

The mimetype is the first file I add to the zip archive -

private function _initializeZip() {
$this->_epub = new ZipArchive();
$this->_epub->open($this->_zip, ZipArchive::OVERWRITE);
$this->_epub->addFromString('mimetype', 'application/epub+zip');
// more code

Any suggestions on what the issue may be?
What I'm worried about is that maybe the way php creates zip archives itself is somehow incompatible with the epub standard. Hopefully that's not the issue and it is something trivial, but the error message is not helpful since the exact mime type it says it is expecting is in fact what is in the archive.

If anyone could shed some light on this, I would be most appreciative. I'm kind of a standards nut and I want to make sure any .epub produced by my CMS does in fact validate and work in as many readers as possible.

Ouch, looks like php does not have the ability to specify compression level when adding files to a zip archive, despite it having been a feature request since 2007, so it will not be possible for me to do this in pure php. I may be able to create a template zip that has the mimetype file uncompressed and add the rest of the files to it, hopefully that won't result in the mimetype file being compressed, as it looks like that may be the issue at play.

The following template works, if anyone else is having the same issue:

private function _initializeZip() {
$this->_epub = new ZipArchive();
//$this->_epub->addFromString('mimetype', 'application/epub+zip');
//more code

Just be careful of line breaks the forum software might add to the base64 string.

Thanks for posting the solution! This is a candidate to make an FAQ answer... in hindsight the complexity of the way OCF uses ZIP (adopted from OpenDocument ODF format) was perhaps not the ideal tradeoff. We discussed removing in EPUB 3 but felt the reducing the ability of older Reading Systems in the market to handle new content was too high a price to pay. EPUB 3 does however relax one minor detail which is that the MIMETYPE file can now have trailing whitespace (existing EPUB 2 Reading Systems in the market could handle this).

Secondary menu