print_epub_phpepub.pages.inc 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * @file
  4. * Generates the EPUB version using PHPePub.
  5. *
  6. * This file is included by the print_epub_phpepub module and includes the
  7. * functions that interface with the PHPePub library.
  8. *
  9. * @ingroup print
  10. */
  11. /**
  12. * Implements hook_print_epub_generate().
  13. */
  14. function print_epub_phpepub_print_epub_generate($html, $meta, $filename = NULL) {
  15. global $language, $base_url;
  16. module_load_include('inc', 'print', 'includes/print');
  17. $epub_tool = explode('|', variable_get('print_epub_epub_tool', PRINT_EPUB_EPUB_TOOL_DEFAULT));
  18. $images_via_file = variable_get('print_epub_images_via_file', PRINT_EPUB_IMAGES_VIA_FILE_DEFAULT);
  19. $tool_path = DRUPAL_ROOT . '/' . $epub_tool[1];
  20. if (file_exists($tool_path)) {
  21. require_once $tool_path;
  22. }
  23. else {
  24. watchdog('print_epub', 'Configured EPUB tool does not exist at path: %path', array('%path' => $tool_path), WATCHDOG_ERROR);
  25. throw new Exception("Configured EPUB tool does not exist, unable to generate EPUB.");
  26. }
  27. // Try to use local file access for image files.
  28. $html = _print_access_images_via_file($html, $images_via_file);
  29. $version = _print_epub_phpepub_version($epub_tool[1]);
  30. // Set document information.
  31. if (version_compare($version, '4.0.0', '>=')) {
  32. $epub = new \PHPePub\Core\EPub();
  33. }
  34. else {
  35. $epub = new EPub();
  36. }
  37. $epub->setTitle(html_entity_decode($meta['title'], ENT_QUOTES, 'UTF-8'));
  38. $epub->setIdentifier($meta['url'], $epub::IDENTIFIER_URI);
  39. $epub->setLanguage($language->language);
  40. if (isset($meta['name'])) {
  41. $epub->setAuthor(strip_tags($meta['name']), strip_tags($meta['name']));
  42. }
  43. $epub->setPublisher(variable_get('site_name', 'Drupal'), $base_url);
  44. $epub->setSourceURL($meta['url']);
  45. @$epub->addChapter("Chapter", "epub.html", $html, FALSE);
  46. // Finalize the book, and build the archive.
  47. $epub->finalize();
  48. // Close and output EPUB document.
  49. $epub->sendBook(empty($filename) ? 'page' : $filename);
  50. return TRUE;
  51. }