Advertisement
Guest User

Untitled

a guest
Oct 15th, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.12 KB | None | 0 0
  1. /**
  2.  * build the custom kit
  3.  */
  4. function serviceskit_build_kit($array_of_form_names) {
  5.     //define('FPDF_FONTPATH','sites/all/libraries/fpdf-i/font');
  6.     require_once('sites/all/libraries/pdf-gen/fpdf1.7/fpdf.php');
  7.   require_once('sites/all/libraries/pdf-gen/fpdi/fpdi.php');
  8.    
  9.     /* -----
  10.      *  build the array's with
  11.      *  file info and build
  12.      *  the command string for the passthru
  13.      * -----
  14.      */
  15.    
  16.     // the forms that are being requested
  17.   $forms = $array_of_form_names;
  18.     $file_ids = array();
  19.     $file_locations = array();
  20.    
  21.     foreach ($forms as $form) {
  22.       // get the file id
  23.         $file_ids[] = (int)str_replace('download-form-', '', $form);
  24.     }
  25.     $all_files = file_load_multiple($file_ids);
  26.     //_dvd($all_files);
  27.     $file_count = count($all_files);
  28.     //var_dump($all_files);
  29.     $command = '';
  30.     if (!empty($all_files)) {
  31.       // get started with pdf merge
  32.         // likely can be removed, no longer using
  33.         // passthru() due to linking issues
  34.         foreach ($all_files as $file) {
  35.             $url = file_create_url($file->uri);    
  36.             $command = $command. ' ' . $url;   
  37.         }
  38.         //
  39.     }
  40.    
  41.     /* -----
  42.      *  make the table of contents
  43.      * -----
  44.      */
  45.     $pdf = new FPDI('P','pt','A4');
  46.     $pdf->AddPage();
  47.     // in order to use fpdi we must pass a relative url
  48.     // this conflicts with what Drupal gives us to return
  49.     // the actual location of a public file, file_create_url(),
  50.     // we must make the url into a relative path
  51.    
  52.     $url = _absolute_to_relative(file_create_url('public://service_kit/TOC.pdf'));
  53.     $pdf->setSourceFile($url);
  54.     $tplIdx = $pdf->importPage(1);
  55.     //die($tplIdx);
  56.        
  57.     $pdf->useTemplate($tplIdx, 0, 0, 612, 792, TRUE);  
  58.     //_dvd($pdf->getTemplateSize($tplIdx));
  59.     $pdf->SetFont('Arial');
  60.   $pdf->SetTextColor(0,0,0);
  61.     $pdf->SetFontSize('10');
  62.    
  63.     // arrange the toc
  64.     // starting at 
  65.   $pdf->SetXY(12, 75);
  66.    
  67.     // General Services
  68.     //$pdf->Cell(50,5,'Shipping & Drayage',0,1,'L');
  69.     $genservice = serviceskit_get_forms_by_category(1);
  70.     $genservice = _is_form_requested_and_or_available($genservice, serviceskit_get_forms_by_id($file_ids));
  71.    
  72.     $kecservice = serviceskit_get_forms_by_category(2);
  73.     $kecservice = _is_form_requested_and_or_available($kecservice, serviceskit_get_forms_by_id($file_ids));
  74.    
  75.     $shipping = serviceskit_get_forms_by_category(3);
  76.     $shipping = _is_form_requested_and_or_available($shipping, serviceskit_get_forms_by_id($file_ids));
  77.    
  78.     $rules = serviceskit_get_forms_by_category(4);
  79.     $rules = _is_form_requested_and_or_available($rules, serviceskit_get_forms_by_id($file_ids));
  80.    
  81.     $other = serviceskit_get_forms_by_category(5);
  82.     $other = _is_form_requested_and_or_available($other, serviceskit_get_forms_by_id($file_ids));
  83.    
  84.     $promotion = serviceskit_get_forms_by_category(6);
  85.     $promotion = _is_form_requested_and_or_available($promotion, serviceskit_get_forms_by_id($file_ids));
  86.    
  87.     $addlinfo = serviceskit_get_forms_by_category(7);
  88.     $addlinfo = _is_form_requested_and_or_available($addlinfo, serviceskit_get_forms_by_id($file_ids));
  89.    
  90.     $page_counter = 0;
  91.    
  92.     $full_kit = array($genservice, $kecservice, $shipping, $rules, $other, $promotion, $addlinfo);
  93.     //_dvd($full_kit);
  94.     $internal_page_counter = 2;
  95.     // height increment
  96.     $hi = 5;
  97.     $width_tracker = 0;
  98.     $addon_forms = array();
  99.     foreach ($full_kit as $section) {
  100.         $_helper = prev($section);
  101.         // this gives section title if there are no forms
  102.         $section_title = serviceskit_get_category_name_from_id($_helper->category_id);
  103.         // section title if there are forms
  104.         if (!$section_title) {
  105.           $section_title = serviceskit_get_category_name_from_id($_helper->servicekit_form->category_id);
  106.         }
  107.         if ($_helper->category_id == 4) {
  108.             //_dvd($section);
  109.         }
  110.         if ($_helper->category_id == 2) {
  111.           $pdf->SetXY(85,75);
  112.         }
  113.         if ($_helper->category_id == 5) {
  114.           $pdf->SetXY(155,75);
  115.         }
  116.        
  117.         //$pdf->SetFontSize('11');
  118.         $pdf->SetTextColor(0,0,0);
  119.         $pdf->SetFont('Arial', '');
  120.         $pdf->Cell(50, 7, strtoupper($section_title), 0, 2, 'L', false);
  121.         $height_tracker = $height_tracker + $hi;
  122.         $pdf->SetFontSize('10');
  123.         foreach ($section as $form) {
  124.             // first if requested
  125.             if ($form->form_requested) {
  126.                 $pdf->SetTextColor(245,114,23); // orange
  127.                 $pdf->SetFont('Arial', 'U');
  128.                 if ($form->fid != 0) {
  129.                
  130.                     // these pdfs are going to be added
  131.                     // onto our document.  We need to set a
  132.                     // link also
  133.                     $addon_forms[$form->fid]['url'] = _absolute_to_relative(file_create_url($form->uri));
  134.                     $addon_link = $pdf->AddLink();
  135.                     $addon_forms[$form->fid]['link'] = $addon_link;
  136.                    
  137.                     $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $addon_link);
  138.                     $height_tracker = $height_tracker + $hi;
  139.                     unset($addon_link);
  140.                 }
  141.                 else {
  142.                   $link = $form->servicekit_form->form_url;
  143.                     $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $link);
  144.                     $height_tracker = $height_tracker + $hi;
  145.                 }
  146.                
  147.                
  148.                 continue;
  149.             }
  150.             // next if available
  151.             if (!($form->form_requested) && $form->form_available) {
  152.                 $pdf->SetTextColor(51,51,102); // purple
  153.                 $pdf->SetFont('Arial', 'U');
  154.               if ($form->fid != 0) {
  155.                 $link = file_create_url($form->uri);
  156.                     $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $link);
  157.                     $height_tracker = $height_tracker + $hi;
  158.                 }
  159.                 else {
  160.                   $link = $form->servicekit_form->form_url;
  161.                     $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $link);
  162.                     $height_tracker = $height_tracker + $hi;
  163.                 }
  164.                
  165.                 continue;
  166.             }
  167.             // form is not available
  168.             else {
  169.                 $pdf->SetFont('Arial', '');
  170.                 $pdf->SetTextColor(204,51,51); // red
  171.                 $pdf->Cell(50, $hi, $form->form_title, 0, 2, 'L');
  172.                 $height_tracker = $height_tracker + $hi;
  173.             }
  174.            
  175.         }
  176.     }
  177.     //_dvd($addon_forms);
  178.     // here we need to add more pages
  179.     if (!empty($addon_forms)) {
  180.         foreach ($addon_forms as $af) {
  181.           $pdf->AddPage();
  182.           $pdf->SetLink($af['link']);
  183.           $url = $af['url'];
  184.             //die($url);
  185.           $pagecount = $pdf->setSourceFile($url);
  186.           $newIdx = $pdf->importPage(1);
  187.           $pdf->useTemplate($newIdx, 0, 0, 0, 0, TRUE);
  188.           if ($pagecount > 1) {
  189.             for ($i = 2; $i <= $pagecount; $i++) {
  190.                 $pdf->AddPage();
  191.                   $thiscount = $pdf->setSourceFile($url);
  192.                   $thisIdx = $pdf->importPage($i);
  193.                   $pdf->useTemplate($thisIdx, 0, 0, 0, 0, TRUE);
  194.                   unset($thiscount, $thisIdx);
  195.               }
  196.           }
  197.         }
  198.     }
  199.  
  200.    
  201.     //$pdf->Cell(50,5,'Shipping & Drayage',0,1,'L');
  202.     // save the file
  203.     $random_string = user_password(10);
  204.     $file_save_as = 'sites/default/files/img/service_kit/customkit' . $random_string . '.pdf';
  205.     // save the file locally and move on for testing purposes
  206.     $pdf->Output($file_save_as, 'F');
  207.     /* -----
  208.      *  merge the pdfs
  209.      *  adding in the table of contents
  210.      *  to the front
  211.      * -----
  212.      */
  213.     $old_command = $command;
  214.     $command = $base_url . '/' . $file_save_as . ' ' . $old_command;
  215.    
  216.     $output = 'sites/default/files/img/service_kit/' . $random_string . 'merged-pdf.pdf';
  217.     $command = "pdftk $command output $output";
  218.     passthru($command); //run the command
  219.   header(sprintf('Location: %s', $output)); //open the merged pdf file in the browser
  220.     die('Want to stay in processing');
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement