mpdf在使用图像时除以零

时间:2015-01-28 14:49:49

标签: php mpdf

我试图使用mpdf库构建pdf ...

        include('convert-to-pdf/mpdf.php');


        // build the content for the preview...

        ob_start();

                include (DIR_BOOKS.'/'.$book_path.'/preview-male.php');
                $html = ob_get_contents();
        ob_end_clean(); 

        // build the overlay on the child image 

        $mpdf=new mPDF('utf-8', array(221,221));


        $stylesheet = file_get_contents(DIR_BOOKS.$book_path.'/stylesheet.css');
        $mpdf->WriteHTML($stylesheet,1);    
        $mpdf->WriteHTML($html,2);      

        $mpdf->Output(DIR_TEMPORARY_IMAGES.$this->session->data['pdf_id'].'.pdf','F');

然后mpdf中的代码我得到了错误......

$this->WriteHTML($html , 4);    // parameter 4 saves output to $this->headerbuffer
$actual_h = $this->y - $y;
$use_w = $w;
$use_h = $h;

//Line 13865 
$ratio = $actual_h / $use_w;

if ($overflow!='hidden' && $overflow!='visible') {
    //Line 13868 
    $target = $h/$w;
    if (($ratio / $target ) > 1) {
        $nl = CEIL($actual_h / $this->lineheight);
        $l = $use_w * $nl;
        $est_w = sqrt(($l * $this->lineheight) / $target) * 0.8;
        $use_w += ($est_w - $use_w) - ($w/100);
    }
    //Line 13875
    $bpcstart = ($ratio / $target);
    $bpcctr = 1;
    while(($ratio / $target ) > 1) {

        if ($this->progressBar) { $this->UpdateProgressBar(4,intval(100/($ratio/$target)),('Auto-sizing fixed-position block: '.$bpcctr++)); }  // *PROGRESS-BAR*

        $this->x = $x;
        $this->y = $y;

        if (($ratio / $target) > 1.5 || ($ratio / $target) < 0.6) {
            $use_w += ($w/$this->incrementFPR1);
        }
        else if (($ratio / $target) > 1.2 || ($ratio / $target) < 0.85) {
            $use_w += ($w/$this->incrementFPR2);
        }
        //Line 13890
        else if (($ratio / $target) > 1.1 || ($ratio / $target) < 0.91) {
            $use_w += ($w/$this->incrementFPR3);
        }
        else {
            $use_w += ($w/$this->incrementFPR4);
        }

        $use_h = $use_w * $target ;
        $this->rMargin=$this->w - $use_w - $x;
        $this->pgwidth = $this->w - $this->lMargin - $this->rMargin;
        $this->HTMLheaderPageLinks = array();
        $this->HTMLheaderPageAnnots = array();
        $this->HTMLheaderPageForms = array();
        $this->pageBackgrounds = array();
        $this->WriteHTML($html , 4);    // parameter 4 saves output to $this->headerbuffer
        $actual_h = $this->y - $y;
        $ratio = $actual_h / $use_w;
    }
    if ($this->progressBar) { $this->UpdateProgressBar(4,'100',' '); }  // *PROGRESS-BAR*
}
$shrink_f = $w/$use_w;

我的php文件即可从...中获取html

<div class="page-1-image-1"><img src="https://www.example.com/working-image.jpg" /> </div>

当我在mpdf html代码中包含图像时,我得到以下错误....

<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13865</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13868</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13869</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13875</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/stuff.php</b> on line <b>13877</b>
<b>Warning</b>: Division by zero in <b>/var/www/html/example/mpdf.php</b> on line <b>13910</b>
<B>mPDF error: </B>Please do not use values equal to zero for scaling

我已经尝试了两种新生成的图像,但也尝试了其他非常好的图像网址,而且没有任何乐趣。我试过pngs和jpgs。

我怀疑这些错误来自一个只是没有加载图片的问题。提前谢谢。

1 个答案:

答案 0 :(得分:0)

所以正在发生的事情是你试图将数字除以0,这是行不通的!数学不允许这样做。 (例如5 / 0 = ? -> ? * 0 = 5

这一行作为一个例子:

$ratio = $actual_h / $use_w;

您收到错误,因为$use_w为0!那么你如何防止这些错误?


您可以这样检查:

if($use_w != 0) {
    //Here you could end the script OR assign it to 1, just make sure you never use $use_w with the value 0
    $ratio = $actual_h / $use_w;
}