Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading ePub format

Tags:

ios

iphone

epub

I am trying to develop an iPhone application to read ePub files. Is there any framework available to develop this? I have no idea about how to read this file format. I tried to parse a sample file with .epub extension using NSXML Parser, but that fails.

like image 800
MobX Avatar asked Sep 07 '09 09:09

MobX


People also ask

Can I open EPUB in PDF?

EPUB to PDF converter Sometimes you may need to preview e-books on more devices than Kindle. With a single click, you can turn your favorite electronic books into PDF files and use them everywhere.

Is it better to read PDF or EPUB?

EPUB files are flexible in that you can adjust certain aspects of the layout, including font size or color. PDF files, on the other hand, are more static. Font size cannot be changed, which makes reading on an e-reader much harder, or even impossible.


4 Answers

The EPUB format brings together a bunch of different specifications / formats:

  • one to say what the content of the book should look like (a subset of XHTML 1.1 + CSS)
  • one to define a "manifest" that lists all of the files that make up that content (OPF, which is an XML file)
  • one to define how everything is packaged up (OEBPS: a zip file of everything in the manifest plus a few extra files)

The specs look a bit daunting but actually once you've got the basics (unzipping, parsing XML) down it's not particularly difficult or complex.

You'll need to work out how to download the EPUB, to unzip it somewhere, to parse the manifest and then to display the relevant content.

Some pointers if you're just starting out:

  • parse xml
  • unzip

To display content just use a UIWebView for now.

Here's a high level step by step for your code:

1) create a view with a UIWebView

2) download the EPUB file

3) unzip it to a subdirectory in your app's documents folder using the zip library, linked above

4) parse the XML file at META-INF/container.xml (if this file doesn't exist the EPUB is invalid) using TBXML, linked above

5) In this XML, find the first "rootfile" with media-type application/oebps-package+xml. This is the OPF file for the book.

6) parse the OPF file (also XML)

7) now you need to know what the first chapter of the book is.

a) each <item> in the <manifest> element has an id and an href. Store these in an NSDictionary where the key is the id and the object is the href.

b) Look at the first <itemref> in the <spine>. It has an idref attribute which corresponds to one of the ids in (a). Look up that id in the NSDictionary and you'll get an href.

c) this is the the file of the first chapter to show the user. Work out what the full path is (hint: it's wherever you unzipped the zip file to in (3) plus the base directory of the OPF file in (6))

8) create an NSURL using fileURLWithPath:, where the path is the full path from (7c). Load this request using the UIWebView you created in (1).

You'll need to implement forward / backward buttons or swipes or something so that users can move from one chapter to another. Use the <spine> to work out which file to show next - the <itemrefs> in the XML are in the order they should appear to the reader.

like image 181
Euan Avatar answered Oct 11 '22 09:10

Euan


Apparently EPUB is "just" an XML format, so if you have an xml parser and the spec it should be okay.

Plus a little tuto? Have fun!

EDIT: you could also read some code here, this is for generating epub, not reading them but the code may be useful.

EDIT again: And see links to related question in the right sidebar, there are some links in the answers to free ebook reader which support ePub.


EDIT 3: You should add a comment when you edit your question so people who answer you can continue the discussion (if you don't comment we're not noticed of your edit).

So, The parsing fail because you didn't read the spec or related questions on Stack Overflow... *.epub file are a zipped folder containing XML file(s), not plain xml.

like image 33
p4bl0 Avatar answered Oct 11 '22 10:10

p4bl0


I read through this tutorial once (free registration required, sorry) and it gave me a great introduction to ePub. deverloperWorks tutorial here

I highly suggest you look at some of the XML processing libraries. If you just want to get specific information out of the XML file, then you can pick the right parsing strategy.

like image 6
yonkeltron Avatar answered Oct 11 '22 11:10

yonkeltron


there is an open source project fbreader,

it also support iphone

http://www.fbreader.org/about.php

like image 3
camino Avatar answered Oct 11 '22 10:10

camino