You are here

class FillPdfServicePdfBackend in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Plugin/PdfBackend/FillPdfServicePdfBackend.php \Drupal\fillpdf\Plugin\PdfBackend\FillPdfServicePdfBackend

FillPDF Service PdfBackend plugin.

Plugin annotation


@PdfBackend(
  id = "fillpdf_service",
  label = @Translation("FillPDF Service"),
  description = @Translation(
    "No technical prerequisites.
     Sign up for <a href=':url'>FillPDF Service</a>.",
    arguments = {
      ":url" = "https://fillpdf.io"
    }
  ),
  weight = -10
)

Hierarchy

Expanded class hierarchy of FillPdfServicePdfBackend

File

src/Plugin/PdfBackend/FillPdfServicePdfBackend.php, line 29

Namespace

Drupal\fillpdf\Plugin\PdfBackend
View source
class FillPdfServicePdfBackend extends PdfBackendBase {

  /**
   * {@inheritdoc}
   */
  public function parseFile(FileInterface $template_file) {
    $pdf_content = file_get_contents($template_file
      ->getFileUri());
    return $this
      ->parseStream($pdf_content);
  }

  /**
   * {@inheritdoc}
   */
  public function parseStream($pdf_content) {
    $result = $this
      ->xmlRpcRequest('parse_pdf_fields', base64_encode($pdf_content));
    if ($result->error == TRUE) {

      // @todo: Throw an exception, log a message etc.
      return [];
    }

    // after setting error message
    $fields = $result->data;
    return $fields;
  }

  /**
   * Make an XML-RPC request.
   *
   * @param string $method
   *   The method to call. Additional arguments are the paramters to the
   *   xmlrpc() call.
   *
   * @return object
   *   Object with properties 'error' and 'data' representing the result of the
   *   request.
   */
  protected function xmlRpcRequest($method) {
    $url = $this->configuration['remote_protocol'] . '://' . $this->configuration['remote_endpoint'];
    $args = func_get_args();

    // Fix up the array for Drupal 7 xmlrpc() function style.
    $args = [
      $args[0] => array_slice($args, 1),
    ];
    $result = xmlrpc($url, $args);
    $ret = new \stdClass();
    if (isset($result['error'])) {
      $this
        ->messenger()
        ->addError($result['error']);
      $ret->error = TRUE;
    }
    elseif ($result == FALSE || xmlrpc_error()) {
      $error = xmlrpc_error();
      $ret->error = TRUE;
      $this
        ->messenger()
        ->addError($this
        ->t('There was a problem contacting the FillPDF service.
      It may be down, or you may not have internet access. [ERROR @code: @message]', [
        '@code' => $error->code,
        '@message' => $error->message,
      ]));
    }
    else {
      $ret->data = $result['data'];
      $ret->error = FALSE;
    }
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function mergeFile(FileInterface $template_file, array $field_mappings, array $context) {
    $pdf_content = file_get_contents($template_file
      ->getFileUri());
    return $this
      ->mergeStream($pdf_content, $field_mappings, $context);
  }

  /**
   * {@inheritdoc}
   */
  public function mergeStream($pdf_content, array $field_mappings, array $context) {
    $api_key = $this->configuration['fillpdf_service_api_key'];
    $fields = $images = [];
    foreach ($field_mappings as $pdf_key => $mapping) {
      if ($mapping instanceof TextFieldMapping) {
        $fields[$pdf_key] = (string) $mapping;
      }
      elseif ($mapping instanceof ImageFieldMapping) {

        // Anonymize image data from the fields array; we should not send the
        // real filename to FillPDF Service. We do this in the specific backend
        // because other plugin types may need the filename on the local system.
        $field_path_info = pathinfo($mapping
          ->getUri());
        $fields[$pdf_key] = '{image}' . md5($field_path_info['filename']) . '.' . $field_path_info['extension'];
        $images[$pdf_key] = [
          'data' => base64_encode($mapping
            ->getData()),
          'filenamehash' => md5($field_path_info['filename']) . '.' . $field_path_info['extension'],
        ];
      }
    }
    $result = $this
      ->xmlRpcRequest('merge_pdf_v3', base64_encode($pdf_content), $fields, $api_key, $context['flatten'], $images);
    if ($result->error === FALSE && $result->data) {
      $populated_pdf = base64_decode($result->data);
      return $populated_pdf;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FillPdfServicePdfBackend::mergeFile public function Populate a PDF file with field data. Overrides PdfBackendInterface::mergeFile
FillPdfServicePdfBackend::mergeStream public function Populate a PDF file with field data. Overrides PdfBackendInterface::mergeStream
FillPdfServicePdfBackend::parseFile public function Parse a PDF and return a list of its fields. Overrides PdfBackendInterface::parseFile
FillPdfServicePdfBackend::parseStream public function Parse a PDF and return a list of its fields. Overrides PdfBackendInterface::parseStream
FillPdfServicePdfBackend::xmlRpcRequest protected function Make an XML-RPC request.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
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 2
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.