From 786f7f79eb4cb74af87d4a02757ec928ff824e36 Mon Sep 17 00:00:00 2001
From: Kevin Kaland <kevin@wizone.solutions>
Date: Wed, 29 Jan 2020 00:43:38 +0100
Subject: [PATCH] Issue #3105191 by wizonesolutions, liquidcms: Add API call to
 create PDFs

This fixes error reporting when something goes wrong with the underlying
backend.
---
 src/Controller/HandlePdfController.php |  3 +++
 src/Service/BackendProxy.php           | 20 +++++++++++++++-----
 2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/src/Controller/HandlePdfController.php b/src/Controller/HandlePdfController.php
index 784b7f9..eb27ec6 100644
--- a/src/Controller/HandlePdfController.php
+++ b/src/Controller/HandlePdfController.php
@@ -134,6 +134,7 @@ class HandlePdfController extends ControllerBase {
    *
    * @throws \InvalidArgumentException
    * @throws \Drupal\Component\Plugin\Exception\PluginException
+   * @throws \Drupal\Core\Entity\EntityMalformedException
    *   If one of the passed arguments is missing or does not pass the
    *   validation.
    */
@@ -198,6 +199,8 @@ class HandlePdfController extends ControllerBase {
    * @return \Symfony\Component\HttpFoundation\Response
    *   The action plugin's response object.
    *
+   * @throws \Drupal\Component\Plugin\Exception\PluginException
+   * @throws \Drupal\Core\Entity\EntityMalformedException
    * @see \Drupal\fillpdf\FillPdfLinkManipulatorInterface::parseLink()
    */
   protected function handlePopulatedPdf(FillPdfFormInterface $fillpdf_form, $pdf_data, array $context, $filename, array $entities) {
diff --git a/src/Service/BackendProxy.php b/src/Service/BackendProxy.php
index b7899ed..ed1ed46 100644
--- a/src/Service/BackendProxy.php
+++ b/src/Service/BackendProxy.php
@@ -67,10 +67,10 @@ class BackendProxy implements BackendProxyInterface {
     ];
 
     // Populate mappings array.
-    $field_mappings = [];
+    $fieldMappings = [];
     foreach ($fillPdfForm->getFormFields() as $pdf_key => $field) {
       if ($mergeOptions['sample']) {
-        $field_mappings[$pdf_key] = new TextFieldMapping($pdf_key);
+        $fieldMappings[$pdf_key] = new TextFieldMapping($pdf_key);
       }
       else {
         $options = [];
@@ -88,7 +88,7 @@ class BackendProxy implements BackendProxyInterface {
 
         // Resolve tokens.
         $text = count($field->value) ? $field->value->value : '';
-        $field_mappings[$pdf_key] = $this->tokenResolver->replace($text, $entities, $options);
+        $fieldMappings[$pdf_key] = $this->tokenResolver->replace($text, $entities, $options);
       }
     }
 
@@ -98,8 +98,18 @@ class BackendProxy implements BackendProxyInterface {
 
     // @todo: Emit event (or call alter hook?) before populating PDF.
     // Rename fillpdf_merge_fields_alter() to fillpdf_populate_fields_alter().
-    $template_file = File::load($fillPdfForm->file->target_id);
-    return $backend->mergeFile($template_file, $field_mappings, $mergeOptions);
+    /** @var \Drupal\file\FileInterface $templateFile */
+    $templateFile = File::load($fillPdfForm->file->target_id);
+
+    $mergedPdf = $backend->mergeFile($templateFile, $fieldMappings, $mergeOptions);
+
+    if (!is_string($mergedPdf)) {
+      // Make sure we return a string as not to get an error. The underlying
+      // backend will already have set more detailed errors.
+      $mergedPdf = '';
+    }
+
+    return $mergedPdf;
   }
 
 }
-- 
GitLab