function fillpdf_merge_pdf in FillPDF 6
Same name and namespace in other branches
- 7.2 fillpdf.module \fillpdf_merge_pdf()
- 7 fillpdf.module \fillpdf_merge_pdf()
Constructs a page from scratch (pdf content-type) and sends it to the browser or saves it, depending on if a custom path is configured or not.
@seealso fillpdf_pdf_link() for $_GET params
Parameters
$fid: The integer ID of the PDF.
$nids: Array of integer IDs of the CCK nodes from which to draw data.
$webform_arr: Array of integer IDs of the Webform nodes from which to draw data.
$sample: If "true" (exact string), each field will be filled with its field name.
$force_download: Boolean. If TRUE, always send a PDF to the browser, even if a destination_path is set for the PDF.
$skip_access_check: Boolean. If TRUE, do not do any access checks. Allow the user to download any PDF with data from any node. Only use when access checks are being done some other way.
$flatten: Boolean. If TRUE, flatten the PDF so that fields cannot be edited. Otherwise leave fields editable.
Return value
Nothing.
1 call to fillpdf_merge_pdf()
- fillpdf_parse_uri in ./
fillpdf.module - Get the data and form that need to be merged, from the $_GET, and print the PDF @seealso fillpdf_pdf_link for $_GET params
File
- ./
fillpdf.module, line 206 - Allows mappings of PDFs to site content
Code
function fillpdf_merge_pdf($fid, $nids = null, $webform_arr = null, $sample = null, $force_download = FALSE, $skip_access_check = FALSE, $flatten = TRUE) {
// Case 1: No $fid
if (is_null($fid)) {
drupal_set_message('Fillpdf Form ID required to print a PDF', 'warning');
drupal_goto();
}
$fillpdf_info = db_fetch_object(db_query("SELECT title, url, destination_path, replacements, destination_redirect FROM {fillpdf_forms} WHERE fid=%d", $fid));
// Case 1.5: $fid is not valid.
if ($fillpdf_info === FALSE) {
drupal_set_message(t('Non-existent Fill PDF Form ID.'), 'error');
drupal_not_found();
exit;
}
$fillpdf_info->replacements = _fillpdf_replacements_to_array($fillpdf_info->replacements);
global $user;
$nodes = $webforms = array();
// Nodes
if (is_array($nids)) {
foreach ($nids as $nid) {
$nodes[] = node_load($nid);
}
}
// Webforms
if (module_exists('webform') && is_array($webform_arr)) {
// Load the submissions inc depending on webform-2.x or webform-3.x
$webform_info_file = drupal_parse_info_file(drupal_get_path('module', 'webform') . '/webform.info');
if (strstr($webform_info_file['version'], "6.x-2") === FALSE) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
}
else {
module_load_include('inc', 'webform', 'webform_submissions');
}
foreach ($webform_arr as $webform) {
if (!$webform['sid']) {
// user didn't specify submission-id, meaning they want most recent
$webform['sid'] = db_result(db_query('select sid from {webform_submissions}
where nid=%d and uid=%d order by submitted desc', $webform['nid'], $user->uid));
}
$webforms[] = array(
'webform' => node_load($webform['nid']),
'submission' => webform_get_submission($webform['nid'], $webform['sid']),
);
}
}
// Are we skipping the access check? This may be done when the PDF is generated
// programmatically. Use wisely.
if ($skip_access_check !== TRUE) {
if (!fillpdf_merge_pdf_access($nodes, $webforms)) {
drupal_access_denied();
module_invoke_all('exit');
exit;
}
}
$fields = $token_objects = $image_data = array();
$query = db_query("SELECT * FROM {fillpdf_fields} WHERE fid=%d", $fid);
while ($obj = db_fetch_object($query)) {
$obj->replacements = _fillpdf_replacements_to_array($obj->replacements);
// Keep track of whether we're dealing with an image or not
$transform_string = FALSE;
// Fill a sample PDF & return
if ($sample == 'true') {
$fields[$obj->pdf_key] = $obj->pdf_key;
// If sampling, return to the form edit page
$_REQUEST['destination'] = "admin/content/fillpdf/{$fid}";
}
else {
// multiple nids, #516840
// we want the last nid in $_GET to override previous ones (aka, of fillpdf?nids[]=1&nids[]=2, 2 wins)
$nodes = array_reverse($nodes);
$webforms = array_reverse($webforms);
// --- node token replacements
if (!empty($nodes)) {
foreach ($nodes as $node) {
$token_objects['node'] = $node;
$token = token_replace($obj->value, $type = 'node', $object = $node);
if ($token && $token != $obj->value) {
break;
}
}
$transform_string = TRUE;
}
// If they're populating with an Image field
if (strstr($obj->value, 'filefield-fid]')) {
module_load_include('inc', 'filefield', 'field_file');
$filefield = field_file_load($token);
$file_bytes = _fillpdf_get_file_contents($filefield['filepath']);
$str = base64_encode($file_bytes);
$transform_string = FALSE;
$fields[$obj->pdf_key] = '{image}' . realpath($filefield['filepath']);
$image_path_info = pathinfo(realpath($filefield['filepath']));
// Store the image data to transmit to the remote service if necessary
$image_data[$obj->pdf_key] = array(
'data' => base64_encode(file_get_contents(realpath($filefield['filepath']))),
'filenamehash' => md5($image_path_info['filename']) . '.' . $image_path_info['extension'],
);
}
else {
}
// /--- node token replacements ---
// --- webform token replacements
if (!empty($webforms)) {
foreach ($webforms as $webform) {
$token_objects['webform'] = $webform['submission'];
$token = token_replace($obj->value, $type = 'webform', $webform['submission']);
if ($token) {
break;
}
}
$transform_string = TRUE;
}
// /--- webform token replacements ---
if ($transform_string) {
// Replace <br /> occurrences with newlines
$str = preg_replace('|<br />|', '
', $token);
$str = _fillpdf_transform_field_value($str, $fillpdf_info->replacements, $obj->replacements);
$fields[$obj->pdf_key] = $str;
}
}
}
$pdf_data = _fillpdf_get_file_contents($fillpdf_info->url, "<front>");
$fillpdf_remote_service = variable_get('fillpdf_remote_service', TRUE);
$fillpdf_local_service = variable_get('fillpdf_local_service', TRUE);
// use fillpdf-service.com's xmlrpc service (must be registered)
if ($fillpdf_remote_service) {
$api_key = variable_get('fillpdf_api_key', '0');
$result = _fillpdf_xmlrpc_request(DEFAULT_SERVLET_URL, 'merge_pdf_v3', base64_encode($pdf_data), $fields, $api_key, $flatten, $image_data);
if ($result->error == true) {
drupal_goto();
}
//after setting error message
$data = base64_decode($result->data);
}
elseif ($fillpdf_local_service) {
$require = drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc';
require_once $require;
try {
$fillpdf = new java('com.ocdevel.FillpdfService', base64_encode($pdf_data), 'bytes');
foreach ($fields as $key => $field) {
if (substr($field, 0, 7) == '{image}') {
// Remove {image} marker.
$image_bytes = substr($field, 7);
$fillpdf
->image($key, $image_bytes, "file");
}
else {
$fillpdf
->text($key, $field);
}
}
} catch (JavaException $e) {
drupal_set_message(java_truncate((string) $e), 'error');
drupal_goto();
//after setting error message
}
try {
if ($flatten) {
$data = java_values(base64_decode($fillpdf
->toByteArray()));
}
else {
$data = java_values(base64_decode($fillpdf
->toByteArrayUnflattened()));
}
} catch (JavaException $e) {
drupal_set_message(java_truncate((string) $e), 'error');
drupal_goto();
//after setting error message
}
}
else {
$data = fillpdf_execute_merge('pdftk', $fields, $fillpdf_info, 'url', $flatten);
}
// Log this, could be useful
watchdog('fillpdf', 'User %user has generated form %form for node %node.', array(
'%user' => $user->name,
'%form' => $fillpdf_info->title,
'%node' => $node->title,
));
// Generate the filename of downloaded PDF from title of the PDF set in
// admin/content/fillpdf/%fid
$output_name = _fillpdf_process_filename($fillpdf_info->title, $token_objects);
if (!empty($fillpdf_info->destination_path)) {
$destination_path = _fillpdf_process_destination_path($fillpdf_info->destination_path, $token_objects);
$path_exists = file_check_directory($destination_path, FILE_CREATE_DIRECTORY);
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 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 Fill PDF at the time of PDF\n generation.", array(
'%destination_path' => $destination_path,
));
}
else {
// Full steam ahead!
$saved_file_path = file_save_data($data, $destination_path . "/{$output_name}", FILE_EXISTS_RENAME);
}
if ($force_download === FALSE) {
if (isset($_GET['destination']) === FALSE) {
// Should we send the user directly to the saved PDF? If so, do that.
if ($fillpdf_info->destination_redirect) {
drupal_goto(file_create_url($saved_file_path));
}
}
// Allow the "destination" query string parameter to be used
// e.g. fillpdf?nid=1&fid=1&destination=node/1
// If no destination is provided, drupal_goto() will send the
// user to the front page.
drupal_goto();
}
}
drupal_set_header('Pragma: public');
drupal_set_header('Expires: 0');
drupal_set_header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
drupal_set_header('Content-type:application/pdf');
drupal_set_header('Content-Length: ' . strlen($data));
drupal_set_header('Content-disposition:attachment; filename="' . $output_name . '"');
drupal_set_header('Content-Transfer-Encoding: binary');
echo $data;
exit;
}