Skip to content

Commit

Permalink
Fixed 'PHP Warning: A non-numeric value encountered'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas Elbers committed May 23, 2017
1 parent d08e076 commit 5cbaff4
Showing 1 changed file with 85 additions and 88 deletions.
173 changes: 85 additions & 88 deletions mpdf.php
Expand Up @@ -107,7 +107,6 @@

class mPDF
{

///////////////////////////////
// EXTERNAL (PUBLIC) VARIABLES
// Define these in config.php
Expand Down Expand Up @@ -30553,103 +30552,101 @@ function _colAtoString($cor)

function ConvertSize($size = 5, $maxsize = 0, $fontsize = false, $usefontsize = true)
{
$scale = 72 / 25.4;
// usefontsize - set false for e.g. margins - will ignore fontsize for % values
// Depends of maxsize value to make % work properly. Usually maxsize == pagewidth
// For text $maxsize = Fontsize
// Setting e.g. margin % will use maxsize (pagewidth) and em will use fontsize
// Returns values using 'mm' units
$size = trim(strtolower($size));

if ($size == 'thin')
$size = 1 * (25.4 / $this->dpi); //1 pixel width for table borders
elseif (stristr($size, 'px'))
$size *= (25.4 / $this->dpi); //pixels
elseif (stristr($size, 'cm'))
$size *= 10; //centimeters
elseif (stristr($size, 'mm'))
$size += 0; //millimeters
elseif (stristr($size, 'pt'))
$size *= 25.4 / 72; //72 pts/inch
elseif (stristr($size, 'rem')) {
$size += 0; //make "0.83rem" become simply "0.83"
$size *= ($this->default_font_size / _MPDFK);
} elseif (stristr($size, 'em')) {
$size += 0; //make "0.83em" become simply "0.83"
if ($fontsize) {
$size *= $fontsize;
} else {
$size *= $maxsize;
}
} elseif (stristr($size, '%')) {
$size += 0; //make "90%" become simply "90"
if ($fontsize && $usefontsize) {
$size *= $fontsize / 100;
} else {
$size *= $maxsize / 100;
}
} elseif (stristr($size, 'in'))
$size *= 25.4; //inches
elseif (stristr($size, 'pc'))
$size *= 38.1 / 9; //PostScript picas
elseif (stristr($size, 'ex')) { // Approximates "ex" as half of font height
$size += 0; //make "3.5ex" become simply "3.5"
if ($fontsize) {
$size *= $fontsize / 2;
} else {
$size *= $maxsize / 2;
}
} elseif ($size == 'medium')
$size = 3 * (25.4 / $this->dpi); //3 pixel width for table borders
elseif ($size == 'thick')
$size = 5 * (25.4 / $this->dpi); //5 pixel width for table borders
elseif ($size == 'xx-small') {
if ($fontsize) {
$size *= $fontsize * 0.7;
} else {
$size *= $maxsize * 0.7;
}
} elseif ($size == 'x-small') {
if ($fontsize) {
$size *= $fontsize * 0.77;
} else {
$size *= $maxsize * 0.77;
}
} elseif ($size == 'small') {
if ($fontsize) {
$size *= $fontsize * 0.86;
} else {
$size *= $maxsize * 0.86;
}
} elseif ($size == 'medium') {
if ($fontsize) {
$size *= $fontsize;
} else {
$size *= $maxsize;
}
} elseif ($size == 'large') {
if ($fontsize) {
$size *= $fontsize * 1.2;
} else {
$size *= $maxsize * 1.2;
}
} elseif ($size == 'x-large') {
if ($fontsize) {
$size *= $fontsize * 1.5;
} else {
$size *= $maxsize * 1.5;
}
} elseif ($size == 'xx-large') {
if ($fontsize) {
$size *= $fontsize * 2;
} else {
$size *= $maxsize * 2;
}
} else
$size *= (25.4 / $this->dpi); //nothing == px
$res = preg_match('/^(?P<size>[-0-9.,]+)?(?P<unit>[%a-z-]+)?$/', $size, $parts);
if (!$res) {
throw new MpdfException(sprintf('Invalid size representation "%s"', $size));
}
$unit = !empty($parts['unit']) ? $parts['unit'] : null;
$size = !empty($parts['size']) ? (float) $parts['size'] : 0.0;
switch ($unit) {
case 'mm':
// do nothing
break;
case 'cm':
$size *= 10;
break;
case 'pt':
$size *= 1 / $scale;
break;
case 'rem':
$size *= ($this->default_font_size / (1 / $scale));
break;
case '%':
if ($fontsize && $usefontsize) {
$size *= $fontsize / 100;
} else {
$size *= $maxsize / 100;
}
break;
case 'in':
// mm in an inch
$size *= 25.4;
break;
case 'pc':
// PostScript picas
$size *= 38.1 / 9;
break;
case 'ex':
// Approximates "ex" as half of font height
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 0.5);
break;
case 'em':
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 1);
break;
case 'thin':
$size = 1 * (25.4 / $this->dpi);
break;
case 'medium':
$size = 3 * (25.4 / $this->dpi);
// Commented-out dead code from legacy method
// $size *= multiplyFontSize($fontsize, $maxsize, 1);
break;
case 'thick':
$size = 5 * (25.4 / $this->dpi); // 5 pixel width for table borders
break;
case 'xx-small':
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 0.7);
break;
case 'x-small':
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 0.77);
break;
case 'small':
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 0.86);
break;
case 'large':
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 1.2);
break;
case 'x-large':
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 1.5);
break;
case 'xx-large':
$size *= $this->MultiplyFontSize($fontsize, $maxsize, 2);
break;
case 'px':
default:
$size *= (25.4 / $this->dpi);
break;
}

return $size;
}

function MultiplyFontSize($fontsize, $maxsize, $ratio)
{
if ($fontsize) {
return $fontsize * $ratio;
}

return $maxsize * $ratio;
}

// mPDF 5.7.3 TRANSFORMS
function ConvertAngle($s, $makepositive = true)
{
Expand Down

9 comments on commit 5cbaff4

@Moganero
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced the existing file with this and am still getting some non-numeric value errors:

Warning: A non-numeric value encountered in /var/sites/s/example.com/php_libs/vendor/mpdf/mpdf/mpdf.php on line 17073
Warning: A non-numeric value encountered in /var/sites/s/example.com/php_libs/vendor/mpdf/mpdf/mpdf.php on line 17074
Warning: A non-numeric value encountered in /var/sites/s/example.com/php_libs/vendor/mpdf/mpdf/mpdf.php on line 17092

@baselbers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to change this.
c51b080

@mealexxxx
Copy link

@mealexxxx mealexxxx commented on 5cbaff4 Jul 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is in E_WARNING exception in PHP 7:
https://stackoverflow.com/a/42044413

I solved this problem just by replacing line 89:
$errorlevel = error_reporting($errorlevel & ~E_NOTICE);
with:
$errorlevel = error_reporting($errorlevel & ~(E_NOTICE|E_WARNING));

It's not error!

PS. You can even don't touch source file
Before calling mdpf, turn off E_WARNING
After it finished switch back

@Moganero
Copy link

@Moganero Moganero commented on 5cbaff4 Jul 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that's fixed it!

@baselbers
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RyuujiSalamander
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, saved me from hadache 😄

@baiano
Copy link

@baiano baiano commented on 5cbaff4 Aug 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix suggested by @mealexxxx resolved it. Thanks sir.

@mayank-mehrotra
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@baselbers you saved me Thanks a lot. It worked perfectly with PHP 7.1.9 for non-numeric value.

@maheshTandan
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem here is in E_WARNING exception in PHP 7:
https://stackoverflow.com/a/42044413

I solved this problem just by replacing line 89:
$errorlevel = error_reporting($errorlevel & ~E_NOTICE);
with:
$errorlevel = error_reporting($errorlevel & ~(E_NOTICE|E_WARNING));

It's not error!

PS. You can even don't touch source file
Before calling mdpf, turn off E_WARNING
After it finished switch back

thanks you so much dud that really work for me thanks a lot

Please sign in to comment.