You are here

class JavaBridgeFillPdfBackend in FillPDF 8.4

Legacy JavaBridge FillPdfBackend plugin.

Plugin annotation


@Plugin(
  id = "local"
)

Hierarchy

Expanded class hierarchy of JavaBridgeFillPdfBackend

Deprecated

in fillpdf:8.x-4.9 and is removed from fillpdf:8.x-5.0. Use FillPDF LocalServer with the new LocalServerPdfBackend plugin instead.

See also

https://www.drupal.org/node/3059476

\Drupal\fillpdf\Plugin\PdfBackend\LocalServerPdfBackend

File

modules/fillpdf_legacy/src/Plugin/FillPdfBackend/JavaBridgeFillPdfBackend.php, line 25

Namespace

Drupal\fillpdf_legacy\Plugin\FillPdfBackend
View source
class JavaBridgeFillPdfBackend extends PluginBase implements FillPdfBackendPluginInterface, ContainerFactoryPluginInterface {

  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystem
   */
  protected $fileSystem;

  /**
   * Constructs a LocalFillPdfBackend plugin object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param array $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\File\FileSystem $file_system
   *   The file system.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, FileSystem $file_system) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  public function parse(FillPdfFormInterface $fillpdf_form) {

    /** @var \Drupal\file\FileInterface $file */
    $file = File::load($fillpdf_form->file->target_id);
    $content = file_get_contents($file
      ->getFileUri());
    $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) {
      $this
        ->messenger()
        ->addError(java_truncate((string) $e));
    }
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function populateWithFieldData(FillPdfFormInterface $fillpdf_form, array $field_mapping, array $context) {

    /** @var \Drupal\file\FileInterface $original_file */
    $original_file = File::load($fillpdf_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) {
      $this
        ->messenger()
        ->addError(java_truncate((string) $e));
      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) {
      $this
        ->messenger()
        ->addError(java_truncate((string) $e));
      return NULL;
    }
    return $populated_pdf;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
JavaBridgeFillPdfBackend::$fileSystem protected property The file system.
JavaBridgeFillPdfBackend::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
JavaBridgeFillPdfBackend::parse public function Parse a PDF and return a list of its fields. Overrides FillPdfBackendPluginInterface::parse
JavaBridgeFillPdfBackend::populateWithFieldData public function Populate a FillPDF form with field data. Overrides FillPdfBackendPluginInterface::populateWithFieldData
JavaBridgeFillPdfBackend::__construct public function Constructs a LocalFillPdfBackend plugin object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.