You are here

function epub_create_file in Epub 6

Generate an ePub file from a book.

Parameters

$nid: An integer containing book node id.

Return value

If a book structure exists, send the ePub file to the user else set an error message and redirect to the node page.

1 call to epub_create_file()
epub_read_page_callback in ./epub.module
Determine whether the user has to create the ePub or can download it.

File

./epub.module, line 432
Provide ePub content type and enable the creation of ePub files from book contents.

Code

function epub_create_file($nid) {
  $epub_library_path = 'sites/all/libraries/epub';
  require_once "{$epub_library_path}/EPubChapterSplitter.php";
  require_once "{$epub_library_path}/EPub.php";
  $epub_nid = _epub_links_book($nid);
  if (!$epub_nid) {
    drupal_set_message(t('Error during the creation of the ePub'), 'error');
    drupal_goto("node/{$nid}");
  }
  $epub_node = node_load(array(
    'nid' => $epub_nid,
  ));
  $book_node = node_load(array(
    'nid' => $nid,
  ));
  $book_tree = _epub_get_book_tree($book_node);
  if (empty($book_tree)) {
    drupal_set_message(t('Error during the creation of the ePub'), 'error');
    drupal_goto("node/{$nid}");
  }
  $epub = new EPub();

  // Title and Identifier are mandatory!
  $epub
    ->setTitle($epub_node->title);

  // Could also be the ISBN, ISSN, UUID or others.
  $epub
    ->setIdentifier($epub_node->identifier ? $epub_node->identifier : "1234", "URI");

  // Language is mandatory, but EPub defaults to "en".

  //Use RFC3066 Language codes, such as "en", "da", "fr" etc.
  $epub
    ->setLanguage($epub_node->language_code);
  $epub
    ->setDescription($epub_node->body);
  $epub
    ->setAuthor($epub_node->author_name, $epub_node->author_name);
  $epub
    ->setPublisher($epub_node->publisher_name, $epub_node->publisher_site);

  // Strictly not needed as the book date defaults to time().
  $epub
    ->setDate(date('U', $epub_node->creation_date));

  // As this is generated, this _could_ contain the name or licence information
  // of the user who purchased the book, if needed. If this is used that way,
  // the identifier must also be made unique for the book.
  $epub
    ->setRights($epub_node->rights);
  $epub
    ->setSourceURL($epub_node->source_url);
  _epub_traverse_book_tree($book_tree, $epub);
  $epub
    ->setIgnoreEmptyBuffer(true);
  $epub
    ->finalize();

  // Finalize the book, and build the archive.
  $data = $epub
    ->getBook();
  $headers = array(
    'Pragma: public',
    'Last-Modified: ' . date('r', time()),
    'Expires: 0',
    'Accept-Ranges: bytes',
    'Connection: close',
    'Content-Type: application/epub+zip',
    'Content-Disposition: attachment; filename="' . $epub_node->title . '.epub";',
    'Content-Transfer-Encoding: binary',
    'Content-Length: "' . drupal_strlen($data),
  );
  _epub_file_transfer($data, $headers);
}