Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * build the custom kit
- */
- function serviceskit_build_kit($array_of_form_names) {
- //define('FPDF_FONTPATH','sites/all/libraries/fpdf-i/font');
- require_once('sites/all/libraries/pdf-gen/fpdf1.7/fpdf.php');
- require_once('sites/all/libraries/pdf-gen/fpdi/fpdi.php');
- /* -----
- * build the array's with
- * file info and build
- * the command string for the passthru
- * -----
- */
- // the forms that are being requested
- $forms = $array_of_form_names;
- $file_ids = array();
- $file_locations = array();
- foreach ($forms as $form) {
- // get the file id
- $file_ids[] = (int)str_replace('download-form-', '', $form);
- }
- $all_files = file_load_multiple($file_ids);
- //_dvd($all_files);
- $file_count = count($all_files);
- //var_dump($all_files);
- $command = '';
- if (!empty($all_files)) {
- // get started with pdf merge
- // likely can be removed, no longer using
- // passthru() due to linking issues
- foreach ($all_files as $file) {
- $url = file_create_url($file->uri);
- $command = $command. ' ' . $url;
- }
- //
- }
- /* -----
- * make the table of contents
- * -----
- */
- $pdf = new FPDI('P','pt','A4');
- $pdf->AddPage();
- // in order to use fpdi we must pass a relative url
- // this conflicts with what Drupal gives us to return
- // the actual location of a public file, file_create_url(),
- // we must make the url into a relative path
- $url = _absolute_to_relative(file_create_url('public://service_kit/TOC.pdf'));
- $pdf->setSourceFile($url);
- $tplIdx = $pdf->importPage(1);
- //die($tplIdx);
- $pdf->useTemplate($tplIdx, 0, 0, 612, 792, TRUE);
- //_dvd($pdf->getTemplateSize($tplIdx));
- $pdf->SetFont('Arial');
- $pdf->SetTextColor(0,0,0);
- $pdf->SetFontSize('10');
- // arrange the toc
- // starting at
- $pdf->SetXY(12, 75);
- // General Services
- //$pdf->Cell(50,5,'Shipping & Drayage',0,1,'L');
- $genservice = serviceskit_get_forms_by_category(1);
- $genservice = _is_form_requested_and_or_available($genservice, serviceskit_get_forms_by_id($file_ids));
- $kecservice = serviceskit_get_forms_by_category(2);
- $kecservice = _is_form_requested_and_or_available($kecservice, serviceskit_get_forms_by_id($file_ids));
- $shipping = serviceskit_get_forms_by_category(3);
- $shipping = _is_form_requested_and_or_available($shipping, serviceskit_get_forms_by_id($file_ids));
- $rules = serviceskit_get_forms_by_category(4);
- $rules = _is_form_requested_and_or_available($rules, serviceskit_get_forms_by_id($file_ids));
- $other = serviceskit_get_forms_by_category(5);
- $other = _is_form_requested_and_or_available($other, serviceskit_get_forms_by_id($file_ids));
- $promotion = serviceskit_get_forms_by_category(6);
- $promotion = _is_form_requested_and_or_available($promotion, serviceskit_get_forms_by_id($file_ids));
- $addlinfo = serviceskit_get_forms_by_category(7);
- $addlinfo = _is_form_requested_and_or_available($addlinfo, serviceskit_get_forms_by_id($file_ids));
- $page_counter = 0;
- $full_kit = array($genservice, $kecservice, $shipping, $rules, $other, $promotion, $addlinfo);
- //_dvd($full_kit);
- $internal_page_counter = 2;
- // height increment
- $hi = 5;
- $width_tracker = 0;
- $addon_forms = array();
- foreach ($full_kit as $section) {
- $_helper = prev($section);
- // this gives section title if there are no forms
- $section_title = serviceskit_get_category_name_from_id($_helper->category_id);
- // section title if there are forms
- if (!$section_title) {
- $section_title = serviceskit_get_category_name_from_id($_helper->servicekit_form->category_id);
- }
- if ($_helper->category_id == 4) {
- //_dvd($section);
- }
- if ($_helper->category_id == 2) {
- $pdf->SetXY(85,75);
- }
- if ($_helper->category_id == 5) {
- $pdf->SetXY(155,75);
- }
- //$pdf->SetFontSize('11');
- $pdf->SetTextColor(0,0,0);
- $pdf->SetFont('Arial', '');
- $pdf->Cell(50, 7, strtoupper($section_title), 0, 2, 'L', false);
- $height_tracker = $height_tracker + $hi;
- $pdf->SetFontSize('10');
- foreach ($section as $form) {
- // first if requested
- if ($form->form_requested) {
- $pdf->SetTextColor(245,114,23); // orange
- $pdf->SetFont('Arial', 'U');
- if ($form->fid != 0) {
- // these pdfs are going to be added
- // onto our document. We need to set a
- // link also
- $addon_forms[$form->fid]['url'] = _absolute_to_relative(file_create_url($form->uri));
- $addon_link = $pdf->AddLink();
- $addon_forms[$form->fid]['link'] = $addon_link;
- $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $addon_link);
- $height_tracker = $height_tracker + $hi;
- unset($addon_link);
- }
- else {
- $link = $form->servicekit_form->form_url;
- $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $link);
- $height_tracker = $height_tracker + $hi;
- }
- continue;
- }
- // next if available
- if (!($form->form_requested) && $form->form_available) {
- $pdf->SetTextColor(51,51,102); // purple
- $pdf->SetFont('Arial', 'U');
- if ($form->fid != 0) {
- $link = file_create_url($form->uri);
- $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $link);
- $height_tracker = $height_tracker + $hi;
- }
- else {
- $link = $form->servicekit_form->form_url;
- $pdf->Cell(50, $hi, $form->servicekit_form->form_title, 0, 2, 'L', false, $link);
- $height_tracker = $height_tracker + $hi;
- }
- continue;
- }
- // form is not available
- else {
- $pdf->SetFont('Arial', '');
- $pdf->SetTextColor(204,51,51); // red
- $pdf->Cell(50, $hi, $form->form_title, 0, 2, 'L');
- $height_tracker = $height_tracker + $hi;
- }
- }
- }
- //_dvd($addon_forms);
- // here we need to add more pages
- if (!empty($addon_forms)) {
- foreach ($addon_forms as $af) {
- $pdf->AddPage();
- $pdf->SetLink($af['link']);
- $url = $af['url'];
- //die($url);
- $pagecount = $pdf->setSourceFile($url);
- $newIdx = $pdf->importPage(1);
- $pdf->useTemplate($newIdx, 0, 0, 0, 0, TRUE);
- if ($pagecount > 1) {
- for ($i = 2; $i <= $pagecount; $i++) {
- $pdf->AddPage();
- $thiscount = $pdf->setSourceFile($url);
- $thisIdx = $pdf->importPage($i);
- $pdf->useTemplate($thisIdx, 0, 0, 0, 0, TRUE);
- unset($thiscount, $thisIdx);
- }
- }
- }
- }
- //$pdf->Cell(50,5,'Shipping & Drayage',0,1,'L');
- // save the file
- $random_string = user_password(10);
- $file_save_as = 'sites/default/files/img/service_kit/customkit' . $random_string . '.pdf';
- // save the file locally and move on for testing purposes
- $pdf->Output($file_save_as, 'F');
- /* -----
- * merge the pdfs
- * adding in the table of contents
- * to the front
- * -----
- */
- $old_command = $command;
- $command = $base_url . '/' . $file_save_as . ' ' . $old_command;
- $output = 'sites/default/files/img/service_kit/' . $random_string . 'merged-pdf.pdf';
- $command = "pdftk $command output $output";
- passthru($command); //run the command
- header(sprintf('Location: %s', $output)); //open the merged pdf file in the browser
- die('Want to stay in processing');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement