Sunday, February 13, 2011

Epub Reader

After a lot of search, i finally found a solution to read ePub files for iphone.Few months before i had posted a question in stackerflow.com about a reader for ePub files in iphone.So many peoples answered with their ideas.From the various answers  got from there i finally created a reader for ePub.

Here are the steps to create an ePub reader.



  1. Create a view with a UIWebView
  2. Download the EPUB file
  3. Unzip it to a subdirectory in your app's documents folder 
  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 
    • (c) Look up that id in the NSDictionary and you'll get an href.
    • (d) 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.

Thanks to Euan(Stackoverflow.com)


You can download the sample code from here


43 comments:

  1. Thanks so much for your brilliant work. You save my life.

    ReplyDelete
  2. Great exemple....
    Very clearfull and descriptive.
    I used it with instruments and shows 38 mem leak... Do you know how to solve it??
    Thanks!! :)

    ReplyDelete
  3. Thank you so much. Saved me a lot of time.

    ReplyDelete
  4. Wow, Great tutorial. Is there a way to include a search a word capability? Thanks in advance

    ReplyDelete
  5. @Sreelal: I am getting strange behaviour of this ePub reader..It automatically displays all fonts in BOLD..Can you tell me the solution??

    ReplyDelete
  6. This puts me a step ahead. Thanks!

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. maybe you can add this code to swipe the page
    UISwipeGestureRecognizer *rightSwipeRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gotoNextPage)] autorelease];
    [rightSwipeRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];

    UISwipeGestureRecognizer *leftSwipeRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(gotoPrevPage)] autorelease];
    [leftSwipeRecognizer setDirection:UISwipeGestureRecognizerDirectionRight];
    [_webview addGestureRecognizer:rightSwipeRecognizer];
    [_webview addGestureRecognizer:leftSwipeRecognizer];

    Thanks this tutorial , it help me a lot !
    PS.How to read the book not using scroll down ?I think like page control is batter than scroll down reading : )

    ReplyDelete
  9. Is there a way to include a search a word capability? Thanks in advance

    ReplyDelete
  10. To Ronak :Maybe you can check this sample
    https://github.com/fedefrappi/AePubReader

    ReplyDelete
    Replies
    1. Thanks a alot sir

      sometimes i think why to share my code on net when i put so much efforts on it for months, but seeing such codes i realized there is nothing wrong in sharing our code with other developers it really helps .

      Delete
  11. Respected Sir,
    here i am using the sample for parsing the epub as "https://github.com/fedefrappi/AePubReader "

    But how can i bookmark the page while changing the font size as well as the font family

    Kindly help Me Sir...

    ReplyDelete
    Replies
    1. Did you get the answer of this if yes then please can you share

      Delete
    2. No, still i didnt receive any answers , if you have any ideas means kindly share with me

      Delete
  12. Thanks for wonder full tutorial it was very useful for me but as I am new to XPATH, i need small help.
    Here is my scenario -

    Let suppose i have already

    href = @"abcdef1 "

    but inside my toc.ncx i have an entry that is "abcdef1#tocmarker-1" corresponding to href of .opf

    I have to find the node for "abcdef1#tocmarker-1"

    using function
    - (void) parseOPF:(NSString*)opfPath- (void) parseOPF:(NSString*)opfPath
    {
    NSString* xpath = [NSString stringWithFormat:@"//ncx:content[@src='%@']/../ncx:navLabel/ncx:text", href];
    NSArray* navPoints = [ncxToc nodesForXPath:x namespaceMappings:[NSDictionary dictionaryWithObject:@"http://www.daisy.org/z3986/2005/ncx/"


    }

    how can i get node by nodesForXPath in above function for , src="abcdef1#tocmarker-1"

    I am not sure how it can be done in objective C.
    If possible please help me

    ReplyDelete
    Replies
    1. Dear Sandeep.

      I am also struggle with the same issue., pls share the idea with me if you got any solution ..

      Thanks.,

      Manik.,

      Delete
  13. Respected Sir,
    how can we implement the pagecurl animation in similar to ibook, kindly help me sir

    ReplyDelete
  14. How to mock the same steps for the the Android? Any idea?

    Thanks!

    ReplyDelete
  15. How to add bookmarks and highlighting in epub

    ReplyDelete
    Replies
    1. Hey did u get ny soln 4 dis...I m workin on same prjct but on Android & I m facing d same issues....do tell me if u get ny soln !!!

      Delete
    2. @Nishant - i am also trying to implement an android epub using monocle js library. It is working good in mobiles where as in Tablets it is not working properly.

      Could you suggest any other library for android.

      Delete
    3. Well, I am afraid I have no idea regarding this. I am a student & a beginner in Android.

      & by the way did you get any way for highlighting text permanently?

      Delete
  16. i downloaded this project, but m getting this errors,

    ld: warning: ignoring file /Users/krunal/Downloads/EpubReader/libz.1.2.3.dylib, missing required architecture i386 in file
    Undefined symbols for architecture i386:
    "_inflateEnd", referenced from:
    _unzCloseCurrentFile in unzip.o

    "_inflateInit2_", referenced from:
    _unzOpenCurrentFile3 in unzip.o

    "_get_crc_table", referenced from:
    _unzOpenCurrentFile3 in unzip.o
    _zipOpenNewFileInZip3 in zip.o

    "_crc32", referenced from:
    _unzReadCurrentFile in unzip.o
    _zipWriteInFileInZip in zip.o
    -[ZipArchive addFileToZip:newname:] in ZipArchive.o

    "_inflate", referenced from:
    _unzReadCurrentFile in unzip.o

    "_deflateInit2_", referenced from:
    _zipOpenNewFileInZip3 in zip.o

    "_deflate", referenced from:
    _zipWriteInFileInZip in zip.o
    _zipCloseFileInZipRaw in zip.o

    "_deflateEnd", referenced from:
    _zipCloseFileInZipRaw in zip.o

    ld: symbol(s) not found for architecture i386
    clang: error: linker command failed with exit code 1 (use -v to see invocation)


    how to solve this any idea ?

    ReplyDelete
    Replies
    1. If you're attempting to build/run for iOS 5.x+, then try removing the 1.2.3 library and adding 1.2.5 in the 'Linked Frameworks and Libraries' section of the project target summary.

      Delete
    2. thank you it's working :)

      Delete
  17. Hi friends,

    This tutorial and sample code is very useful for me..

    Thank u so much for ur great effort..

    Please anyone guide me about,

    1.How can i find the page size and no.of pages in each chapter?

    2.How to setup horizontal scroll instead of vertical?

    Please anyone help me..

    Thanks in advance..

    ReplyDelete
  18. Hi. this is very great tutorial..

    I'm using https://github.com/fedefrappi/AePubReader for my application.. My client requirement is to show two pages in ipad landscape mode.. like iBook application.. does any one have idea to implement.. Please share me link.. or mail me to bhilwadi.madhu@gmail.com . thanks

    ReplyDelete
  19. I have problem like fueqotusecog.
    How can I resolve this error.

    Thanks

    ReplyDelete
  20. Hi..

    This is very good example. but I want some more features in it. Any body know how to add annotation to this library.

    Thanks In advance. Please help me out.

    ReplyDelete
  21. Hi...
    I am working with custom epub book reader where I have a book.html file as a book and a book.properties and content.toc file I want to create a book (book.html) reader using UIWebView with pagination with all the features of book reader. Please help me how can I implement this ? I have googled lots of search but unable to find any good tutorial which read a single html file with pagination and all the features.

    ReplyDelete
  22. hello its really gr8 app...But m having one prblem...actually m downloading epub file from server in my app's document directory but my epub is not opening...i hav used your code for open it ...but UnzippedEpub folder is not creating in app's document folder..pls help me for this issue...

    ReplyDelete
  23. I'm getting an error on XMLHandler.m file at [_parser parse]; line, can any one solve this problem. thanks in advance.

    ReplyDelete
  24. hi guys,
    i need epub reader for arabic, can anyone help me ? if so then please send me code by email : umair2071@ymail.com.
    i will be very thankful :)

    ReplyDelete
  25. @Zahid Hossain,
    replace your code with this it will work perfect,

    dispatch_queue_t reentrantAvoidanceQueue = dispatch_queue_create("reentrantAvoidanceQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(reentrantAvoidanceQueue, ^{
    _parser=[[NSXMLParser alloc] initWithContentsOfURL:[NSURL fileURLWithPath:strPath]];
    _parser.delegate=self;
    if (![_parser parse]) {
    NSLog(@"There was an error=%@ parsing the xml. with data %@", [_parser parserError], [[NSString alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:strPath]] encoding: NSASCIIStringEncoding]);
    }
    //[_parser release];
    });
    dispatch_sync(reentrantAvoidanceQueue, ^{ });

    ReplyDelete
  26. hi guys,
    i need epub reader for arabic, can anyone help me ? if so then please send me code by email : umair2071@ymail.com.
    i will be very thankful :)

    ReplyDelete
  27. I have to bookmark page or highlight text, can you guide how I can do this ?
    Thanks

    ReplyDelete
  28. Showing ld: library not found for -lz.1.2.3
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    ReplyDelete