You are here

function fillpdf_execute_merge in FillPDF 7.2

Same name and namespace in other branches
  1. 6 fillpdf.module \fillpdf_execute_merge()
  2. 7 fillpdf.module \fillpdf_execute_merge()

Parameters

string $method The service or program being used. Possible values: local, remote, pdftk. Currently, only pdftk is supported.:

array $fields The fields to merge into the PDF. Should be retrieved from the {fillpdf_fields} table.:

mixed $fillpdf When in URL mode, this is the record from {fillpdf_forms}. When in Stream mode, this is the PDF data.:

string $mode A special flag to control the behavior of this function. URL mode merges using a PDF on the: file system and Stream mode merges using the value of $fillpdf directly. Possible values: url, stream.

Deprecated

@todo: Port to PDF Forms API

Utility function to allow other functions to merge PDFs with the various methods in a consistent way.

File

./fillpdf.module, line 822
Allows mappings of PDFs to site content

Code

function fillpdf_execute_merge($method, $fields, $fillpdf, $mode = 'url', $flatten = TRUE) {
  $data = NULL;

  // Try to prepare the data so that the $method part can process it without caring too much about merge tool
  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;
  }
  switch ($method) {
    case 'pdftk':
      module_load_include('inc', 'pdf_forms', 'pdf_forms.xfdf');

      // Looks like I'm the first actually to use this! (wizonesolutions)
      $xfdfname = $filename . '.xfdf';
      $xfdf = pdf_forms_xfdf_create(basename($xfdfname), $fields);

      // Generate the file
      $xfdffile = file_save_data($xfdf, $xfdfname, FILE_EXISTS_RENAME);

      // Now feed this to pdftk and save the result to a variable
      $path_to_pdftk = variable_get('fillpdf_pdftk_path', 'pdftk');
      ob_start();
      passthru($path_to_pdftk . ' ' . escapeshellarg(drupal_realpath($filename)) . ' fill_form ' . escapeshellarg(drupal_realpath($xfdffile->uri)) . ' output - ' . ($flatten ? 'flatten ' : '') . 'drop_xfa');
      $data = ob_get_clean();
      if ($data === FALSE) {
        drupal_set_message(t('pdftk not properly installed. No PDF generated.'), 'error');
      }
      file_delete($xfdffile);
      if ($mode == 'stream') {
        file_unmanaged_delete($filename);
      }
      break;
  }
  if ($data) {
    return $data;
  }
  else {
    return FALSE;
  }
}