skip to Main Content

I am using MPdf of version 8.0 and PHP version 7.1.
This is my code.

function generatePDF($order){
  $html = getHTML($order);
  try {
  $mpdf = new MpdfMpdf(['margin_left' => 20,
    'margin_right' => 15,
    'margin_top' => 48,
    'margin_bottom' => 25,
    'margin_header' => 10,
    'margin_footer' => 10]);
  $mpdf->SetProtection(array('print'));
  $mpdf->SetDisplayMode('fullpage');
  $mpdf->WriteHTML($html);
  $mpdf->Output('ABCD.pdf','I');
}catch (MpdfMpdfException $e) { // Note: safer fully qualified exception name used for catch
    // Process the exception, log, print etc.
    $error = $e->getMessage();
    echo $e->getMessage();
}

This code is working perfectly for desktop. If I use my laptop or any desktop, it generate PDF very well. But, When I am checking it on mobile device, it do not downloading any PDF. It is also not throwing any exceptions or errors.

I have debugged each code line, every line is executed very well as expected just not generating PDF.

2

Answers


  1. Instead of I parameter in $mpdf->Output('ABCD.pdf','I');, you can use F parameter. See the link here for more information.

    F parameter create the file in the server in specific directory without any problem that usually cause when using I or D parameter: $mpdf->Output('/DIRABCD.pdf','F');

    After that you can force the browser (mobile or PC) to download the file using readfile() or fread() functions in PHP.

    The code would be like this:

    function generatePDF($order){
      $html = getHTML($order);
      try {
      $mpdf = new MpdfMpdf(['margin_left' => 20,
        'margin_right' => 15,
        'margin_top' => 48,
        'margin_bottom' => 25,
        'margin_header' => 10,
        'margin_footer' => 10]);
      $mpdf->SetProtection(array('print'));
      $mpdf->SetDisplayMode('fullpage');
      $mpdf->WriteHTML($html);
      $mpdf->Output('/DIR/ABCD.pdf','F');
      
        ob_start();
        header("Content-type:application/pdf");
        header("Content-Disposition:attachment;ABCD.pdf");
        readfile("/DIR/ABCD.pdf");
        exit;
      
    }catch (MpdfMpdfException $e) { // Note: safer fully qualified exception name used for catch
        // Process the exception, log, print etc.
        $error = $e->getMessage();
        echo $e->getMessage();
    }
    
    Login or Signup to reply.
  2. Try to get you buffer clean before writing the HTML to pdf. Here is your updated code.

    function generatePDF($order){
        $html = getHTML($order);
        try {
            $mpdf = new MpdfMpdf(['margin_left' => 20,
            'margin_right' => 15,
            'margin_top' => 48,
            'margin_bottom' => 25,
            'margin_header' => 10,
            'margin_footer' => 10]);
            $mpdf->SetProtection(array('print'));
            $mpdf->SetDisplayMode('fullpage');
            
            ob_get_clean(); // Add this line before writing the HTML
            
            $mpdf->WriteHTML($html);
            $mpdf->Output('/DIR/ABCD.pdf','F');
        
            exit;
        
        } catch (MpdfMpdfException $e) { // Note: safer fully qualified exception name used for catch
          // Process the exception, log, print etc.
          $error = $e->getMessage();
          echo $e->getMessage();
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search