How to generate pdf using blade view with tcpdf laravel 5?

I am trying to create pdf using laravel 5 and https://github.com/elibyy/laravel-tcpdf

I have a form of 20 pages written in a crib blade when my client fills out the form and clicks the submit button. I want to create a PDF file for it and save it

I am trying to do this in my controller

public function createPDF()
{
    $pdf = Input::get('pdf',null);
    $company = Input::get('company',null);
    $branch = Input::get('branch',null);
    $sub_branch = Input::get('sub_branch',null);
    $form_type = Input::get('form_type',null);
    $form_name = Input::get('form_name',null);
    $form_heb_name = Input::get('form_heb_name',null);
    $sig_path=FormsController::getSignature_file();

    $data=compact('company','branch','sub_branch','form_type','form_name','form_heb_name','sig_path');
    Input::flash();
    if ($pdf) 
        { 
            $pdf = new TCPDF();
            $pdf->SetPrintHeader(false);
            $pdf->SetPrintFooter(false);
            $pdf->AddPage();
            $pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
            $filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
            $pdf->output($filename, 'I');
            return redirect('forms');
        }
     return view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name , $data);
}

bat it does not work, it creates a 2-page page with all the fields on top of each other

How to fix it?

In addition, I want to save the pdf so that it cannot be edited, how can I do it?

thank.

+4
source share
1 answer

I will try to give you a step-by-step answer (tested on laravel 5.1):

, , TCPDF:

TCPDF

composer.json :

"require": {
    "elibyy/laravel-tcpdf": "0.*"
}

config/app.php

'providers' => [
    //..
    Elibyy\TCPDF\ServiceProvider::class,
]

php artisan vendor: , config/laravel-tcpdf.php

PDF

public function createPDF()
{
    [...]
    //get default settings from config/laravel-tcpdf.php
    $pdf_settings = \Config::get('laravel-tcpdf');

   $pdf = new \Elibyy\TCPDF\TCPdf($pdf_settings['page_orientation'], $pdf_settings['page_units'], $pdf_settings['page_format'], true, 'UTF-8', false);

   $pdf->SetPrintHeader(false);
        $pdf->SetPrintFooter(false);
        $pdf->AddPage();
        $pdf->writeHTML(view('forms.'.$company.'.'.$branch.'.'.$sub_branch.'.'.$form_type.'.'.$form_name, $data)->render());
        $filename = storage_path().'/forms_pdf/10006/26/4718326/'.$form_name.'.pdf';
        $pdf->output($filename, 'I');
        return redirect('forms');

}
+5

All Articles