Can't get rid of PHP notices in mPDF

后端 未结 3 647
不思量自难忘°
不思量自难忘° 2021-01-20 11:24

I\'m using the mPDF library to generate PDF docs directly from HTML output. The problem is that this mPDF library is written as it is and it is generating dozens of notices

相关标签:
3条回答
  • 2021-01-20 11:37

    If you use $mpdf->Output() after ob_end_clean(), you can even display the PDF without notices in the browser! I use this in OpenCart. But you need to use ob_start() and ob_end_clean().

    0 讨论(0)
  • 2021-01-20 11:47

    It seems the error occurs just when it tries to write out the new table headings for each page. I commented out in V 5.4 line 26210

    #$this->TableHeaderFooter($tablefooter,$tablestartpage,$tablestartcolumn,'F',$level, $firstSpread, $finalSpread);   // mPDF 5.3.36
    

    The heading where not getting drawn regardless so commenting out this line had no effect other than killing the notices.

    0 讨论(0)
  • 2021-01-20 11:51

    While there is no answer and because I've not found any other appropriate solution, here is summary of what I have so far (mainly copy from my question above):

    ob_start(); // <--| This is very important to start output buffering and to catch out any possible notices
    include(DIR_MPDF.'mpdf.php');
    $html = $this->render(TRUE);
    
    $mpdf = new mPDF('utf-8','A4');
    
    $mpdf->useOnlyCoreFonts = true;
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->SetAutoFont(0);
    
    $stylesheet = file_get_contents(DIR_APPLICATION.'view/stylesheet/declaration.css');
    $mpdf->WriteHTML($stylesheet,1); // <--| By the second param we are saying to MPDF that it is icnluding only stylesheet
    
    $mpdf->WriteHTML($html);
    
    $pdf = $mpdf->Output('', 'S'); // <--| With the binary PDF data in $pdf we can do whatever we want - attach it to email, save to filesystem, push to browser's PDF plugin or offer it to user for download
    $ob = ob_get_contents(); // <--| Here we catch out previous output from buffer (and can log it, email it, or throw it away as I do :-) )
    ob_end_clean(); // <--| Finaly we clean output buffering and turn it off
    
    // The next headers() section is copied out form mPDF Output() method that offers a PDF file to download
    if (headers_sent())
        die('Some data has already been output to browser, can\'t send PDF file');
    header('Content-Description: File Transfer');
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: public, must-revalidate, max-age=0');
    header('Pragma: public');
    header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header('Content-Type: application/force-download');
    header('Content-Type: application/octet-stream', false);
    header('Content-Type: application/download', false);
    header('Content-Type: application/pdf', false);
    if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
        header('Content-Length: '.strlen($pdf));
    }
    header('Content-disposition: attachment; filename="invoice.pdf"');
    echo $pdf; // <--| With the headers set PDF file is ready for download after we call echo
    exit;
    

    As written in the comment above, it is then just upon me (or client :-) ) what will be done with the PDF data returned from mPDF. I use this PDF generating on more places through the application and mostly offer PDF just for download but I also attaches it to email (user makes a payment, I generate the PDF invoice and send it via email).

    I have found no solution (and also have no more time to do so) to stop mPDF generate notices and haven't yet lost my mind to "repair" the mpdf.php (with its 1.34 MB of PHP code) therefore this is (for now) the only solution that works for me.

    Maybe it will help somebody.

    0 讨论(0)
提交回复
热议问题