4

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?

  • 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 May 1 '14 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 May 30 '14 at 13:04
2

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.

  • 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 May 30 '14 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 May 30 '14 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 May 30 '14 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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