Skip to content
Snippets Groups Projects
Commit ac3f0ba1 authored by Bernd Oliver Suenderhauf's avatar Bernd Oliver Suenderhauf
Browse files

Issue #3022450 by Anas_maw, Pancho: Image being encoded twice when using FillPDF LocalServer

parent 86fd86a1
No related branches found
No related tags found
No related merge requests found
...@@ -368,8 +368,23 @@ class HandlePdfController extends ControllerBase { ...@@ -368,8 +368,23 @@ class HandlePdfController extends ControllerBase {
* Array of image data, keyed by the PDF key. * Array of image data, keyed by the PDF key.
*/ */
protected function processImageTokens(FileInterface $image_file, array &$mapped_fields, $pdf_key, array &$image_data) { 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(); $image_path = $image_file->getFileUri();
$mapped_fields[$pdf_key] = "{image}{$image_path}"; $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); $image_path_info = pathinfo($image_path);
// Store the image data to transmit to the remote service if necessary. // Store the image data to transmit to the remote service if necessary.
$file_data = file_get_contents($image_path); $file_data = file_get_contents($image_path);
......
...@@ -4,24 +4,36 @@ namespace Drupal\fillpdf\FieldMapping; ...@@ -4,24 +4,36 @@ namespace Drupal\fillpdf\FieldMapping;
use 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 { 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 * @var string
* data sent through.
*/ */
protected $extension; protected $extension;
/** /**
* @param $data * Constructs an ImageFieldMapping object.
* @param $extension *
* The original extension corresponding to the image data. Not all consumers * @param string $data
* actually need this information, and if you know that yours doesn't, you * String containing the image data, as returned by file_get_contents() and
* can leave it blank. * 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 * @throws \InvalidArgumentException
* If the extension isn't one of 'jpg', 'png', or 'gif'.
*/ */
public function __construct($data, $extension = NULL) { public function __construct($data, $extension = NULL) {
parent::__construct($data); parent::__construct($data);
...@@ -34,7 +46,10 @@ class ImageFieldMapping extends FieldMapping { ...@@ -34,7 +46,10 @@ class ImageFieldMapping extends FieldMapping {
} }
/** /**
* Gets the image file's extension.
*
* @return string * @return string
* The file's extension.
*/ */
public function getExtension() { public function getExtension() {
return $this->extension; return $this->extension;
......
...@@ -126,7 +126,7 @@ class LocalService implements FillPdfBackendPluginInterface, ContainerFactoryPlu ...@@ -126,7 +126,7 @@ class LocalService implements FillPdfBackendPluginInterface, ContainerFactoryPlu
// Remove {image} marker. // Remove {image} marker.
$image_filepath = substr($field, 7); $image_filepath = substr($field, 7);
$image_realpath = $this->fileSystem->realpath($image_filepath); $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 { else {
$mapping_objects[$key] = new TextFieldMapping($field); $mapping_objects[$key] = new TextFieldMapping($field);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment