diff --git a/src/Controller/HandlePdfController.php b/src/Controller/HandlePdfController.php index dd32bd1966d4598f8f89d8d48b1b1703d4b27cf5..03489fea80bceeae4cc7fe84de8069bdba18c953 100644 --- a/src/Controller/HandlePdfController.php +++ b/src/Controller/HandlePdfController.php @@ -368,8 +368,23 @@ class HandlePdfController extends ControllerBase { * Array of image data, keyed by the PDF key. */ protected function processImageTokens(FileInterface $image_file, array &$mapped_fields, $pdf_key, array &$image_data) { + $backend = $this->config('fillpdf.settings')->get('backend'); + + // @todo Refactor in 8.x-5.x. Pdftk doesn't support image stamping. + if ($backend == 'pdftk') { + return; + } + $image_path = $image_file->getFileUri(); $mapped_fields[$pdf_key] = "{image}{$image_path}"; + + // @todo Refactor in 8.x-5.x. Local and LocalService backends handle image + // files themselves. So this only remains in place for FillPdfService + // and possible third-party backend plugins. + if (in_array($backend, ['local_service', 'local'])) { + return; + } + $image_path_info = pathinfo($image_path); // Store the image data to transmit to the remote service if necessary. $file_data = file_get_contents($image_path); diff --git a/src/FieldMapping/ImageFieldMapping.php b/src/FieldMapping/ImageFieldMapping.php index 81334c0d41b5fa9a354ce004ba951236c71064bb..2f300139b2e62cef7bdde71053fcc24c1907331f 100644 --- a/src/FieldMapping/ImageFieldMapping.php +++ b/src/FieldMapping/ImageFieldMapping.php @@ -4,24 +4,36 @@ namespace Drupal\fillpdf\FieldMapping; use Drupal\fillpdf\FieldMapping; +/** + * Represents a mapping between a PDF image field and a merge value. + * + * ImageFieldMapping objects are immutable; replace the value by calling the + * constructor again if the value needs to change. + */ class ImageFieldMapping extends FieldMapping { /** - * @var string + * The image file's extension. + * + * May be 'jpg', 'png', or 'gif'. * - * The common extension (jpg, png, or gif) corresponding to the type of image - * data sent through. + * @var string */ protected $extension; /** - * @param $data - * @param $extension - * The original extension corresponding to the image data. Not all consumers - * actually need this information, and if you know that yours doesn't, you - * can leave it blank. + * Constructs an ImageFieldMapping object. + * + * @param string $data + * String containing the image data, as returned by file_get_contents() and + * not encoded. + * @param string $extension + * (optional) The original extension corresponding to the image data. If the + * backend doesn't need to know the extension and you don't want extensions + * to be checked, you can leave it blank. * * @throws \InvalidArgumentException + * If the extension isn't one of 'jpg', 'png', or 'gif'. */ public function __construct($data, $extension = NULL) { parent::__construct($data); @@ -34,7 +46,10 @@ class ImageFieldMapping extends FieldMapping { } /** + * Gets the image file's extension. + * * @return string + * The file's extension. */ public function getExtension() { return $this->extension; diff --git a/src/Plugin/FillPdfBackend/LocalService.php b/src/Plugin/FillPdfBackend/LocalService.php index 54fb60d29a28eefc9394026c305d680065112d66..1a571802c1b9b06dec70a2187204d191ddc3e0c1 100644 --- a/src/Plugin/FillPdfBackend/LocalService.php +++ b/src/Plugin/FillPdfBackend/LocalService.php @@ -126,7 +126,7 @@ class LocalService implements FillPdfBackendPluginInterface, ContainerFactoryPlu // Remove {image} marker. $image_filepath = substr($field, 7); $image_realpath = $this->fileSystem->realpath($image_filepath); - $mapping_objects[$key] = new ImageFieldMapping(base64_encode(file_get_contents($image_realpath))); + $mapping_objects[$key] = new ImageFieldMapping(file_get_contents($image_realpath), pathinfo($image_filepath, PATHINFO_EXTENSION)); } else { $mapping_objects[$key] = new TextFieldMapping($field);