You are here

public function LegacyProviderPdfBackend::mergeFile in FillPDF 8.4

Populate a PDF file with field data.

Parameters

\Drupal\file\FileInterface $template_file: The PDF template the field values specified in the mapping should be merged into.

\Drupal\fillpdf\FieldMapping[] $field_mappings: An array of FieldMapping objects mapping PDF field keys to the values they should be replaced with. Example:

[
  'Foo' => new TextFieldMapping('bar'),
  'Foo2' => new TextFieldMapping('bar2'),
  'Image1' => new ImageFieldMapping(base64_encode(file_get_contents($image)), 'jpg'),
];

array $context: The request context as returned by FillPdfLinkManipulator::parseLink().

Return value

string|null The raw file contents of the new PDF, or NULL if merging failed. The caller has to handle saving or serving the file accordingly.

Overrides PdfBackendInterface::mergeFile

See also

\Drupal\fillpdf\Plugin\PdfBackendInterface::mergeStream()

1 call to LegacyProviderPdfBackend::mergeFile()
LegacyProviderPdfBackend::mergeStream in modules/fillpdf_legacy/src/Plugin/PdfBackend/LegacyProviderPdfBackend.php
Populate a PDF file with field data.

File

modules/fillpdf_legacy/src/Plugin/PdfBackend/LegacyProviderPdfBackend.php, line 103

Class

LegacyProviderPdfBackend
Legacy provider PdfBackend plugin.

Namespace

Drupal\fillpdf_legacy\Plugin\PdfBackend

Code

public function mergeFile(FileInterface $template_file, array $field_mappings, array $context) {
  $legacy_mapping = [];
  foreach ($field_mappings as $pdf_key => $mapping) {
    if ($mapping instanceof TextFieldMapping) {
      $legacy_mapping['fields'][$pdf_key] = (string) $mapping
        ->getData();
    }
    elseif ($mapping instanceof ImageFieldMapping) {
      $uri = (string) $mapping
        ->getUri();
      if ($uri) {
        $legacy_mapping['fields'][$pdf_key] = "{image}{$uri}";
        $image_path_info = pathinfo($uri);
        $legacy_mapping['images'][$pdf_key] = [
          'data' => base64_encode($mapping
            ->getData()),
          'filenamehash' => md5($image_path_info['filename']) . '.' . $image_path_info['extension'],
        ];
      }
    }
  }
  $fillpdf_form = FillPdfForm::create([
    'file' => $template_file,
  ]);
  return $this->legacyBackend
    ->populateWithFieldData($fillpdf_form, $legacy_mapping, $context);
}