diff --git a/fillpdf.module b/fillpdf.module index fdab05cec7e7796488a6c37543b47ddeb6ea21ca..4cfd674d88acc573ad612f411430414de378516a 100644 --- a/fillpdf.module +++ b/fillpdf.module @@ -514,9 +514,14 @@ function fillpdf_merge_pdf($fid, $nids = NULL, $webform_array = NULL, $sample = _fillpdf_merge_pdf_token_replace($obj->value, $token_objects, $token); - $transform_string = TRUE; + // If the token points to an image, treat it as an image-stamping + // request. + $entity_type = 'node'; + $entity = $node; - // If they're populating with an Image field. + $transform_string = _fillpdf_process_image_tokens($entity_type, $entity, $obj, $fields, $image_data); + + // (Legacy approach.) If they're populating a node with an Image field. if (strstr($obj->value, '[stamp:')) { // HACK: Use a pseudo-token to stamp images. // Find the two sides of the square bracket contents. @@ -743,6 +748,51 @@ function fillpdf_load_entities($fillpdf_info, $nids, $webform_array, $uc_order_i return $context; } +/** + * @param $entity_type + * @param $entity + * @param $obj + * @param $fields + * @param $image_data + * @return bool + * TRUE if no images were replaced (and normal token processing should + * continue), and FALSE otherwise. + */ +function _fillpdf_process_image_tokens($entity_type, $entity, $obj, &$fields, &$image_data) { + $entity_fields = field_read_fields(array('entity_type' => $entity_type)); + foreach ($entity_fields as $field_name => $field_data) { + if (!$field_data['type'] === 'image') { + continue; + } + if ($obj->value === "[{$entity_type}:{$field_name}]") { + // It's a match! + $image_field = field_get_items($entity_type, $entity, $field_name); + if (!$image_field) { + // We matched the token, but there was no file set. + return TRUE; + } + $image_path = $image_field[0]['uri']; + $real_image_path = drupal_realpath($image_path); + $fields[$obj->pdf_key] = '{image}' . $real_image_path; + $image_path_info = pathinfo($real_image_path); + // Store the image data to transmit to the remote service if necessary. + if (!file_exists($real_image_path)) { + // We matched the token, but there was no file set. + return TRUE; + } + $file_data = file_get_contents($real_image_path); + $image_data[$obj->pdf_key] = array( + 'data' => base64_encode($file_data), + 'filenamehash' => md5($image_path_info['filename']) . '.' . $image_path_info['extension'], + ); + + return FALSE; + } + } + + return TRUE; +} + /** * @param $value * @param $token_objects