Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements the callback for print_pages
- * @param string $response
- */
- function gs_pdf_manager_print_pages() {
- $_SESSION['pdf_manager']['printing']['percentage'] = 0;
- module_load_include('inc', 'print_pdf', 'print_pdf.pages');
- global $base_url;
- $GLOBALS['conf']['cache'] = FALSE;
- $PdfPages = $_SESSION['pdf_manager']['pages'];
- $PdfPages = isset($PdfPages) ? unserialize($PdfPages) : new PdfPages();
- $pages = $_POST['pages'];
- $toc = $_POST['toc'];
- if(!isset($pages)) {
- $pages = array();
- foreach($PdfPages->getPages() as $PdfPage) {
- $pages[$PdfPage->getId()] = 'print';
- }
- $toc = true;
- }
- # Redirect if nothing to print
- if(!count($pages)) {
- header('Location: '.url('flag-management'));
- exit;
- }
- $document = '';
- # Add a table of contents
- if($toc) {
- $content = '<ul>';
- foreach($pages as $pdf_page_id => $do_print) {
- $PdfPage = $PdfPages->find($pdf_page_id);
- if(!strlen($do_print)) {
- // echo sprintf("Skipping %d: %s <%s><br/>", $pdf_page_id, $PdfPage->title, $PdfPage->link);
- continue;
- }
- $content .= '<li>'.$PdfPage->title.'</li>';
- }
- $content .= '</ul>';
- $print['content'] = $content;
- $node = new stdClass();
- $node->path = 'table-of-contents';
- $print['node'] = $node;
- /**
- * Taken from print.pages.inc :: _print_var_generator
- */
- global $base_url, $language, $_print_urls;
- init_theme();
- $print_css = variable_get('print_css', PRINT_CSS_DEFAULT);
- $print['language'] = $language->language;
- $print['title'] = check_plain($node->title);
- $print['head'] = drupal_get_html_head();
- if ($print_html_sendtoprinter) {
- drupal_add_js('misc/drupal.js', 'core');
- }
- $print['scripts'] = drupal_get_js();
- $print['robots_meta'] = _print_robots_meta_generator();
- $print['base_href'] = "<base href='". $base_url ."' />\n";
- $print['favicon'] = theme_get_setting('toggle_favicon') ? "<link rel='shortcut icon' href='". theme_get_setting('favicon') ."' type='image/x-icon' />\n" : '';
- drupal_add_css(strtr($print_css, array('%t' => path_to_theme())));
- $drupal_css = drupal_add_css();
- foreach ($drupal_css as $key => $types) {
- $drupal_css[$key]['theme'] = array();
- }
- $print['css'] = drupal_get_css($drupal_css);
- $html = theme('print_page', $print, PRINT_PDF_FORMAT);
- $document .= (strlen($document) ? '<div style="page-break-after:always;"></div>' : '') . $html;
- $_SESSION['pdf_manager']['printing']['percentage'] = 1 / count($pages) + ($toc ? 1 : 0);
- }
- $count = 0;
- foreach($pages as $pdf_page_id => $print) {
- $PdfPage = $PdfPages->find($pdf_page_id);
- if(!strlen($print)) {
- // echo sprintf("Skipping %d: %s <%s><br/>", $pdf_page_id, $PdfPage->title, $PdfPage->link);
- continue;
- }
- $path = preg_replace('/^\//', '', $PdfPage->link);
- $_GET['q'] = 'printpdf/'.$path; # This is needed for blocks basing what to display off this
- # Beging the printing to PDF
- $print_pdf_pdf_tool = variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
- $print = print_controller($path, $cid, PRINT_PDF_FORMAT);
- if ($print === FALSE) {
- return;
- }
- // Img elements must be set to absolute
- $pattern = '!<(img\s[^>]*?)>!is';
- $print['content'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['content']);
- $print['logo'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['logo']);
- $print['footer_message'] = preg_replace_callback($pattern, '_print_rewrite_urls', $print['footer_message']);
- // Send to printer option causes problems with PDF
- $print['sendtoprinter'] = '';
- $node = $print['node'];
- $html = theme('print_page', $print, PRINT_PDF_FORMAT, $node);
- // Convert the a href elements, to make sure no relative links remain
- $pattern = '!<(a\s[^>]*?)>!is';
- $html = preg_replace_callback($pattern, '_print_rewrite_urls', $html);
- // And make anchor links relative again, to permit in-PDF navigation
- $html = preg_replace("!${base_url}/". PRINTPDF_PATH ."/.*?%2523!", '#', $html);
- // echo sprintf("Printing %d: %s <%s><br/>", $pdf_page_id, $PdfPage->title, $path);
- $document .= (strlen($document) ? '<div style="page-break-after:always;"></div>' : '') . $html;
- $count++;
- $_SESSION['pdf_manager']['printing']['percentage'] = $count + ($toc ? 1 : 0) / count($pages) + ($toc ? 1 : 0);
- }
- # For Debugging we can just print the html to the screen rather than creating a pdf
- echo $document; exit;
- # Create PDF
- $pdf_filename = sprintf('Example.org - %s.pdf', date("Y-m-d h:s"));
- if (basename($print_pdf_pdf_tool) == 'dompdf_config.inc.php') {
- _print_pdf_dompdf($print, $document, $pdf_filename);
- }
- elseif (basename($print_pdf_pdf_tool) == 'tcpdf.php') {
- _print_pdf_tcpdf($print, $document, $pdf_filename);
- }
- elseif (substr(basename($print_pdf_pdf_tool, '.exe'), 0, 11) == 'wkhtmltopdf') {
- _print_pdf_wkhtmltopdf($print, $document, $pdf_filename);
- }
- else {
- return drupal_not_found();
- }
- $_SESSION['pdf_manager']['printing']['percentage'] = 100;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement