6

I have an epub file which has n number of chapters. I have to copy or extract up to 1 chapter and make a new epub file which contains only 1 chapter in order to show a sample file which contains only one chapter to the user. If the user purchases the book I need to show the original book.

I know epub.js viewer, but how can I meet the above requirement with PHP and jQuery/JavaScript and also with any other language like .net, Java, or Python, etc.? Any suggestions?

3
  • 1
    I'm unclear on what you're asking for--do you need a new file, or a single file that will display different amounts onf content based on whether or not someone has purchased it? The latter would only be possible on reading systems that support Javascript, which is not that common.
    – Tom
    Commented May 1, 2014 at 19:57
  • i just want to maintain the epub sample copy which contains only 3 chapters while the original may contain "n" number of chapters
    – acushla
    Commented May 30, 2014 at 13:04
  • Possible duplicate of How can I extract some chapters or sections from an ebook into a new one?
    – Sekhemty
    Commented May 7, 2017 at 23:22

1 Answer 1

3

An EPUB file consists of multiple entries in a ZIP file. To extract the first chapter, you have to first determine how you know where the second chapter starts, as this doesn't have to coincide with an entry in the ZIP file.

Essentially what you need to do is:

  1. Extract and analyse "META-INF/container.xml" to determine where is the root content file of the document which can be in the root of the EPUB, but also in a subdirectory)
  2. Walk over the items in the content file to start building up the first chapter and accumulate data until you hit the second chapter.
  3. On the way there you might have to take care of included images, cross references and the like.

Once you have gathered this data, write out the first chapter into a new file. Care should be taken this is a valid EPUB file, that adheres to the restrictions on EPUB entry sizes that some older readers impose (i.e. split the HTML for the first chapter in small enough chunks). Any language with a good XML library (including namespaces), HTML parsing and a handling zip files should do the job. I tend to use Python for that.

3
  • Thanks For the solution,is parsing the opf is the only way?it seems a difficult task to parse the file and get the required chapter and its related images and title and cover and again we need to copy in new folder in order show the sample? is there any other any scripts in google it would be a great help?and can you please share your code
    – acushla
    Commented May 30, 2014 at 13:02
  • @acushla If you only have to do one file, you can of course use an editor (e.g. the one that comes with Calibre): just copy the file and delete everything that is unneeded. But for doing multiple files in a batch, without user interaction some programming is required. I don't know about existing scripts doing that, but I estimate it would take me 1-2 hours of programming to do this in Python, something only worth the effort if having to use it multiple times.
    – Anthon
    Commented May 30, 2014 at 13:38
  • Thanks Fro the reply i just wnt to write the script in core php,need to integrate in the website which consists of epub books
    – acushla
    Commented May 30, 2014 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.