/**
   * @inheritdoc
   */
  public function populateWithFieldData(FillPdfFormInterface $pdf_form, array $field_mapping, array $context) {
    /** @var FileInterface $original_file */
    $original_file = File::load($pdf_form->file->target_id);
    $filename = $original_file->getFileUri();
    $fields = $field_mapping['fields'];

    module_load_include('inc', 'fillpdf', 'xfdf');
    $xfdfname = $filename . '.xfdf';
    $xfdf = create_xfdf(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 = $this->getPdftkPath();
    ob_start();
    passthru($path_to_pdftk . ' ' . escapeshellarg($this->fileSystem->realpath($filename)) . ' fill_form ' . escapeshellarg($this->fileSystem->realpath($xfdffile->getFileUri())) . ' output - ' . ($context['flatten'] ? 'flatten ' : '') . 'drop_xfa');
    $data = ob_get_clean();
    if ($data === FALSE) {
      drupal_set_message($this->t('pdftk not properly installed. No PDF generated.'), 'error');
    }
    $xfdffile->delete();

    if ($data) {
      return $data;
    }

    return FALSE;
  }
  /**
   * @inheritdoc
   */
  public function populateWithFieldData(FillPdfFormInterface $pdf_form, array $field_mapping, array $context) {
    /** @var FileInterface $original_file */
    $original_file = File::load($pdf_form->file->target_id);
    $pdf_data = file_get_contents($original_file->getFileUri());
    $fields = $field_mapping['fields'];

    $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($pdf_data), 'bytes');
      foreach ($fields as $key => $field) {
        if (substr($field, 0, 7) == '{image}') {
          // Remove {image} marker.
          $image_filepath = substr($field, 7);
          $image_realpath = $this->fileSystem->realpath($image_filepath);
          $fillpdf->image($key, $image_realpath, 'file');
        }
        else {
          $fillpdf->text($key, $field);
        }
      }
    }
    catch (\JavaException $e) {
      drupal_set_message(java_truncate((string) $e), 'error');
      return NULL;
    }
    try {
      if ($context['flatten']) {
        $populated_pdf = java_values(base64_decode($fillpdf->toByteArray()));
      }
      else {
        $populated_pdf = java_values(base64_decode($fillpdf->toByteArrayUnflattened()));
      }
    }
    catch (\JavaException $e) {
      drupal_set_message(java_truncate((string) $e), 'error');
      return NULL;
    }

    return $populated_pdf;
  }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 public function upload(EmbridgeAssetEntityInterface $asset, array $metadata = [])
 {
     $this->login();
     $file_path = $this->fileSystem->realpath($asset->getSourcePath());
     $filename = $asset->getFilename();
     // Sanitize metadata values.
     $metadata = array_filter($metadata, 'is_scalar');
     if (isset($metadata['libraries'])) {
         // The libraries value won't stick unless it is a string.
         $metadata['libraries'] = (string) $metadata['libraries'];
     }
     // Build the main request data.
     $json_values = ['description' => $filename];
     $json_values = array_merge($json_values, $metadata);
     $json_request = $this->jsonEncoder->encode($json_values);
     $body = ['multipart' => [['name' => 'jsonrequest', 'contents' => $json_request], ['name' => 'file', 'contents' => file_get_contents($file_path), 'filename' => $filename]]];
     $response_body = $this->doRequest(self::EMBRIDGE_UPLOAD_PATH_DEFAULT, $body);
     $asset->setAssetId($response_body['data']['id']);
     $asset->setSourcePath($response_body['data']['sourcepath']);
     return $asset;
 }