You are here

public function FillPdfServicePdfBackend::mergeStream 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::mergeStream()

Populate a PDF file with field data.

Parameters

string $pdf_content: 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::mergeStream

See also

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

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

File

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

Class

FillPdfServicePdfBackend
FillPDF Service PdfBackend plugin.

Namespace

Drupal\fillpdf\Plugin\PdfBackend

Code

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;
  }
}