Not able to set Landscape orientation in mPdf - niklasravnsborg/laravel-pdf

Viewed 2455

I am trying to generate the pdf using mPdf - niklasravnsborg/laravel-pdf in laravel 5.5. But every time I am getting the pdf in portrait mode.

public function showCertificate()
{
    $data = [ 'name' => 'John Doe', 'prize' => '1st Prize'];
    $pdf = PDF::loadView('certificates.show', 
        $data, 
        [], 
        [ 
          'title' => 'Certificate', 
          'orientation' => 'L'
        ]);
    return $pdf->stream('certificate.pdf');
}
2 Answers

I was finally able to get it resolved by using format to A4-L along with orientation. My working code is given below.

public function showCertificate()
{
    $data = [ 'name' => 'John Doe', 'prize' => '1st Prize'];
    $pdf = PDF::loadView('certificates.show', 
        $data, 
        [], 
        [ 
          'title' => 'Certificate', 
          'format' => 'A4-L',
          'orientation' => 'L'
        ]);
    return $pdf->stream('certificate.pdf');
}

I saw this issue and inferred from there to my answer.

Another subtle thing you may be missing if you encounter this same problem...take my case for example, I did not add the empty array syntax [] before the options/config array...

This will NOT work

public function showCertificate()
{
    $data = [ 'name' => 'John Doe', 'prize' => '1st Prize'];
    $pdf = PDF::loadView('certificates.show', 
        $data, 
      //  [], I removed the emptyy arrayy
        [ 
          'title' => 'Certificate', 
          'format' => 'A4-L',
          'orientation' => 'L'
        ]);
    return $pdf->stream('certificate.pdf');
}

This will work

public function showCertificate()
{
    $data = [ 'name' => 'John Doe', 'prize' => '1st Prize'];
    $pdf = PDF::loadView('certificates.show', 
        $data, 
       [],
        [ 
          'title' => 'Certificate', 
          'format' => 'A4-L',
          'orientation' => 'L'
        ]);
    return $pdf->stream('certificate.pdf');
}

Take not of [].

Related