View source
<?php
namespace Drupal\fillpdf_legacy\Plugin\PdfBackend;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\file\FileInterface;
use Drupal\fillpdf\Entity\FillPdfForm;
use Drupal\fillpdf\FieldMapping\ImageFieldMapping;
use Drupal\fillpdf\FieldMapping\TextFieldMapping;
use Drupal\fillpdf_legacy\Plugin\FillPdfBackendManager;
use Drupal\fillpdf\Plugin\PdfBackendBase;
use Drupal\fillpdf\FillPdfBackendPluginInterface;
use Drupal\fillpdf\FillPdfFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class LegacyProviderPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInterface, FillPdfBackendPluginInterface {
private $legacyBackend;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, FillPdfBackendManager $legacy_backend_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->legacyBackend = $legacy_backend_manager
->createInstance($configuration['backend'], $configuration);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.fillpdf_backend'));
}
public function parse(FillPdfFormInterface $fillpdf_form) {
return $this->legacyBackend
->parse($fillpdf_form);
}
public function parseFile(FileInterface $template_file) {
$fillpdf_form = FillPdfForm::create([
'file' => $template_file,
]);
return $this
->parse($fillpdf_form);
}
public function parseStream($pdf_content) {
$template_file = file_save_data($pdf_content);
return $this
->parseFile($template_file);
}
public function populateWithFieldData(FillPdfFormInterface $fillpdf_form, array $field_mapping, array $context) {
return $this->legacyBackend
->populateWithFieldData($fillpdf_form, $field_mapping, $context);
}
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);
}
public function mergeStream($pdf_content, array $field_mappings, array $context) {
$template_file = file_save_data($pdf_content);
return $this
->mergeFile($template_file);
}
}