From 442be3b94aa8a418041a828bfa5220d2eb1669f2 Mon Sep 17 00:00:00 2001 From: demeritcowboy Date: Fri, 17 Sep 2021 15:31:53 -0400 Subject: [PATCH] allow setting some dompdf options --- CRM/Admin/Form/Setting/Miscellaneous.php | 3 + CRM/Utils/PDF/Utils.php | 29 ++++++-- settings/Core.setting.php | 72 +++++++++++++++++++ .../CRM/Admin/Form/Setting/Miscellaneous.tpl | 18 +++++ tests/phpunit/CRM/Utils/PDF/UtilsTest.php | 33 +++++++++ 5 files changed, 151 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/CRM/Utils/PDF/UtilsTest.php diff --git a/CRM/Admin/Form/Setting/Miscellaneous.php b/CRM/Admin/Form/Setting/Miscellaneous.php index 1540e6a52d..dfe75c761f 100644 --- a/CRM/Admin/Form/Setting/Miscellaneous.php +++ b/CRM/Admin/Form/Setting/Miscellaneous.php @@ -31,6 +31,9 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { 'recordGeneratedLetters' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'secondDegRelPermissions' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'checksum_timeout' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'dompdf_font_dir' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'dompdf_chroot' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'dompdf_enable_remote' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'wkhtmltopdfPath' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'recentItemsMaxCount' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'recentItemsProviders' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, diff --git a/CRM/Utils/PDF/Utils.php b/CRM/Utils/PDF/Utils.php index 2459ce3af2..8c97d681a8 100644 --- a/CRM/Utils/PDF/Utils.php +++ b/CRM/Utils/PDF/Utils.php @@ -179,10 +179,7 @@ class CRM_Utils_PDF_Utils { * @return string */ public static function _html2pdf_dompdf($paper_size, $orientation, $html, $output, $fileName) { - // CRM-12165 - Remote file support required for image handling. - $options = new Options(); - $options->set('isRemoteEnabled', TRUE); - + $options = self::getDompdfOptions(); $dompdf = new DOMPDF($options); $dompdf->set_paper($paper_size, $orientation); $dompdf->load_html($html); @@ -300,4 +297,28 @@ class CRM_Utils_PDF_Utils { return $value; } + /** + * Allow setting some dompdf options. + * + * We don't support all the available dompdf options. + * + * @return \Dompdf\Options + */ + private static function getDompdfOptions(): Options { + $options = new Options(); + $settings = [ + // CRM-12165 - Remote file support required for image handling so default to TRUE + 'enable_remote' => \Civi::settings()->get('dompdf_enable_remote') ?? TRUE, + ]; + // only set these ones if a setting exists for them + foreach (['font_dir', 'chroot', 'log_output_file'] as $setting) { + $value = \Civi::settings()->get("dompdf_$setting"); + if (isset($value)) { + $settings[$setting] = $value; + } + } + $options->set($settings); + return $options; + } + } diff --git a/settings/Core.setting.php b/settings/Core.setting.php index 64778e4d87..b01373382d 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -529,6 +529,78 @@ return [ 'callback' => 'CRM_Contact_Form_Task_PDFLetterCommon::getLoggingOptions', ], ], + 'dompdf_font_dir' => [ + 'is_domain' => 1, + 'is_contact' => 0, + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'dompdf_font_dir', + 'title' => ts('DOMPDF Font Folder'), + 'description' => ts('Additional folder where DOMPDF will look for fonts.'), + 'type' => 'String', + 'quick_form_type' => 'Element', + 'html_type' => 'text', + 'html_attributes' => [ + 'size' => 64, + 'maxlength' => 256, + ], + 'default' => NULL, + 'help_text' => NULL, + 'add' => '5.43', + ], + 'dompdf_chroot' => [ + 'is_domain' => 1, + 'is_contact' => 0, + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'dompdf_chroot', + 'title' => ts('DOMPDF Local Images Folder'), + 'description' => ts('Folder to restrict where DOMPDF looks when loading local images. By default it is the DOMPDF folder itself for security reasons. It will search in subfolders.'), + 'type' => 'String', + 'quick_form_type' => 'Element', + 'html_type' => 'text', + 'html_attributes' => [ + 'size' => 64, + 'maxlength' => 256, + ], + 'default' => NULL, + 'help_text' => NULL, + 'add' => '5.43', + ], + 'dompdf_enable_remote' => [ + 'is_domain' => 1, + 'is_contact' => 0, + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'dompdf_enable_remote', + 'title' => ts('DOMPDF Enable Remote Images'), + 'description' => ts('Enable the use of remote images. By default this is enabled, but if not using remote images you may wish to turn it off for security reasons.'), + 'type' => 'Boolean', + 'quick_form_type' => 'YesNo', + 'html_type' => '', + 'default' => TRUE, + 'help_text' => NULL, + 'add' => '5.43', + ], + 'dompdf_log_output_file' => [ + 'is_domain' => 1, + 'is_contact' => 0, + 'group_name' => 'CiviCRM Preferences', + 'group' => 'core', + 'name' => 'dompdf_log_output_file', + 'title' => ts('DOMPDF Log File'), + 'description' => ts('DOMPDF will log debugging output in this file.'), + 'type' => 'String', + 'quick_form_type' => 'Element', + 'html_type' => 'text', + 'html_attributes' => [ + 'size' => 64, + 'maxlength' => 256, + ], + 'default' => NULL, + 'help_text' => NULL, + 'add' => '5.43', + ], 'wkhtmltopdfPath' => [ 'group_name' => 'CiviCRM Preferences', 'group' => 'core', diff --git a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl index 6b761b840e..bb7fc8892c 100644 --- a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl +++ b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl @@ -53,6 +53,24 @@

{ts}When generating a letter (PDF/Word) via mail-merge, how should the letter be recorded?{/ts}

+ + {$form.dompdf_font_dir.label} + {$form.dompdf_font_dir.html}
+

{ts}Additional folder where DOMPDF will look for fonts.{/ts}

+ + + + {$form.dompdf_chroot.label} + {$form.dompdf_chroot.html}
+

{ts}Folder to restrict where DOMPDF looks when loading local images. By default it is the DOMPDF folder itself for security reasons. It will search in subfolders.{/ts}

+ + + + {$form.dompdf_enable_remote.label} + {$form.dompdf_enable_remote.html}
+

{ts}Enable the use of remote images. By default this is enabled, but if not using remote images you may wish to turn it off for security reasons.{/ts}

+ + {$form.wkhtmltopdfPath.label} {$form.wkhtmltopdfPath.html}
diff --git a/tests/phpunit/CRM/Utils/PDF/UtilsTest.php b/tests/phpunit/CRM/Utils/PDF/UtilsTest.php new file mode 100644 index 0000000000..fbc459f456 --- /dev/null +++ b/tests/phpunit/CRM/Utils/PDF/UtilsTest.php @@ -0,0 +1,33 @@ +get('dompdf_log_output_file'); + + $log_file = tempnam(sys_get_temp_dir(), 'pdf'); + \Civi::settings()->set('dompdf_log_output_file', $log_file); + + $pdf_output = CRM_Utils_PDF_Utils::html2pdf('

Some output

', 'civicrm.pdf', TRUE); + // Not much of a test but this isn't the main thing we're testing. + $this->assertEquals('%PDF', substr($pdf_output, 0, 4)); + + // If the setting worked, we should have some debug output in this file. + // The exact contents might change between dompdf versions but it's likely + // to contain a span tag. + // If this is too brittle, it might be ok to just check it's not empty, + // since if it's empty then our setting didn't work. + $this->assertStringContainsString('set('dompdf_log_output_file', $old_setting); + } + +} -- 2.25.1