diff --git a/pdf_using_mpdf.admin.inc b/pdf_using_mpdf.admin.inc index b1a997e..5cbd31a 100644 --- a/pdf_using_mpdf.admin.inc +++ b/pdf_using_mpdf.admin.inc @@ -30,7 +30,7 @@ function pdf_using_mpdf_config() { $form['pdf']['pdf_using_mpdf_pdf_save_option'] = array( '#type' => 'radios', '#title' => t('Open PDF File in'), - '#options' => array(t('Web Browser'), t('Save Dialog Box'), t('Save to Server')), + '#options' => array(t('Web Browser'), t('Save Dialog Box'), t('Save to Server'), t('Save to Field (API only)')), '#default_value' => variable_get('pdf_using_mpdf_pdf_save_option'), '#description' => t("Default's to Web Browser."), ); diff --git a/pdf_using_mpdf.module b/pdf_using_mpdf.module index 3ec80db..77d85c4 100644 --- a/pdf_using_mpdf.module +++ b/pdf_using_mpdf.module @@ -94,8 +94,19 @@ function _pdf_using_mpdf_attributes_access($node) { * contents of the template already with the node data. * @param string $filename * name of the PDF file to be generated. + * @param array $field_data + * field_data is array with the following structure: + * - entity_type + * - bundle + * - field_name + * - entity_id + * @param string $disposition + * for API usage of save to field: + * - trigger_dl: Trigger Download + * - bounce_refer: Bounce back to referring page + * - pass_thru: (Default) Pass through this function w/o doing anything else. */ -function _pdf_using_mpdf_generator($html, $filename = NULL) { +function _pdf_using_mpdf_generator($html, $filename = NULL, $field_data = NULL, $disposition = 'pass_thru') { ini_set('Display_errors', 'On'); error_reporting(E_ALL); @@ -324,6 +335,40 @@ function _pdf_using_mpdf_generator($html, $filename = NULL) { drupal_goto($_SERVER['HTTP_REFERER']); exit; break; + // Save to field and send for download. + case 3: + if (!empty($field_data) && is_array($field_data)) { + $entity = entity_load($field_data['entity_type'], array($field_data['entity_id'])); + $entity = array_pop($entity); + $entity_wrapper = entity_metadata_wrapper($field_data['entity_type'], $entity); + $path = file_directory_temp() . '/' . $filename . '.pdf'; + $mpdf->Output($path , 'F'); + + // Save the file in the file admin. + $file = file_save_data(file_get_contents($path), 'private://' . $filename . '.pdf'); + // And set the file. + $entity_wrapper->{$field_data['field_name']}->file->set($file); + // And save the entity. + $entity_wrapper->save(); + + // Disposition: download, bounce, passthrough + switch ($disposition) { + case 'trigger_dl': + $mpdf->Output($filename . '.pdf', 'D'); + break; + + case 'bounce_refer': + drupal_goto($_SERVER['HTTP_REFERER']); + break; + + case 'pass_thru': + default: + // Do nothing. + break; + } + } + break; + case 0: default: // Open in same browser. @@ -344,16 +389,28 @@ function _pdf_using_mpdf_generator($html, $filename = NULL) { * @param string $pdf_using_mpdf_pdf_filename * pdf_using_mpdf_pdf_filename is Optional name of the PDF file. * + * @param array $pdf_using_field_data + * pdf_using_field_data is array with the following structure: + * - entity_type + * - bundle + * - field_name + * - entity_id + * @param string $disposition + * for API usage of save to field: + * - trigger_dl: Trigger Download + * - bounce_refer: Bounce back to referring page + * - pass_thru: (Default) Pass through this function w/o doing anything else. + * * @return bool * TRUE if PDF is successfully generated and FALSE if it isn't. */ -function pdf_using_mpdf_api($html, $pdf_using_mpdf_pdf_filename = NULL) { +function pdf_using_mpdf_api($html, $pdf_using_mpdf_pdf_filename = NULL , $pdf_using_field_data = NULL, $disposition = 'pass_thru') { if (pdf_using_mpdf_library_exist() == TRUE) { if ($pdf_using_mpdf_pdf_filename === NULL) { $filename = explode(variable_get('pdf_using_mpdf_pdf_filename'), '[site:name]'); $pdf_using_mpdf_pdf_filename = token_replace($filename[0]); } - _pdf_using_mpdf_generator($html, $pdf_using_mpdf_pdf_filename); + _pdf_using_mpdf_generator($html, $pdf_using_mpdf_pdf_filename, $pdf_using_field_data, $disposition); } else { drupal_set_message(t('No mPDF Library Found in "sites/all/libraries" or "!default_module_path". Please download PHP mPDF PDF generation tool from mPDF1.com', array(