Advertisement
isleshocky77

pdf_manager_print_ages

Feb 10th, 2012
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.20 KB | None | 0 0
  1. /**
  2.  * Implements the callback for print_pages
  3.  * @param string $response
  4.  */
  5. function gs_pdf_manager_print_pages() {
  6.  
  7.   $_SESSION['pdf_manager']['printing']['percentage'] = 0;
  8.  
  9.   module_load_include('inc', 'print_pdf', 'print_pdf.pages');
  10.   global $base_url;
  11.   $GLOBALS['conf']['cache'] = FALSE;
  12.  
  13.   $PdfPages = $_SESSION['pdf_manager']['pages'];
  14.   $PdfPages = isset($PdfPages) ? unserialize($PdfPages) : new PdfPages();
  15.  
  16.   $pages = $_POST['pages'];
  17.   $toc = $_POST['toc'];
  18.  
  19.   if(!isset($pages)) {
  20.     $pages = array();
  21.     foreach($PdfPages->getPages() as $PdfPage) {
  22.       $pages[$PdfPage->getId()] = 'print';
  23.     }
  24.     $toc = true;
  25.   }
  26.  
  27.   # Redirect if nothing to print
  28.   if(!count($pages)) {
  29.     header('Location: '.url('flag-management'));
  30.     exit;
  31.   }
  32.  
  33.   $document = '';
  34.  
  35.   # Add a table of contents
  36.   if($toc) {
  37.  
  38.     $content = '<ul>';
  39.     foreach($pages as $pdf_page_id => $do_print) {
  40.       $PdfPage = $PdfPages->find($pdf_page_id);
  41.  
  42.       if(!strlen($do_print)) {
  43. //        echo sprintf("Skipping %d: %s <%s><br/>", $pdf_page_id, $PdfPage->title, $PdfPage->link);
  44.        continue;
  45.      }
  46.      $content .= '<li>'.$PdfPage->title.'</li>';
  47.    }
  48.    $content .= '</ul>';
  49.  
  50.    $print['content'] = $content;
  51.  
  52.  
  53.    $node = new stdClass();
  54.    $node->path = 'table-of-contents';
  55.    $print['node'] = $node;
  56.  
  57.  
  58.    /**
  59.     * Taken from print.pages.inc :: _print_var_generator
  60.     */
  61.    global $base_url, $language, $_print_urls;
  62.    init_theme();
  63.    $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
  64.    $print['language'] = $language->language;
  65.    $print['title'] = check_plain($node->title);
  66.    $print['head'] = drupal_get_html_head();
  67.    if ($print_html_sendtoprinter) {
  68.      drupal_add_js('misc/drupal.js', 'core');
  69.    }
  70.    $print['scripts'] = drupal_get_js();
  71.    $print['robots_meta'] = _print_robots_meta_generator();
  72.    $print['base_href'] = "<base href='". $base_url ."' />\n";
  73.    $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='". theme_get_setting('favicon') ."' type='image/x-icon' />\n" : '';
  74.    drupal_add_css(strtr($print_css, array('%t' => path_to_theme())));
  75.    $drupal_css = drupal_add_css();
  76.    foreach ($drupal_css as $key => $types) {
  77.      $drupal_css[$key]['theme'] = array();
  78.    }
  79.    $print['css'] = drupal_get_css($drupal_css);
  80.  
  81.    $html = theme('print_page', $print, PRINT_PDF_FORMAT);
  82.    $document .= (strlen($document) ? '<div style="page-break-after:always;"></div>' : '') . $html;
  83.  
  84.    $_SESSION['pdf_manager']['printing']['percentage'] = 1 / count($pages) + ($toc ? 1 : 0);
  85.  }
  86.  
  87.  $count = 0;
  88.  foreach($pages as $pdf_page_id => $print) {
  89.    $PdfPage = $PdfPages->find($pdf_page_id);
  90.  
  91.    if(!strlen($print)) {
  92. //      echo sprintf("Skipping %d: %s <%s><br/>", $pdf_page_id, $PdfPage->title, $PdfPage->link);
  93.      continue;
  94.    }
  95.  
  96.    $path = preg_replace('/^\//', '', $PdfPage->link);
  97.    $_GET['q'] = 'printpdf/'.$path; # This is needed for blocks basing what to display off this
  98.  
  99.    # Beging the printing to PDF
  100.    $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
  101.  
  102.  
  103.    $print = print_controller($path, $cid, PRINT_PDF_FORMAT);
  104.    if ($print === FALSE) {
  105.      return;
  106.    }
  107.  
  108.    // Img elements must be set to absolute
  109.    $pattern = '!<(img\s[^>]*?)>!is';
  110.    $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']);
  111.    $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']);
  112.    $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']);
  113.  
  114.    // Send to printer option causes problems with PDF
  115.    $print['sendtoprinter'] = '';
  116.  
  117.    $node = $print['node'];
  118.    $html = theme('print_page', $print, PRINT_PDF_FORMAT, $node);
  119.  
  120.    // Convert the a href elements, to make sure no relative links remain
  121.    $pattern = '!<(a\s[^>]*?)>!is';
  122.    $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
  123.    // And make anchor links relative again, to permit in-PDF navigation
  124.    $html = preg_replace("!${base_url}/". PRINTPDF_PATH ."/.*?%2523!", '#', $html);
  125.  
  126. //    echo sprintf("Printing %d: %s &lt;%s&gt;<br/>", $pdf_page_id, $PdfPage->title, $path);
  127.    $document .= (strlen($document) ? '<div style="page-break-after:always;"></div>' : '') . $html;
  128.  
  129.    $count++;
  130.    $_SESSION['pdf_manager']['printing']['percentage'] = $count + ($toc ? 1 : 0) / count($pages) + ($toc ? 1 : 0);
  131.  }
  132.  
  133.  # For Debugging we can just print the html to the screen rather than creating a pdf
  134.  echo $document; exit;
  135.  
  136.  # Create PDF
  137.  $pdf_filename = sprintf('Example.org - %s.pdf', date("Y-m-d h:s"));
  138.  
  139.  if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
  140.    _print_pdf_dompdf($print, $document, $pdf_filename);
  141.  }
  142.  elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
  143.    _print_pdf_tcpdf($print, $document, $pdf_filename);
  144.  }
  145.  elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
  146.    _print_pdf_wkhtmltopdf($print, $document, $pdf_filename);
  147.  }
  148.  else {
  149.    return drupal_not_found();
  150.  }
  151.  
  152.  $_SESSION['pdf_manager']['printing']['percentage'] = 100;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement