Vikas Gupta
Vikas Gupta

Reputation: 1233

MPdf not generating PDF on mobile devices. Works perfectly on desktops

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 \Mpdf\Mpdf(['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 (\Mpdf\MpdfException $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.

Upvotes: 0

Views: 1023

Answers (2)

Tassawer
Tassawer

Reputation: 39

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 \Mpdf\Mpdf(['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 (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception name used for catch
      // Process the exception, log, print etc.
      $error = $e->getMessage();
      echo $e->getMessage();
    }
}

Upvotes: 0

MJB
MJB

Reputation: 65

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 \Mpdf\Mpdf(['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 (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception name used for catch
    // Process the exception, log, print etc.
    $error = $e->getMessage();
    echo $e->getMessage();
}

Upvotes: 1

Related Questions