jeic
jeic

Reputation: 1

PDF displaying in laravel

I tried displaying the PDF file in laravel but this keeps on displaying. I tried all the suggested codes in the internet, but it will either not display or display like in the picture. I don't know what the problem is :(

my code my code

output output

Upvotes: 0

Views: 3724

Answers (2)

Aslam Khan
Aslam Khan

Reputation: 378

The Go trough this link state that the first argument of Output() is the file path, second is the saving mode - you need to set it to 'F'.

 $upload_dir = public_path(); 
             $filename = $upload_dir.'/testing7.pdf'; 
              $mpdf = new \Mpdf\Mpdf();
              //$test = $mpdf->Image($pro_image, 0, 0, 50, 50);

              $html ='<h1> Project Heading </h1>';
              $mail = ' <p> Project Heading </p> ';
              
              $mpdf->autoScriptToLang = true;
              $mpdf->autoLangToFont = true;
              $mpdf->WriteHTML($mail);

              $mpdf->Output($filename,'F'); 
              $mpdf->debug = true;

Example :

 $mpdf->Output($filename,'F');

Example #2

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');

// Saves file on the server as 'filename.pdf'
$mpdf->Output('filename.pdf', \Mpdf\Output\Destination::FILE);

Upvotes: 0

Sudhanshu sharma
Sudhanshu sharma

Reputation: 1967

I have used this library to generate PDF in laravel. Please look at doc. https://github.com/barryvdh/laravel-dompdf

You can also take help of my code. First install the library and configure as instructed in doc. after that, In controller

// Put this in top
use Dompdf\Dompdf;
use Dompdf\Options;

create one function

public function DownloadPdf($id , $download = false){
      // instantiate and use the dompdf class
      $options = new Options();
      $options->set('enable_remote', true);
      $options->set('enable_css_float', true);
      $dompdf = new Dompdf($options);
      $dompdf->loadHtmlFile(action('HomeController@PdfHTML',['id'=>$id]));

      // (Optional) Setup the paper size and orientation
      $dompdf->setPaper('A4', 'portrait');

      // Render the HTML as PDF
      $dompdf->render();
      if($download == true){
        // Save PDF to server
        file_put_contents('uploads/pdf/document_'.$id.'.pdf', $dompdf->output());
      }else{
        // Output the generated PDF to Browser
        $dompdf->stream('document');
      }
    }

Now load the html file action

public function PdfHTML($id){

      return view('home.home_pdf');
    }

Now create home.blade.pdf

<h2>Hello world</h2>

Your PDF will be generated. Please let me know if you get any error

Upvotes: 3

Related Questions