mPDF and PHP Requirement Check

  1. خانه
  2. /
  3. PHP
  4. /
  5. mPDF and PHP Requirement Check

mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML. I have  used it in many projects, including WordPress plugins to generate PDF. The major problem with this library is it works in Test Environment but when you deploy and make live preview, Surprise 😲 IT DOES NOT WORK! And the more you dig in you will find out you cannot debug it! So, I had to make a path through this problem, said to myself why it works in Localhost, my another site, yet another site, but not this client’s site.

Oreka! It’s lacking something. Yeah, mbstring extension was enabled but, mb_regex_encoding was not working and had to enable it by myself or asking Host Provider to do.Thought to myself why not we first check the requirements then if everything was okay, continue to the next level? So I used code below to check if required modules are available and enabled.

function CheckPDFRequirements()
{
    foreach (array(
      "mbstring" => "extension",
      "gd"       => "extension",
      "zlib"     => "extension",
      "bcmath"   => "extension",
      "xml"      => "extension",
      "curl"     => "extension",
      "mb_regex_encoding" => "function",
    ) as $key => $value) {
        echo  "extension" == $value ?
          extension_loaded($key) ?
            "<p>extension <strong>$key</strong>: OK</p>" :
            "<p>extension <strong>$key</strong>: DOES NOT EXISTS</p>" :
          function_exists($key) ?
            "<p>function <strong>$key</strong>: OK</p>" :
            "<p>function <strong>$key</strong>: DOES NOT EXISTS</p>";
    }
}

So this code checks whether extensions are enabled or not, but what if we don’t want to show users anything unless extension is not enabled? Here is the complete solution which you can use in any production by just a bit of modification.

// Start PDF Creation Proccess
if (!$this->CheckPDFRequirements()) {
    // if we didn't pass the requirement test, show error and do not continue !
    $this->CheckPDFRequirements(true);
    return false;
}
// requirements test passed
new \Mpdf\Mpdf();

/**
* Check PDF requirements
*
* @method CheckPDFRequirements
* @param boolean $show_err show error or not
* @version 1.0.0
* @since 1.0.0
* @license https://hpv.im/license
* @copyright 2020 - Amirhosseinhpv (its@hpv.im)
*/
function CheckPDFRequirements($show_err = false)
{
    $ok = true;
    foreach (array(
    "mbstring" => "extension",
    "gd"       => "extension",
    "zlib"     => "extension",
    "bcmath"   => "extension",
    "xml"      => "extension",
    "curl"     => "extension",
    "mb_regex_encoding" => "function",
  ) as $key => $value) {
        if ($show_err) {
            echo  "extension" == $value ?
      extension_loaded($key) ?
      "<p>extension <strong>$key</strong>: OK</p>" :
      "<p>extension <strong>$key</strong>: DOES NOT EXISTS</p>" :
      function_exists($key) ?
      "<p>function <strong>$key</strong>: OK</p>" :
      "<p>function <strong>$key</strong>: DOES NOT EXISTS</p>";
        } else {
            if (!$ok) {
                return $ok;
            }
            if ("extension" == $value) {
                if (!extension_loaded($key)) {
                    $ok = false;
                }
            } else {
                if (!function_exists($key)) {
                    $ok = false;
                }
            }
        }
    }
    return $ok;
}

Do you know any other workaround? let me know by adding in comment section below.

کامنت ها (0)

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد.

این قسمت نباید خالی باشد
این قسمت نباید خالی باشد
لطفاً یک نشانی ایمیل معتبر بنویسید.
شما برای ادامه باید با شرایط موافقت کنید

دسته بندی:
PHPآموزشیتکه کدتوسعه
تگ ها:
Atom Issue: Multiple-cursor not working after update
Add Yoast SEO Breadcrumb to WordPress Theme
فهرست