View source
<?php
require_once __DIR__ . '/fillpdf.deprecated.inc';
define("FILLPDF_DEFAULT_SERVLET_URL", variable_get('fillpdf_remote_protocol', 'https') . "://" . variable_get('fillpdf_remote_endpoint', "fillpdf.io/xmlrpc.php"));
module_load_include('inc', 'fillpdf', 'fillpdf.admin');
function fillpdf_help($path, $arg) {
switch ($path) {
case 'admin/help#fillpdf':
$content = t('See the <a href="!documentation">documentation on drupal.org</a> for a full description of and guide to this module.', array(
'!documentation' => url('http://drupal.org/documentation/modules/fillpdf'),
));
return $content;
case 'admin/structure/fillpdf':
if (module_exists('help')) {
return t('See the !link for an explanation on dowloading these forms to PDF', array(
'!link' => l(t('documentation'), 'http://drupal.org/documentation/modules/fillpdf'),
));
}
else {
return t('Activate the help module if you need an explanation on downloading these forms to PDF.');
}
}
}
function fillpdf_menu() {
$access = array(
'administer pdfs',
);
$items = array();
$items['fillpdf'] = array(
'page callback' => 'fillpdf_parse_uri',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['admin/config/media/fillpdf'] = array(
'title' => 'FillPDF settings',
'description' => 'Configure tool to use with FillPDF',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_settings',
),
'access arguments' => $access,
'type' => MENU_NORMAL_ITEM,
);
$items['admin/structure/fillpdf'] = array(
'title' => 'FillPDF',
'description' => 'Manage your PDFs',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_forms_admin',
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%'] = array(
'title' => 'Edit PDF form',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_form_edit',
3,
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%/delete'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_form_delete_confirm',
3,
),
'access arguments' => $access,
'type' => MENU_CALLBACK,
);
$items['admin/structure/fillpdf/%/export'] = array(
'title' => 'Export FillPDF field mappings',
'page callback' => 'fillpdf_form_export',
'page arguments' => array(
3,
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%/import'] = array(
'title' => 'Import FillPDF field mappings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'fillpdf_form_import_form',
3,
),
'access arguments' => $access,
);
$items['admin/structure/fillpdf/%/add'] = array(
'title' => 'Add field',
'page callback' => 'fillpdf_field',
'page arguments' => array(
4,
3,
),
'access arguments' => $access,
'type' => MENU_LOCAL_TASK,
);
$items['admin/structure/fillpdf/%/edit/%'] = array(
'page callback' => 'fillpdf_field',
'page arguments' => array(
4,
3,
5,
),
'access arguments' => $access,
);
return $items;
}
function fillpdf_permission() {
$permissions = array(
'administer pdfs' => array(
'title' => t('Administer PDFs'),
'description' => t('Allows usage of the FillPDF administration screen.'),
),
'publish own pdfs' => array(
'title' => t('Publish Own PDFs'),
'description' => t("Allows filling in and downloading PDFs with one's own site content."),
),
'publish all pdfs' => array(
'title' => t('Publish All PDFs'),
'description' => t('Allows filling in and downloading PDFs with any site content.'),
),
);
if (module_exists('uc_order')) {
$order_statuses = uc_order_status_list();
foreach ($order_statuses as $order_status) {
$id = $order_status['id'];
$title = $order_status['title'];
$permissions["publish {$id} order data"] = array(
'title' => t("Publish data from %status Ubercart orders", array(
'%status' => $title,
)),
'description' => t("Publish data from Ubercart orders and ordered products with a status of %status.", array(
'%status' => $title,
)),
);
}
}
return $permissions;
}
function fillpdf_modules_enabled($modules) {
if (in_array('uc_order', $modules)) {
module_load_install('fillpdf');
_fillpdf_add_publish_completed_orders_permission();
}
}
function fillpdf_file_download($uri) {
$uri_parts = explode('/', $uri);
if ($uri_parts[2] === 'fillpdf') {
$fid = db_query('SELECT fid FROM {file_managed} WHERE uri = :uri', array(
':uri' => $uri,
))
->fetchField();
if ($fid) {
$file = file_load($fid);
$usage = file_usage_list($file);
if (!isset($usage['fillpdf'])) {
return;
}
foreach ($usage['fillpdf'] as $type => $per_id) {
switch ($type) {
case 'fillpdf_form':
if (user_access('administer pdfs')) {
return file_get_content_headers($file);
}
break;
case 'fillpdf_file':
foreach ($per_id as $id => $count) {
$raw_file_context = fillpdf_file_context_load($id);
if ($raw_file_context) {
$stub_context = fillpdf_link_to_stub_context($raw_file_context);
if ($stub_context['fid']) {
$fillpdf_info = fillpdf_load($stub_context['fid']);
$file_context = fillpdf_load_entities($fillpdf_info, $stub_context['nids'], $stub_context['webforms'], $stub_context['uc_order_ids'], $stub_context['uc_order_product_ids'], $GLOBALS['user'], $stub_context['entity_ids']);
if (fillpdf_merge_pdf_access($file_context['nodes'], $file_context['webforms'], $file_context['uc_orders'], $file_context['uc_order_products'])) {
return file_get_content_headers($file);
}
}
}
}
break;
}
}
return -1;
}
}
}
function fillpdf_file_download_access_alter(&$grants, $file_item, $entity_type, $entity) {
if (user_access('publish all pdfs') && !!fillpdf_file_download($file_item['uri'])) {
$grants['fillpdf'] = TRUE;
}
}
function fillpdf_pdf_link($fid, $nids = NULL, array $webform_arr = NULL, $sample = FALSE, $uc_order_ids = NULL, $uc_order_product_ids = NULL, $entity_ids = NULL) {
$query = array(
'fid' => $fid,
);
if (!empty($nids)) {
$nids = (array) $nids;
$query += count($nids) > 1 ? array(
'nids' => $nids,
) : array(
'nid' => reset($nids),
);
}
if (is_array($webform_arr) && count($webform_arr)) {
$webform_arr = isset($webform_arr['nid'], $webform_arr['sid']) ? array(
$webform_arr,
) : $webform_arr;
$query += count($webform_arr) > 1 ? array(
'webforms' => $webform_arr,
) : array(
'webform' => reset($webform_arr),
);
}
if (!empty($uc_order_ids)) {
$uc_order_ids = (array) $uc_order_ids;
$query += count($uc_order_ids) > 1 ? array(
'uc_order_ids' => $uc_order_ids,
) : array(
'uc_order_id' => reset($uc_order_ids),
);
}
if (!empty($uc_order_product_ids)) {
$uc_order_product_ids = (array) $uc_order_product_ids;
$query += count($uc_order_product_ids) > 1 ? array(
'uc_order_product_ids' => $uc_order_product_ids,
) : array(
'uc_order_product_id' => reset($uc_order_product_ids),
);
}
if (!empty($entity_ids)) {
$entity_ids = (array) $entity_ids;
$query += count($entity_ids) > 1 ? array(
'entity_ids' => $entity_ids,
) : array(
'entity_id' => reset($entity_ids),
);
}
if ($sample) {
$query['sample'] = 'true';
}
$options = array(
'query' => $query,
'absolute' => TRUE,
);
return url('fillpdf', $options);
}
function fillpdf_parse_uri($uri = NULL) {
if (empty($uri)) {
$uri = request_uri();
}
$context = fillpdf_link_to_stub_context($uri);
fillpdf_merge_pdf($context['fid'], $context['nids'], $context['webforms'], $context['sample'], $context['force_download'], FALSE, $context['flatten'], TRUE, $context['uc_order_ids'], $context['uc_order_product_ids'], $context['entity_ids']);
return '';
}
function fillpdf_link_to_stub_context($uri) {
$parsed_url = drupal_parse_url($uri);
$query_string = $parsed_url['query'];
$context = array(
'nids' => array(),
'webforms' => array(),
'uc_order_ids' => array(),
'uc_order_product_ids' => array(),
'entity_ids' => array(),
'force_download' => FALSE,
'flatten' => TRUE,
'sample' => FALSE,
);
$query_string += array(
'nid' => NULL,
'nids' => NULL,
'entity_id' => NULL,
'entity_ids' => NULL,
'webform' => NULL,
'webforms' => NULL,
'uc_order_id' => NULL,
'uc_order_ids' => NULL,
'uc_order_product_id' => NULL,
'uc_order_product_ids' => NULL,
'fid' => NULL,
'sample' => NULL,
'download' => NULL,
'flatten' => NULL,
);
$context['fid'] = $query_string['fid'];
if (isset($query_string['download']) && filter_var($query_string['download'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === TRUE) {
$context['force_download'] = TRUE;
}
if (isset($query_string['flatten']) && $query_string['flatten'] !== '' && filter_var($query_string['flatten'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === FALSE) {
$context['flatten'] = FALSE;
}
if (isset($query_string['sample']) && filter_var($query_string['sample'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === TRUE) {
$context['sample'] = TRUE;
}
if ($query_string['nid'] || $query_string['nids']) {
$context['nids'] = $query_string['nid'] ? array(
$query_string['nid'],
) : $query_string['nids'];
}
if ($query_string['webform'] || $query_string['webforms']) {
$context['webforms'] = $query_string['webform'] ? array(
$query_string['webform'],
) : $query_string['webforms'];
}
if ($query_string['uc_order_id'] || $query_string['uc_order_ids']) {
$context['uc_order_ids'] = $query_string['uc_order_id'] ? array(
$query_string['uc_order_id'],
) : $query_string['uc_order_ids'];
}
if ($query_string['uc_order_product_id'] || $query_string['uc_order_product_ids']) {
$context['uc_order_product_ids'] = $query_string['uc_order_product_id'] ? array(
$query_string['uc_order_product_id'],
) : $query_string['uc_order_product_ids'];
}
if ($query_string['entity_ids'] || $query_string['entity_id'] || isset($query_string['entity']) || isset($query_string['entities'])) {
if (isset($query_string['entities'])) {
$query_string['entity_ids'] = $query_string['entities'];
unset($query_string['entities']);
}
if (isset($query_string['entity'])) {
$query_string['entity_id'] = $query_string['entity'];
unset($query_string['entity']);
}
if (isset($query_string['entity_type'], $query_string['entity_id'])) {
$query_string['entity_id'] = "{$query_string['entity_type']}:{$query_string['entity_id']}";
unset($query_string['entity_type']);
}
$context['entity_ids'] = isset($query_string['entity_id']) ? array(
$query_string['entity_id'],
) : $query_string['entity_ids'];
}
return $context;
}
function fillpdf_context_to_link($fid, $context, $sample = FALSE) {
$nids = NULL;
if (!empty($context['nodes'])) {
foreach ($context['nodes'] as $node) {
$nids[] = $node->nid;
}
}
$webforms = NULL;
if (!empty($context['webforms'])) {
foreach ($context['webforms'] as $webform) {
$webforms[] = array(
'nid' => $webform['webform']->nid,
'sid' => $webform['submission']->sid,
);
}
}
$uc_orders = NULL;
if (!empty($context['uc_orders'])) {
foreach ($context['uc_orders'] as $uc_order) {
$uc_orders[] = $uc_order->order_id;
}
}
$uc_order_products = NULL;
if (!empty($context['uc_order_products'])) {
foreach ($context['uc_order_products'] as $uc_order_product) {
$uc_orders[] = $uc_order_product->order_product_id;
}
}
$entity_ids = NULL;
if (!empty($context['entities'])) {
foreach ($context['entities'] as $entity_type => $entities_by_type) {
foreach ($entities_by_type as $entity) {
list($id) = entity_extract_ids($entity_type, $entity);
$entity_ids[] = $entity_type . ':' . $id;
}
}
}
return fillpdf_pdf_link($fid, $nids, $webforms, $sample, $uc_orders, $uc_order_products, $entity_ids);
}
function fillpdf_merge_pdf($fid, $nids = NULL, $webform_array = NULL, $sample = NULL, $force_download = FALSE, $skip_access_check = FALSE, $flatten = TRUE, $handle = TRUE, $uc_order_ids = NULL, $uc_order_product_ids = NULL, $entity_ids = NULL) {
if (is_null($fid)) {
drupal_set_message(t('FillPDF Form ID required to print a PDF.'), 'warning');
drupal_goto();
}
$fillpdf_info = fillpdf_load($fid);
if ($fillpdf_info === FALSE) {
drupal_set_message(t('Non-existent FillPDF Form ID.'), 'error');
drupal_not_found();
drupal_exit();
}
global $user;
$context = fillpdf_load_entities($fillpdf_info, $nids, $webform_array, $uc_order_ids, $uc_order_product_ids, $user, $entity_ids);
$nodes = $context['nodes'];
$webforms = $context['webforms'];
$uc_orders = $context['uc_orders'];
$uc_order_products = $context['uc_order_products'];
$entities = $context['entities'];
if ($skip_access_check !== TRUE) {
if (!fillpdf_merge_pdf_access($nodes, $webforms, $uc_orders, $uc_order_products, $entities)) {
drupal_access_denied();
drupal_exit();
}
}
$fields = $token_objects = $image_data = array();
$query = db_query("SELECT * FROM {fillpdf_fields} WHERE fid = :fid", array(
':fid' => $fid,
));
foreach ($query as $obj) {
$obj->replacements = _fillpdf_replacements_to_array($obj->replacements);
$token = '';
$transform_string = FALSE;
if ($sample) {
$fields[$obj->pdf_key] = $obj->pdf_key;
}
else {
$nodes = array_reverse($nodes);
$webforms = array_reverse($webforms);
$uc_orders = array_reverse($uc_orders);
$uc_order_products = array_reverse($uc_order_products);
$entities = array_reverse($entities);
if (!empty($nodes)) {
foreach ($nodes as $node) {
$token_objects['node'] = $node;
_fillpdf_merge_pdf_token_replace($obj->value, $token_objects, $token);
$entity_type = 'node';
$entity = $node;
_fillpdf_process_image_tokens($entity_type, $entity, $obj, $fields, $image_data, $transform_string);
if (strstr($obj->value, '[stamp:')) {
$left_side = strpos($obj->value, '[stamp:') + 7;
$right_side = strpos($obj->value, ']');
$field_name = substr($obj->value, $left_side, $right_side - $left_side);
if (isset($node->{$field_name}[$node->language])) {
$image_path = $node->{$field_name}[$node->language][0]['uri'];
$transform_string = FALSE;
$fields[$obj->pdf_key] = '{image}' . drupal_realpath($image_path);
$image_path_info = pathinfo(drupal_realpath($image_path));
$file_data = file_get_contents(drupal_realpath($image_path));
if ($file_data) {
$image_data[$obj->pdf_key] = array(
'data' => base64_encode($file_data),
'filenamehash' => md5($image_path_info['filename']) . '.' . $image_path_info['extension'],
);
}
}
}
}
}
if (!empty($entities)) {
foreach ($entities as $entity_type => $entities_of_type) {
foreach ($entities_of_type as $entity) {
if (!module_exists('entity_token')) {
break 2;
}
$entity_info = entity_get_info($entity_type);
$token_type = !empty($entity_info['token type']) ? $entity_info['token type'] : $entity_type;
$entity_token_objects = array(
$token_type => $entity,
);
$token_objects = array_merge($token_objects, $entity_token_objects);
_fillpdf_merge_pdf_token_replace($obj->value, $token_objects, $token);
$transform_string = TRUE;
_fillpdf_process_image_tokens($entity_type, $entity, $obj, $fields, $image_data, $transform_string);
}
}
}
if (!empty($webforms)) {
foreach ($webforms as $webform) {
$token_objects = array_merge($token_objects, array(
'webform-submission' => $webform['submission'],
'submission' => $webform['submission'],
'node' => $webform['webform'],
));
_fillpdf_merge_pdf_token_replace($obj->value, $token_objects, $token);
$transform_string = TRUE;
$webform_component_data = array_filter($webform['webform']->webform['components'], function ($value) {
if (!empty($value['type']) && $value['type'] !== 'file') {
return FALSE;
}
return TRUE;
});
foreach ($webform_component_data as $cid => $component) {
if (empty($webform['submission']->data[$cid]) || !count($webform['submission']->data[$cid])) {
continue;
}
$submission_component_value = $webform['submission']->data[$cid];
if ($obj->value !== "[submission:values:{$component['form_key']}]") {
continue;
}
$webform_file = file_load($submission_component_value[0]);
if (!$webform_file || count(file_validate_is_image($webform_file))) {
break;
}
_fillpdf_prepare_image_data($webform_file->uri, $obj, $fields, $image_data, $transform_string);
}
}
}
if (!empty($uc_orders)) {
foreach ($uc_orders as $uc_order) {
$token_objects['uc_order'] = $uc_order;
_fillpdf_merge_pdf_token_replace($obj->value, $token_objects, $token);
}
$transform_string = TRUE;
}
if (!empty($uc_order_products)) {
foreach ($uc_order_products as $uc_order_product) {
$token_objects = array_merge($token_objects, array(
'uc_order_product' => $uc_order_product,
'uc_order' => uc_order_load($uc_order_product->order_id),
'node' => node_load($uc_order_product->nid),
));
_fillpdf_merge_pdf_token_replace($obj->value, $token_objects, $token);
}
$transform_string = TRUE;
}
if ($transform_string) {
$str = preg_replace('|<br />|', '
', $token);
$str = _fillpdf_transform_field_value($str, $fillpdf_info->replacements, $obj->replacements);
$fields[$obj->pdf_key] = $str;
}
}
if (isset($fields[$obj->pdf_key]) && $fields[$obj->pdf_key]) {
if (isset($obj->prefix)) {
$fields[$obj->pdf_key] = $obj->prefix . $fields[$obj->pdf_key];
}
if (isset($obj->suffix)) {
$fields[$obj->pdf_key] .= $obj->suffix;
}
}
}
drupal_alter('fillpdf_merge_fields_alter', $fields, $context, $fillpdf_info);
drupal_alter('fillpdf_merge_fields', $fields, $context, $fillpdf_info);
$method = variable_get('fillpdf_service');
if (empty($method)) {
drupal_set_message(t('FillPDF is not configured.'), 'error');
drupal_goto();
}
$data = fillpdf_execute_merge($method, $fields, $fillpdf_info, 'url', $flatten, $image_data);
if (!$data) {
return;
}
if (!empty($webform['webform'])) {
$node = $webform['webform'];
}
if (!empty($node)) {
watchdog('fillpdf', 'User generated form "%form" for node "%node".', array(
'%form' => $fillpdf_info->title,
'%node' => $node->title,
));
}
$fillpdf_object = _fillpdf_build_options_object($force_download, $flatten, $fillpdf_info, $data, $nodes, $webforms, $uc_orders, $uc_order_products, $token_objects, $sample, $entities);
if ($handle === TRUE) {
module_invoke_all('fillpdf_merge_pre_handle', $fillpdf_object);
fillpdf_merge_perform_pdf_action($fillpdf_object, 'default', $force_download);
}
else {
return $fillpdf_object;
}
}
function fillpdf_load_entities($fillpdf_info, $nids, $webform_array, $uc_order_ids, $uc_order_product_ids, $user, $entity_ids) {
$context = array(
'nodes' => array(),
'webforms' => array(),
'uc_orders' => array(),
'uc_order_products' => array(),
'entities' => array(),
);
if (!empty($fillpdf_info->default_nid) && is_array($webform_array)) {
foreach (array_keys($webform_array) as $key) {
if (empty($webform_array[$key]['nid'])) {
$webform_array[$key]['nid'] = $fillpdf_info->default_nid;
}
}
}
$entity_mode = module_exists('entity_token');
if (empty($fillpdf_info->default_entity_type) && !empty($fillpdf_info->default_nid) && empty($nids) && empty($webform_array)) {
$default_node = node_load($fillpdf_info->default_nid);
if ($default_node) {
if (empty($default_node->webform)) {
if (!$entity_mode) {
$context['nodes'][] = $default_node;
}
}
else {
$webform_array = array(
array(
'nid' => $fillpdf_info->default_nid,
'node' => $default_node,
),
);
$entity_mode = FALSE;
}
}
}
if (is_array($nids)) {
foreach ($nids as $nid) {
$context['nodes'][] = node_load($nid);
}
}
if (module_exists('webform') && is_array($webform_array)) {
$included = module_load_include('inc', 'webform', 'includes/webform.submissions');
if ($included === FALSE) {
module_load_include('inc', 'webform', 'webform_submissions');
}
foreach ($webform_array as $this_webform) {
if (!empty($this_webform['nid'])) {
if (empty($this_webform['sid'])) {
$this_webform['sid'] = db_query('SELECT sid FROM {webform_submissions}
WHERE nid = :nid AND uid = :uid ORDER BY submitted DESC', array(
':nid' => $this_webform['nid'],
':uid' => $user->uid,
))
->fetchField();
}
if ($this_webform['sid'] !== FALSE) {
$context['webforms'][] = array(
'webform' => empty($this_webform['node']) ? node_load($this_webform['nid']) : $this_webform['node'],
'submission' => webform_get_submission($this_webform['nid'], $this_webform['sid']),
);
}
}
}
}
if (module_exists('uc_order') && is_array($uc_order_ids)) {
foreach ($uc_order_ids as $uc_order_id) {
$context['uc_orders'][] = uc_order_load($uc_order_id);
}
}
if (module_exists('uc_order') && is_array($uc_order_product_ids)) {
foreach ($uc_order_product_ids as $uc_order_product_id) {
$context['uc_order_products'][] = uc_order_product_load($uc_order_product_id);
}
return $context;
}
$entities_by_type = array();
if ($entity_mode) {
if (empty($nids) && empty($webform_array) && empty($entity_ids) && !empty($fillpdf_info->default_nid)) {
$entity_ids = array(
$fillpdf_info->default_nid,
);
}
if (!empty($entity_ids)) {
foreach ($entity_ids as $entity_id) {
list($type, $id) = strpos($entity_id, ':') ? explode(':', $entity_id) : array(
$entity_id,
NULL,
);
if (empty($id)) {
$id = $type;
$type = !empty($fillpdf_info->default_entity_type) ? $fillpdf_info->default_entity_type : 'node';
}
$entities_by_type += array(
$type => array(),
);
$entities_by_type[$type][] = entity_load_single($type, $id);
}
$context['entities'] = $entities_by_type;
}
}
return $context;
}
function _fillpdf_process_image_tokens($entity_type, $entity, $obj, &$fields, &$image_data, &$transform_string) {
$entity_fields = field_read_fields(array(
'entity_type' => $entity_type,
));
foreach ($entity_fields as $field_name => $field_data) {
if (!$field_data['type'] === 'image') {
continue;
}
$info = entity_get_info($entity_type);
$token_type = !empty($info['token type']) ? $info['token type'] : $entity;
if ($obj->value === "[{$token_type}:{$field_name}]") {
$image_field = field_get_items($entity_type, $entity, $field_name);
if (!$image_field) {
$transform_string = TRUE;
}
else {
$image_path = $image_field[0]['uri'];
_fillpdf_prepare_image_data($image_path, $obj, $fields, $image_data, $transform_string);
return;
}
}
}
$transform_string = TRUE;
}
function _fillpdf_prepare_image_data($image_path, $obj, &$fields, &$image_data, &$transform_string) {
$real_image_path = drupal_realpath($image_path);
$image_path_info = pathinfo($real_image_path);
if (!file_exists($real_image_path)) {
$transform_string = TRUE;
}
else {
$file_data = file_get_contents($real_image_path);
$fields[$obj->pdf_key] = '{image}' . $real_image_path;
$image_data[$obj->pdf_key] = array(
'data' => base64_encode($file_data),
'filenamehash' => md5($image_path_info['filename']) . '.' . $image_path_info['extension'],
);
$transform_string = FALSE;
}
}
function _fillpdf_merge_pdf_token_replace($value, $token_objects, &$existing_token) {
$maybe_token = token_replace($value, $token_objects, array(
'clear' => TRUE,
'sanitize' => FALSE,
));
if ($maybe_token) {
$existing_token = $maybe_token;
}
}
function _fillpdf_build_options_object($force_download, $flatten, $fillpdf_info, $data, $nodes, $webforms, $uc_orders, $uc_order_products, $token_objects, $sample, $entities) {
$fillpdf_object = new stdClass();
$fillpdf_object->info = $fillpdf_info;
$fillpdf_object->data = $data;
$fillpdf_object->context = array(
'nodes' => $nodes,
'webforms' => $webforms,
'uc_orders' => $uc_orders,
'uc_order_products' => $uc_order_products,
'entities' => $entities,
);
$fillpdf_object->token_objects = $token_objects;
$fillpdf_object->options = array(
'download' => $force_download,
'flatten' => $flatten,
'sample' => $sample,
);
return $fillpdf_object;
}
function fillpdf_merge_perform_pdf_action($fillpdf_object, $action = 'download', $force_download = FALSE) {
$pdf_info = $fillpdf_object->info;
$token_objects = $fillpdf_object->token_objects;
$pdf_data = $fillpdf_object->data;
if (in_array($action, array(
'default',
'download',
'save',
'redirect',
)) === FALSE) {
return;
}
if (!$pdf_data) {
return;
}
$output_name = _fillpdf_process_filename($pdf_info->title, $token_objects);
if ($action == 'default') {
if (empty($pdf_info->destination_path) === FALSE) {
if ($pdf_info->destination_redirect) {
$action = 'redirect';
}
else {
$action = 'save';
}
}
else {
$action = 'download';
}
}
$redirect_to_file = FALSE;
switch ($action) {
case 'redirect':
$redirect_to_file = $pdf_info->destination_redirect;
case 'save':
fillpdf_action_save_to_file($fillpdf_object, $output_name, !$force_download, $redirect_to_file);
if (drupal_is_cli()) {
break;
}
case 'download':
drupal_add_http_header("Pragma", "public");
drupal_add_http_header('Expires', 0);
drupal_add_http_header('Cache-Control', 'must-revalidate, post-check=0, pre-check=0');
drupal_add_http_header('Content-type', 'application-download');
drupal_add_http_header('Content-Length', strlen($pdf_data));
drupal_add_http_header('Content-disposition', 'attachment; filename="' . $output_name . '"');
drupal_add_http_header('Content-Transfer-Encoding', 'binary');
echo $pdf_data;
drupal_exit();
break;
}
}
function fillpdf_action_save_to_file($fillpdf_object, $output_name, $redirect = TRUE, $redirect_to_file = FALSE, $destination_path_override = NULL) {
$pdf_info = $fillpdf_object->info;
$token_objects = $fillpdf_object->token_objects;
$pdf_data = $fillpdf_object->data;
if (isset($destination_path_override) && empty($destination_path_override) === FALSE) {
$destination_path = $destination_path_override;
}
elseif (empty($pdf_info->destination_path) && empty($destination_path_override)) {
$destination_path = 'fillpdf';
}
else {
$destination_path = $pdf_info->destination_path;
if ($pdf_info->scheme === 'private') {
$destination_path = "fillpdf/{$destination_path}";
}
}
$resolved_destination_path = _fillpdf_process_destination_path($destination_path, $token_objects, $pdf_info->scheme);
$path_exists = file_prepare_directory($resolved_destination_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
$saved_file = FALSE;
if ($path_exists === FALSE) {
watchdog('fillpdf', "The path %destination_path does not exist and could not be\n automatically created. Therefore, the previous submission was not saved. If\n the URL contained download=1, then the PDF was still sent to the user's browser.\n If you were redirecting them to the PDF, they were sent to the homepage instead.\n If the destination path looks wrong and you have used tokens, check that you have\n used the correct token and that it is available to FillPDF at the time of PDF\n generation.", array(
'%destination_path' => $resolved_destination_path,
));
}
else {
$saved_file = file_save_data($pdf_data, "{$resolved_destination_path}/{$output_name}", FILE_EXISTS_RENAME);
fillpdf_file_usage_add($saved_file, $fillpdf_object);
if ($redirect === TRUE) {
if (isset($_GET['destination']) === FALSE) {
if ($redirect_to_file) {
drupal_goto(file_create_url($saved_file->uri));
}
}
}
}
if ($redirect === TRUE && !drupal_is_cli()) {
drupal_goto();
}
return $saved_file;
}
function fillpdf_fillpdf_merge_pre_handle($fillpdf) {
if (module_exists('rules')) {
rules_invoke_event('fillpdf_merge_pre_handle', $fillpdf);
}
}
function fillpdf_merge_pdf_access($nodes = array(), $webforms = array(), $uc_orders = array(), $uc_order_products = array(), $entities = array()) {
if (user_access('administer pdfs') || user_access('publish all pdfs')) {
return TRUE;
}
if (!user_access('publish own pdfs')) {
return FALSE;
}
global $user;
$account = user_load($user->uid);
if (empty($webforms)) {
foreach ($nodes as $node) {
if (!node_access('view', $node) || $node->uid != $user->uid) {
return FALSE;
}
}
}
else {
foreach ($webforms as $webform) {
if (!node_access('view', node_load($webform['webform']->nid))) {
return FALSE;
}
}
}
if (!empty($webforms)) {
foreach ($webforms as $webform) {
if (!webform_submission_access($webform['webform'], $webform['submission'], 'view')) {
return FALSE;
}
}
}
if (!empty($uc_orders)) {
foreach ($uc_orders as $uc_order) {
$order_status = $uc_order->order_status;
if ((!uc_order_order_entity_access('view', $uc_order, $account) || !user_access("publish {$order_status} order data")) && !user_access('view all orders')) {
return FALSE;
}
}
}
if (!empty($uc_order_products)) {
foreach ($uc_order_products as $uc_order_product) {
$order = uc_order_load($uc_order_product->order_id);
$order_status = $order->order_status;
if ((!uc_order_order_product_access('view', $uc_order_product, $account) || !user_access("publish {$order_status} order data")) && !user_access('view all orders')) {
return FALSE;
}
}
}
if (!empty($entities)) {
foreach ($entities as $entity_type => $entities_of_type) {
foreach ($entities_of_type as $entity_id => $entity) {
if (!entity_access('view', $entity_type, $entity, $account)) {
return FALSE;
}
}
}
}
return TRUE;
}
function _fillpdf_process_filename($original, $token_objects) {
if (!empty($token_objects)) {
$original = token_replace($original, $token_objects, array(
'sanitize' => FALSE,
));
}
$output_name = str_replace(' ', '_', $original);
$output_name = preg_replace('/\\.pdf$/i', '', $output_name);
$output_name = preg_replace('/[^a-zA-Z0-9_.-]+/', '', $output_name) . '.pdf';
return $output_name;
}
function fillpdf_build_filename($original, $token_objects) {
return _fillpdf_process_filename($original, $token_objects);
}
function fillpdf_execute_merge($method, array $fields, $fillpdf, $mode = 'url', $flatten = TRUE, $image_data = array()) {
$data = NULL;
switch ($mode) {
case 'url':
$filename = $fillpdf->url;
break;
case 'stream':
$filename = file_unmanaged_save_data($fillpdf, file_directory_temp() . '/pdf_data.pdf', FILE_EXISTS_RENAME);
break;
default:
$filename = $fillpdf->url;
}
$contents = _fillpdf_get_file_contents($filename, '<front>');
switch ($method) {
case 'remote':
foreach ($fields as $field_name => &$field) {
if (!empty($image_data[$field_name])) {
$field_path_info = pathinfo($field);
$field = '{image}' . md5($field_path_info['filename']) . '.' . $field_path_info['extension'];
}
}
unset($field);
$api_key = variable_get('fillpdf_api_key', '0');
$result = _fillpdf_xmlrpc_request(FILLPDF_DEFAULT_SERVLET_URL, 'merge_pdf_v3', base64_encode($contents), $fields, $api_key, $flatten, $image_data);
if ($result->error == TRUE) {
if ($mode === 'stream') {
file_unmanaged_delete($filename);
}
return FALSE;
}
$data = base64_decode($result->data);
break;
case 'local_service':
$field_mappings = array();
foreach ($fields as $key => $field) {
if (strpos($field, '{image}') === 0) {
if (!empty($image_data[$key]) && !empty($image_data[$key]['data']) && !empty($image_data[$key]['filenamehash'])) {
$field_mappings[$key] = array(
'type' => 'image',
'data' => $image_data[$key]['data'],
'extension' => pathinfo($image_data[$key]['filenamehash'], PATHINFO_EXTENSION),
);
continue;
}
}
$field_mappings[$key] = array(
'type' => 'text',
'data' => $field,
);
}
$request = array(
'pdf' => base64_encode($contents),
'flatten' => $flatten,
'fields' => $field_mappings,
);
$json = drupal_json_encode($request);
$merge_endpoint = variable_get('fillpdf_local_service_endpoint') . '/api/v1/merge';
$result = drupal_http_request($merge_endpoint, array(
'method' => 'POST',
'data' => $json,
'headers' => array(
'Content-Type' => 'application/json',
),
));
if ((int) $result->code !== 200) {
if ($result->code) {
drupal_set_message('Error ' . $result->code . '. Reason: ' . $result->error, 'error');
}
else {
drupal_set_message('Error occurred merging PDF: ' . $result->error, 'error');
}
$fields = array();
break;
}
$data = base64_decode(drupal_json_decode($result->data)['pdf']);
if ($mode === 'stream') {
file_unmanaged_delete($filename);
}
break;
case 'local':
$require = drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc';
require_once DRUPAL_ROOT . '/' . $require;
try {
$fillpdf = new java('com.ocdevel.FillpdfService', base64_encode($contents), 'bytes');
foreach ($fields as $key => $field) {
if (substr($field, 0, 7) == '{image}') {
$image_filepath = substr($field, 7);
$fillpdf
->image($key, $image_filepath, "file");
}
else {
$fillpdf
->text($key, $field);
}
}
} catch (JavaException $e) {
if ($mode == 'stream') {
file_unmanaged_delete($filename);
}
$error = check_plain(java_truncate((string) $e));
drupal_set_message($error, 'error');
watchdog('fillpdf', $error, array(), WATCHDOG_ERROR);
return FALSE;
}
try {
if ($flatten) {
$data = java_values(base64_decode($fillpdf
->toByteArray()));
}
else {
$data = java_values(base64_decode($fillpdf
->toByteArrayUnflattened()));
}
} catch (JavaException $e) {
if ($mode == 'stream') {
file_unmanaged_delete($filename);
}
$error = check_plain(java_truncate((string) $e));
drupal_set_message($error, 'error');
watchdog('fillpdf', $error, array(), WATCHDOG_ERROR);
return FALSE;
}
break;
case 'pdftk':
module_load_include('inc', 'fillpdf', 'xfdf');
$xfdfname = $filename . '.xfdf';
$xfdf = create_xfdf(basename($xfdfname), $fields);
$xfdffile = file_unmanaged_save_data($xfdf, $xfdfname, FILE_EXISTS_RENAME);
$pdftk_command = array();
$pdftk_command[] = fillpdf_pdftk_path();
$pdftk_command[] = escapeshellarg(drupal_realpath($filename));
$pdftk_command[] = 'fill_form';
$pdftk_command[] = escapeshellarg(drupal_realpath($xfdffile));
$pdftk_command[] = 'output -';
if ($flatten) {
$pdftk_command[] = 'flatten';
}
$pdftk_command[] = 'drop_xfa';
$pdftk_command = implode(' ', $pdftk_command);
$descriptorspec = array(
1 => array(
'pipe',
'w',
),
2 => array(
'pipe',
'w',
),
);
$proc = proc_open($pdftk_command, $descriptorspec, $pipes);
$data = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$exit_status = proc_close($proc);
if (!$data) {
drupal_set_message(t('Error with pdftk. No PDF generated.'), 'error');
}
if (!$data || $exit_status !== 0) {
$message = 'Error with pdftk: Exit status: !exit_status; data length: !data_length; stderr: @stderr';
$variables = array(
'!exit_status' => $exit_status,
'!data_length' => strlen($data),
'@stderr' => $stderr,
);
watchdog('fillpdf', $message, $variables, WATCHDOG_ERROR);
}
file_unmanaged_delete($xfdffile);
break;
case 'test':
$data = file_get_contents(drupal_get_path('module', 'fillpdf') . '/tests/fillpdf_test_v4.pdf');
variable_set('fillpdf_test_last_merge_metadata', array(
'fields' => $fields,
'images' => $image_data,
'flatten' => $flatten,
));
}
if ($data) {
return $data;
}
else {
return FALSE;
}
}
function fillpdf_parse_pdf($fid) {
$filename = fillpdf_load($fid);
$filename = $filename->url;
$method = variable_get('fillpdf_service');
if (empty($method)) {
drupal_set_message(t('FillPDF is not configured.'), 'error');
drupal_goto('admin/structure/fillpdf');
}
$parsed_fields = fillpdf_execute_parse($method, $filename);
if ($parsed_fields === FALSE) {
drupal_goto('admin/structure/fillpdf');
}
db_delete('fillpdf_fields')
->condition('fid', $fid)
->execute();
$unique_fields = array();
foreach ((array) $parsed_fields as $key => $field) {
if (!empty($field['type'])) {
$unique_fields[$field['name']] = TRUE;
}
}
foreach (array_keys($unique_fields) as $pdf_key) {
$record = array(
'label' => NULL,
'value' => '',
);
fillpdf_fields_create_update($fid, $pdf_key, $record);
}
}
function fillpdf_fields_create_update($fid, $pdf_key, array &$record, $update = FALSE) {
$record['fid'] = $fid;
$record['pdf_key'] = $pdf_key;
$primary_keys = $update ? array(
'fid',
'pdf_key',
) : array();
return drupal_write_record('fillpdf_fields', $record, $primary_keys);
}
function fillpdf_execute_parse($method, $fillpdf, $mode = 'url') {
switch ($mode) {
case 'url':
$filename = $fillpdf;
$content = _fillpdf_get_file_contents($filename, "<front>");
break;
case 'stream':
$filename = file_unmanaged_save_data($fillpdf, file_directory_temp() . '/pdf_data.pdf', FILE_EXISTS_RENAME);
$content = $fillpdf;
break;
}
switch ($method) {
case 'remote':
$result = _fillpdf_xmlrpc_request(FILLPDF_DEFAULT_SERVLET_URL, 'parse_pdf_fields', base64_encode($content));
if ($result->error == TRUE) {
if ($mode == 'stream') {
file_unmanaged_delete($filename);
}
return FALSE;
}
$fields = $result->data;
break;
case 'local_service':
$request = array(
'pdf' => base64_encode($content),
);
$json = drupal_json_encode($request);
$parse_endpoint = variable_get('fillpdf_local_service_endpoint') . '/api/v1/parse';
$result = drupal_http_request($parse_endpoint, array(
'method' => 'POST',
'data' => $json,
'headers' => array(
'Content-Type' => 'application/json',
),
));
if ((int) $result->code !== 200) {
if ($result->code) {
drupal_set_message('Error ' . $result->code . '. Reason: ' . $result->error, 'error');
}
else {
drupal_set_message('Error occurred parsing PDF: ' . $result->error, 'error');
}
$fields = array();
break;
}
$fields = drupal_json_decode($result->data);
break;
case 'local':
$require = drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc';
require_once DRUPAL_ROOT . '/' . $require;
try {
$fillpdf = new java('com.ocdevel.FillpdfService', base64_encode($content), 'bytes');
$fields = java_values($fillpdf
->parse());
} catch (JavaException $e) {
if ($mode == 'stream') {
file_unmanaged_delete($filename);
}
$error = check_plain(java_truncate((string) $e));
drupal_set_message($error, 'error');
watchdog('fillpdf', $error, array(), WATCHDOG_ERROR);
return FALSE;
}
break;
case 'pdftk':
$path_to_pdftk = fillpdf_pdftk_path();
$status = fillpdf_pdftk_check($path_to_pdftk);
if ($status === FALSE) {
drupal_set_message(t('pdftk not properly installed.'), 'error');
return array();
}
$output = array();
$pdftk_command = array();
$pdftk_command[] = $path_to_pdftk;
$pdftk_command[] = escapeshellarg(drupal_realpath($filename));
$pdftk_command[] = 'dump_data_fields_utf8';
exec(implode(' ', $pdftk_command), $output, $status);
if (count($output) === 0) {
drupal_set_message(t('PDF does not contain fillable fields.'), 'warning');
return array();
}
$data_fields_map = array(
'FieldType' => 'type',
'FieldName' => 'name',
'FieldFlags' => 'flags',
'FieldJustification' => 'justification',
);
$fields = array();
$fieldindex = -1;
foreach ($output as $line => $lineitem) {
if ($lineitem == '---') {
$fieldindex++;
continue;
}
list($key, $value) = explode(':', $lineitem);
if (in_array($key, array_keys($data_fields_map))) {
$fields[$fieldindex][$data_fields_map[$key]] = trim($value);
}
}
break;
case 'test':
$fields = array(
0 => array(
'name' => 'ImageField',
'value' => '',
'type' => 'Pushbutton',
),
1 => array(
'name' => 'Button',
'value' => '',
'type' => 'Pushbutton',
),
2 => array(
'name' => 'TextField',
'value' => '',
'type' => 'Text',
),
3 => array(
'name' => 'textfield',
'value' => '',
'type' => 'Text',
),
4 => array(
'name' => 'ImageField',
'value' => '',
'type' => 'Pushbutton',
),
);
break;
}
if ($mode == 'stream') {
file_unmanaged_delete($filename);
}
return $fields;
}
function _fillpdf_get_file_contents($filepath, $error_goto = NULL) {
$filepath = drupal_realpath($filepath);
if ($error_goto && !file_exists($filepath)) {
drupal_set_message(t('@filepath does not exist. Check your
filesystem settings, as well as http://drupal.org/node/764936', array(
'@filepath' => $filepath,
)), 'error');
drupal_goto($error_goto);
}
$handle = fopen($filepath, "r");
$content = fread($handle, filesize($filepath));
fclose($handle);
return $content;
}
function _fillpdf_xmlrpc_request($url, $method) {
$args = func_get_args();
array_shift($args);
$args = array(
$args[0] => array_slice($args, 1),
);
$result = xmlrpc($url, $args, array(
'timeout' => 3600.0,
));
$ret = new stdClass();
if (isset($result['error'])) {
drupal_set_message($result['error'], 'error');
$ret->error = TRUE;
}
elseif ($result == FALSE || xmlrpc_error()) {
$error = xmlrpc_error();
$ret->error = TRUE;
drupal_set_message(t('There was a problem contacting the FillPDF Service.
It may be down, or you may not have internet access. [ERROR @code: @message]', array(
'@code' => $error->code,
'@message' => $error->message,
)), 'error');
}
else {
$ret->data = $result['data'];
$ret->error = FALSE;
}
return $ret;
}
function fillpdf_get_fields($fid) {
$result = db_query('SELECT * FROM {fillpdf_fields} WHERE fid = :fid', array(
':fid' => $fid,
));
$return = array();
foreach ($result as $result_array) {
$return[$result_array->pdf_key] = array(
'label' => $result_array->label,
'value' => $result_array->value,
);
}
return $return;
}
function _fillpdf_process_destination_path($destination_path, $token_objects, $scheme = 'public') {
$orig_path = $destination_path;
$destination_path = trim($orig_path);
$types = array();
if (isset($token_objects['node'])) {
$types[] = 'node';
}
elseif (isset($token_objects['webform'])) {
$types[] = 'webform';
}
foreach ($types as $type) {
$destination_path = token_replace($destination_path, array(
$type => $token_objects[$type],
), array(
'clear' => TRUE,
));
}
if ($scheme === 'public' && drupal_substr($destination_path, 0, 1) === '/') {
}
else {
$destination_path = file_stream_wrapper_uri_normalize("{$scheme}://{$destination_path}");
}
return $destination_path;
}
function _fillpdf_replacements_to_array($replacements) {
if (empty($replacements) !== TRUE) {
$standardized_replacements = str_replace(array(
"\r\n",
"\r",
), "\n", $replacements);
$lines = explode("\n", $standardized_replacements);
$return = array();
foreach ($lines as $replacement) {
if (!empty($replacement)) {
$split = explode('|', $replacement);
if (count($split) == 2) {
$return[$split[0]] = preg_replace('|<br />|', '
', $split[1]);
}
}
}
return $return;
}
else {
return array();
}
}
function _fillpdf_transform_field_value($value, $pdf_replacements, $field_replacements) {
if (empty($pdf_replacements) && empty($field_replacements)) {
return $value;
}
elseif (!empty($field_replacements) && isset($field_replacements[$value])) {
return $field_replacements[$value];
}
elseif (!empty($pdf_replacements) && isset($pdf_replacements[$value])) {
return $pdf_replacements[$value];
}
else {
return $value;
}
}
function fillpdf_load($fid, $reset = FALSE, $process_replacements = TRUE) {
static $fillpdf = array();
if (!isset($fillpdf[$fid]) || $reset) {
$fillpdf[$fid] = db_query("SELECT * FROM {fillpdf_forms} WHERE fid = :fid", array(
':fid' => $fid,
))
->fetch();
}
if ($fillpdf[$fid]) {
$this_fillpdf = clone $fillpdf[$fid];
if ($process_replacements) {
$this_fillpdf->replacements = _fillpdf_replacements_to_array($this_fillpdf->replacements);
}
return $this_fillpdf;
}
return FALSE;
}
function fillpdf_pdftk_check($pdftk_path = 'pdftk') {
if (empty($pdftk_path)) {
$pdftk_path = 'pdftk';
}
$output = array();
$status = NULL;
exec($pdftk_path . ' --version', $output, $status);
$output = implode("\n", $output);
if (in_array($status, array(
126,
127,
))) {
return FALSE;
}
elseif (!strpos($output, 'pdftk')) {
return FALSE;
}
return TRUE;
}
function fillpdf_pdftk_path() {
return variable_get('fillpdf_pdftk_path') ?: 'pdftk';
}
function fillpdf_localservice_check($localservice_endpoint = NULL) {
if (is_null($localservice_endpoint)) {
$localservice_endpoint = variable_get('fillpdf_local_service_endpoint');
}
$response = drupal_http_request($localservice_endpoint);
return empty($response->error) && $response->code == 200;
}
function fillpdf_build_uri($path) {
$uri = fillpdf_default_scheme() . '://' . $path;
return file_stream_wrapper_uri_normalize($uri);
}
function fillpdf_scheme_options() {
$options = array();
foreach (file_get_stream_wrappers(STREAM_WRAPPERS_WRITE_VISIBLE) as $scheme => $info) {
$options[$scheme] = check_plain($info['description']);
}
return $options;
}
function fillpdf_template_scheme() {
$template_scheme = variable_get('fillpdf_scheme');
$scheme_options = fillpdf_scheme_options();
return $template_scheme && isset($scheme_options[$template_scheme]) ? $template_scheme : variable_get('file_default_scheme', 'public');
}
function fillpdf_default_scheme() {
$site_default_scheme = variable_get('file_default_scheme');
if ($site_default_scheme == 'public') {
$scheme_options = fillpdf_scheme_options();
return isset($scheme_options['private']) ? 'private' : $site_default_scheme;
}
return $site_default_scheme ?: 'public';
}
function fillpdf_file_usage_add($file, $fillpdf_object) {
$fcid = db_insert('fillpdf_file_context')
->fields(array(
'context' => fillpdf_context_to_link($fillpdf_object->info->fid, $fillpdf_object->context, $fillpdf_object->options['sample']),
'fid' => $fillpdf_object->info->fid,
))
->execute();
file_usage_add($file, 'fillpdf', 'fillpdf_file', $fcid);
}
function fillpdf_file_usage_delete(stdClass $file) {
file_usage_delete($file, 'fillpdf', 'fillpdf_file', NULL, 0);
$subquery = db_select('file_usage', 'fu');
$subquery
->addField('fu', 'id', 'fcid');
$subquery = $subquery
->condition('type', 'fillpdf_file')
->condition('module', 'fillpdf');
db_delete('fillpdf_file_context')
->condition('fcid', $subquery, 'NOT IN')
->execute();
}
function fillpdf_file_context_load($fcid) {
$context = db_query('SELECT context
FROM {fillpdf_file_context}
WHERE fcid = :fcid', array(
':fcid' => $fcid,
))
->fetchField();
return $context;
}
function fillpdf_file_usage($file) {
$usage = file_usage_list($file);
foreach ($usage as $module => $per_module) {
if ($module === 'fillpdf') {
foreach ($per_module as $type => $per_id) {
return $per_id;
}
}
}
return FALSE;
}
function fillpdf_form_delete_template($fid) {
$fid = (int) $fid;
db_delete('fillpdf_fields')
->condition('fid', $fid)
->execute();
fillpdf_form_delete_file($fid);
db_delete('fillpdf_forms')
->condition('fid', $fid)
->execute();
}
function fillpdf_form_delete_file($fid) {
$fid = (int) $fid;
$file = file_load($fid);
file_usage_delete($file, 'fillpdf', 'fillpdf_form', $fid, 0);
file_delete($file);
}