You are here

class PdftkPdfBackend in FillPDF 5.0.x

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

Pdftk PdfBackend plugin.

Plugin annotation


@PdfBackend(
  id = "pdftk",
  label = @Translation("pdftk"),
  description = @Translation(
    "Locally installed pdftk. You will need a VPS or a dedicated server to
    install pdftk, see <a href=':url'>documentation</a>.",
    arguments = {
      ":url" = "https://www.drupal.org/docs/8/modules/fillpdf"
    }
  ),
  weight = -5
)

Hierarchy

Expanded class hierarchy of PdftkPdfBackend

2 files declare their use of PdftkPdfBackend
fillpdf.install in ./fillpdf.install
Install functions for FillPDF.
FillPdfForm.php in src/Entity/FillPdfForm.php

File

src/Plugin/PdfBackend/PdftkPdfBackend.php, line 36

Namespace

Drupal\fillpdf\Plugin\PdfBackend
View source
class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInterface {

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

  /**
   * The FillPDF shell manager.
   *
   * @var \Drupal\fillpdf\ShellManager
   */
  protected $shellManager;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a PdftkPdfBackend 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.
   * @param \Drupal\fillpdf\ShellManager $shell_manager
   *   The FillPDF shell manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(array $configuration, $plugin_id, array $plugin_definition, FileSystem $file_system, ShellManager $shell_manager, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->fileSystem = $file_system;
    $this->shellManager = $shell_manager;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@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'), $container
      ->get('fillpdf.shell_manager'), $container
      ->get('entity_type.manager'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function parseFile(FileInterface $template_file) {
    $template_uri = $template_file
      ->getFileUri();
    $pdftk_path = $this
      ->getPdftkPath();
    $status = FillPdf::checkPdftkPath($pdftk_path);
    if ($status === FALSE) {
      $this
        ->messenger()
        ->addError($this
        ->t('pdftk not properly installed.'));
      return [];
    }

    // Escape the template's realpath.
    $template_path = $this->shellManager
      ->escapeShellArg($this->fileSystem
      ->realpath($template_uri));

    // Use exec() to call pdftk (because it will be easier to go line-by-line
    // parsing the output) and pass $content via stdin. Retrieve the fields with
    // dump_data_fields_utf8().
    $output = [];
    exec("{$pdftk_path} {$template_path} dump_data_fields_utf8", $output, $status);
    if (count($output) === 0) {
      $this
        ->messenger()
        ->addWarning($this
        ->t('PDF does not contain fillable fields.'));
      return [];
    }

    // Build a simple map of dump_data_fields_utf8 keys to our own array keys.
    $data_fields_map = [
      'FieldType' => 'type',
      'FieldName' => 'name',
      'FieldFlags' => 'flags',
      'FieldValue' => 'value',
      'FieldJustification' => 'justification',
    ];

    // Build the fields array.
    $fields = [];
    $fieldindex = -1;
    foreach ($output as $lineitem) {
      if ($lineitem == '---') {
        $fieldindex++;
        continue;
      }

      // Separate the data key from the data value.
      list($key, $value) = explode(':', $lineitem);
      if (in_array($key, array_keys($data_fields_map), NULL)) {

        // Trim spaces.
        $fields[$fieldindex][$data_fields_map[$key]] = trim($value);
      }
    }
    return $fields;
  }

  /**
   * Returns the configured path to the local pdftk installation.
   *
   * @return string
   *   The configured path to the local pdftk installation.
   *
   * @internal
   */
  protected function getPdftkPath() {
    return isset($this->configuration['pdftk_path']) ? $this->configuration['pdftk_path'] : 'pdftk';
  }

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

  /**
   * {@inheritdoc}
   */
  public function mergeFile(FileInterface $template_file, array $field_mappings, array $context) {
    $template_uri = $template_file
      ->getFileUri();
    $fields = [];
    foreach ($field_mappings as $pdf_key => $mapping) {
      if ($mapping instanceof TextFieldMapping) {
        $fields[$pdf_key] = (string) $mapping;
      }
    }
    module_load_include('inc', 'fillpdf', 'xfdf');
    $xfdf_name = $template_uri . '.xfdf';
    $xfdf = create_xfdf(basename($xfdf_name), $fields);

    // Generate the file.
    $xfdf_file = file_save_data($xfdf, $xfdf_name, FileSystemInterface::EXISTS_RENAME);

    // @todo: Improve this approach when we turn $context into a value object.
    if (!isset($context['fid'])) {
      throw new InvalidArgumentException("pdftk requires \$context['fid'] to be set to the ID of the FillPDF Form so that it can check if encryption is configured. The merge was aborted because it was not set.");
    }
    $fillpdf_form = $this->entityTypeManager
      ->getStorage('fillpdf_form')
      ->load($context['fid']);

    // Configure PDF security.
    $arg_permissions = $arg_owner_password = $arg_user_password = '';
    $arg_pdftk_encryption = $fillpdf_form->pdftk_encryption->value ? " {$fillpdf_form->pdftk_encryption->value}" : '';
    $permissions = $fillpdf_form->permissions
      ->getString();
    if ($permissions) {

      // ItemList::getString() returns "Item1, Item2", but we don't want commas.
      $arg_permissions = ' allow ' . str_replace(',', '', $permissions);
    }
    $owner_password = $fillpdf_form->owner_password->value;
    if ($owner_password) {
      $arg_owner_password = ' owner_pw ' . $this->shellManager
        ->escapeShellArg($owner_password);
    }
    $user_password = $fillpdf_form->user_password->value;
    if ($user_password) {
      $arg_user_password = ' user_pw ' . $this->shellManager
        ->escapeShellArg($user_password);
    }

    // Escape the template's and the XFDF file's realpath.
    $template_path = $this->shellManager
      ->escapeShellArg($this->fileSystem
      ->realpath($template_uri));
    $xfdf_path = $this->shellManager
      ->escapeShellArg($this->fileSystem
      ->realpath($xfdf_file
      ->getFileUri()));

    // Now feed this to pdftk and save the result to a variable.
    $pdftk_path = $this
      ->getPdftkPath();
    ob_start();
    $command = "{$pdftk_path} {$template_path} fill_form {$xfdf_path} output - " . ($context['flatten'] ? 'flatten drop_xfa' : '') . "{$arg_pdftk_encryption}{$arg_permissions}{$arg_owner_password}{$arg_user_password}";
    passthru($command);
    $data = ob_get_clean();
    if ($data === FALSE) {
      $this
        ->messenger()
        ->addError($this
        ->t('pdftk not properly installed. No PDF generated.'));
    }
    $xfdf_file
      ->delete();
    if ($data) {
      return $data;
    }
    return NULL;
  }

  /**
   * Get valid PDFtk encryption options.
   *
   * @return array
   *   The valid encryption options.
   */
  public static function getEncryptionOptions() : array {
    return [
      '' => t('No encryption (Default)'),
      'encrypt_128bit' => t('128-bit encryption (Recommended)'),
      'encrypt_40bit' => t('40-bit encryption'),
    ];
  }

  /**
   * Return a list of available user permissions for configuring PDF security.
   *
   * @return array
   *   The permission list.
   */
  public static function getUserPermissionList() : array {
    return [
      'Printing' => t('Printing (Top Quality Printing)'),
      'DegradedPrinting' => t('DegradedPrinting (Lower Quality Printing)'),
      'ModifyContents' => t('ModifyContents (Also allows <em>Assembly</em>)'),
      'Assembly' => t('Assembly'),
      'CopyContents' => t('CopyContents (Also allows <em>ScreenReaders</em>)'),
      'ScreenReaders' => t('ScreenReaders'),
      'ModifyAnnotations' => t('ModifyAnnotations (Also allows <em>FillIn</em>)'),
      'FillIn' => t('FillIn'),
      'AllFeatures' => t('AllFeatures (Allows the user to perform all of the above, and top quality printing.)'),
    ];
  }

}

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
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PdftkPdfBackend::$entityTypeManager protected property The entity type manager.
PdftkPdfBackend::$fileSystem protected property The file system.
PdftkPdfBackend::$shellManager protected property The FillPDF shell manager.
PdftkPdfBackend::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PdftkPdfBackend::getEncryptionOptions public static function Get valid PDFtk encryption options.
PdftkPdfBackend::getPdftkPath protected function Returns the configured path to the local pdftk installation.
PdftkPdfBackend::getUserPermissionList public static function Return a list of available user permissions for configuring PDF security.
PdftkPdfBackend::mergeFile public function Populate a PDF file with field data. Overrides PdfBackendInterface::mergeFile
PdftkPdfBackend::mergeStream public function Populate a PDF file with field data. Overrides PdfBackendInterface::mergeStream
PdftkPdfBackend::parseFile public function Parse a PDF and return a list of its fields. Overrides PdfBackendInterface::parseFile
PdftkPdfBackend::parseStream public function Parse a PDF and return a list of its fields. Overrides PdfBackendInterface::parseStream
PdftkPdfBackend::__construct public function Constructs a PdftkPdfBackend plugin object. Overrides PluginBase::__construct
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.
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.